Setting up the iOS side for Copilot.cx SDK
Please follow steps 1 through 9 in Importing the iOS SDK
Add a CopilotModule
class through which you'll interface with your flutter dart code using the FlutterMethodChannel
.
Inside the setupMethodChannel
function, we'll "capture" function calls from the dart side(with or without parameters) and point them to our native side fuctions.
In this following example we're using the signup
, login
, logout
and sendEvent
functions.
import Flutter
import CopilotAPIAccess
class CopilotModule {
private let channel = "myProjectChannelName"
init ( _ controller: FlutterViewController ) {
setupMethodChannel ( with: controller)
}
fileprivate func setupMethodChannel ( with controller: FlutterViewController ) {
let methodChannel = FlutterMethodChannel ( name: channel, binaryMessenger: controller. binaryMessenger)
methodChannel. setMethodCallHandler { ( call: FlutterMethodCall , result: @escaping FlutterResult ) in
switch ( call. method) {
case "signup" :
if let args = call. arguments as ? [ String : Any ] {
if let email = args[ "email" ] as ? String ,
let password = args[ "password" ] as ? String ,
let firstName = args[ "firstName" ] as ? String ,
let lastName = args[ "lastName" ] as ? String {
self . signup ( email: email, password: password, firstName: firstName, lastName: lastName, result: result)
}
}
case "login" :
if let args = call. arguments as ? [ String : Any ] {
if let email = args[ "email" ] as ? String ,
let password = args[ "password" ] as ? String {
self . login ( email: email, password: password, result: result)
}
}
case "logout" :
self . logout ( result: result)
default :
result ( FlutterMethodNotImplemented )
}
}
}
fileprivate func signup ( email: String , password: String , firstName: String , lastName: String , result: @escaping FlutterResult ) {
Copilot . instance. manage. sphere. auth. signup ( ) . withCopilotAnalysisConsent ( true ) . with ( email: email, password: password, firstname: firstName, lastname: lastName) . build ( ) . execute { response in
switch ( response) {
case . success ( ) :
result ( true )
break
case . failure ( error: let error) :
result ( FlutterError ( code: "signup failed: \( error. localizedDescription ) " , message: nil , details: nil ) )
break
}
}
}
fileprivate func login ( email: String , password: String , result: @escaping FlutterResult ) {
Copilot . instance. manage. sphere. auth. login ( ) . with ( email: email, password: password) . build ( ) . execute { response in
switch ( response) {
case . success ( ) :
result ( true )
break
case . failure ( error: let error) :
result ( FlutterError ( code: "login failed: \( error. localizedDescription ) " , message: nil , details: nil ) )
break
}
}
}
fileprivate func logout ( result: @escaping FlutterResult ) {
Copilot . instance. manage. sphere. auth. logout ( ) . build ( ) . execute { response in
switch ( response) {
case . success ( ) :
result ( true )
break
case . failure ( error: let error) :
result ( FlutterError ( code: "logout failed: \( error. localizedDescription ) " , message: nil , details: nil ) )
break
}
}
}
}
Initialize the CopilotModule
in your AppDelegate
through which all Copilot functions will be activated and then initialize Copilot.cx, setting it with a list of EventLogProviders
as shown below (please refer the Managing providers section).
import CopilotAPIAccess
override func application (
_ application: UIApplication ,
didFinishLaunchingWithOptions launchOptions: [ UIApplication . LaunchOptionsKey : Any ] ?
) -> Bool {
...
let controller = window? . rootViewController as ! FlutterViewController
let copilotModule = CopilotModule ( controller)
Copilot . setup ( analyticsProviders: [ FirebaseAnalyticsProvider ( ) ] )
...
}