Using the AppConnect framework in a Swift app

First time use of SDK in your Swift app

The following procedure describes what to do to add the AppConnect framework to a Swift app.

When you add the AppConnect framework into your Xcode project, the Swift interfaces corresponding to all the Objective-C APIs are automatically generated by Xcode.

Before you begin 

Do the tasks in Before you begin adding the AppConnect SDK to your app.

Procedure 

  1. Do the following steps from the Getting started task list:

    1. Add AppConnect files and settings to your Xcode project.

    2. Add your own libcrypto.a, libProtocolBuffers.a, and libssl.a libraries if needed.

    3. Register as a handler of the AppConnect URL scheme.

    4. Declare the AppConnect URL schemes as allowed.

    5. Add AppConnect-related entries to your Info.plist.

    6. Optional: Specify app permissions and configuration in a plist file

  2. Add a file named main.swift to your Xcode project, if you don’t already have one.

  3. In main.swift, add the following code:

      import Foundation
      import AppConnect
       
      UIApplicationMain(
          CommandLine.argc,
          UnsafeMutableRawPointer(CommandLine.unsafeArgv)
              .bindMemory(
                  to: UnsafeMutablePointer<Int8>.self,
                  capacity: Int(CommandLine.argc)),
          ACUIApplicationClassName,
          NSStringFromClass(YourAppDelegate.self)
      )
  4. Add a bridging header file, if you don’t already have one, to your Xcode project. Name the file:

    <app name>-Bridging-Header.h

    Example:

    HelloSwiftAppConnect-Bridging-Header.h

  5. In the bridging header file, import AppConnect.h:

    #import <AppConnect/AppConnect.h>

  6. Go to your Xcode project's Build Settings for the Swift app target. Under Swift Compiler - General, set Objective-C Bridging Header to the bridging header file, including the path.

  7. Create a class that implements the AppConnectDelegate protocol. Usually this class is also the AppDelegate for your app.

  8. Initialize the AppConnect class with your AppConnectDelegate, save the singleton instance of the AppConnect library, and initialize the AppConnect library. Then wait for the initialization to complete.

    The following code is an excerpt from HelloSwiftAppConnect in the file HSAppDelegate.swift:

  import UIKit
  import AppConnect
 
  class HSAppDelegate: UIResponder, UIApplicationDelegate, AppConnectHandler {
 
  	  var appConnect: AppConnect?
 
	  func application(_ application: UIApplication, 
		  didFinishLaunchingWithOptions launchOptions: 
		  [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
 
		  AppConnect.log(at: .status, message: "HelloAppConnect started")
		  self.startAppConnect(launchOptions: launchOptions)
        		  return true
	  }
 
	  func startAppConnect(launchOptions: [AnyHashable : Any]? = [:]) {
 
		AppConnect.initWith(self)
		self.appConnect = AppConnect.sharedInstance()
		self.appConnect!.start(launchOptions: launchOptions)
 
		// Wait for appConnectIsReady() before using any of the AppConnect 
		// singleton’s instance properties. The app can use AppConnect class properties 
		// and methods of the AppConnect singleton object.
		// If your app uses AppTunnel with HTTP/S tunneling, be sure to register any
		// NSURLProtocol subclasses AFTER initializing the AppConnect library.
 
		// Indicate in the user interface that the app is initializing if the app requires 
		// the AppConnect singleton’s instance properties to determine what to do. For example, 
		// use an activity indicator (spinner). Remove the indication after the app is notified 
		// that the AppConnect singleton is ready.
		// One reason this indication is important involves when to display sensitive data. Do
		// not show any sensitive data until the AppConnect singleton is ready, because until
		// that time, the app cannot determine whether it is authorized. Only an authorized app
		//should show sensitive data.
	  }
 
	  func appConnectIsReady(_ appConnect: AppConnect) {
		// The app can now use the AppConnect singleton’s instance properties.
 
		self.updateLabels()
	  }
 
}

If your application supports UIScene, call the method sceneWillConnectToSession(with:).

The app must call the method from its UISceneDelegate's method scene(_:willConnectTo:options:), and must pass along the UIScene connection options as input parameter to the AppConnect instance method sceneWillConnectToSession(with:).

Example:

 
class MySceneDelegate: UIResponder, UIWindowSceneDelegate {
	func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions){
		AppConnect.sharedInstance()?.sceneWillConnectToSession(with: connectionOptions)
	}
}

Tasks for upgrading the SDK in your Swift app

If you are upgrading your Swift app from a previous version of the AppConnect for iOS SDK:

  • Replace the AppConnect.framework bundle in the project folder with AppConnect.xcframework.
  • If you are using the AppConnectExtension.framework, replace the AppConnectExtension.framework bundle in the project folder with the AppConnectExtension.xcframework bundle.