iosobjective-cswiftintercomintercom-ios

Integrating Intercom into Swift iOS app


I am trying to use the Intercom iOS SDK in a Swift app that I am building. However, all their documentation is written in Objective-C. As I am just starting out I am not quite sure how to implement Intercom in my application.

I've added "pod 'Intercom'" to my Podfile and added the import statement to my bridging header.

The next step in setting up intercom in an iOS app is initializing it in my applications AppDelegate with the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize Intercom
[Intercom setApiKey:@"<#ios_sdk-...#>" forAppId:@"<#your-app-id#>"];
}

And then using objective-c code in my swift view-controller like so:

- (void)successfulLogin { 
...
// Registering with Intercom is easy. For best results, use a unique user_id if you have one.
[Intercom registerUserWithUserId:@"<#123456#>"];
}

How do I implement Objective-c code like this in my swift project?

I've searched around, as well as contacted Intercom's support, but am yet to find an answer.

Many thanks!


Solution

  • [Intercom setApiKey:@"<#ios_sdk-...#>" forAppId:@"<#your-app-id#>"];
    

    becomes

    Intercom.setApiKey("<#ios_sdk-...#>", forAppId:"<#your-app-id#>")
    

    and

    [Intercom registerUserWithUserId:@"<#123456#>"];
    

    becomes

    Intercom.registerUserWithUserId("<#123456#>")
    

    I would suggest to look over Swift tutorials to see the syntax and learn how to migrate Objective-C code to Swift code properly.