Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set up Amazon Amplify iOS in Objective-C?

The docs show Swift code only. When trying to use Objective-C, I can't access any of the Amplify libraries. Am I missing an installation step?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

do {
    try Amplify.add(plugin: AWSCognitoAuthPlugin())
    try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
    try Amplify.configure()
    print("Amplify configured with Auth and Analytics plugins")
} catch {
    print("Failed to initialize Amplify with \(error)")
}

return true

}

How to do the equivalent in Objective-C?

like image 393
tperei Avatar asked Jul 08 '20 23:07

tperei


People also ask

Can you build iOS app on AWS?

In the first module, you'll build a simple iOS application. Through the remaining modules, you will initialize a local app using the Amplify Command Line Interface (Amplify CLI), add user authentication, add a GraphQL API and a database to store your data, and update your app to store images.

What is AWS amplify iOS?

Amplify iOS is a collection of open-source client libraries that provides interfaces for specific use cases across many of the AWS services. Amplify iOS is the recommended way to build native iOS applications powered by AWS.

Is AWS amplify worth it?

AWS Amplify can be very much useful for web and mobile developers. All built-in authentication, notifications, and APIs can be created with a minimal workload for full-stack applications.


2 Answers

Yes, crusty old Objective C apps still exist. And they have to be maintained. It really isn't reasonable to expect developers to rewrite them in Swift just so they can use Amplify. I was recently asked to add Amplify to an app built in early 2015 before Swift existed. (Gosh -- are there really apps out there that are over 5 years old?!)

Fortunately, if you can bite the bullet and add Swift support to your Objective C project, it isn't so hard to make a Swift wrapper class that you use from Objective C. Here's one I created for free. It would be nice if the highly paid folks at Amazon would be kind enough to help us out with examples like this.

import Amplify
import AmplifyPlugins

@objc
class AmplifyWrapper: NSObject {
    override init() {
        super.init()
    }
    @objc
    public func initialize() {
        do {
            try Amplify.add(plugin: AWSCognitoAuthPlugin())
            try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
            try Amplify.configure()
            print("Amplify configured with Auth and Analytics plugins")
        } catch {
            print("Failed to initialize Amplify with \(error)")
        }
    }
    @objc
    public func recordEvent(name: String, category: String, accountId: String) {
        let properties: AnalyticsProperties = [ "category": category, "accountId": accountId]
        let event = BasicAnalyticsEvent(name: name, properties: properties)
        Amplify.Analytics.record(event: event)
    }
}

Use form Objective C like this:

#import "PutYourAppNameHere-Swift.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[[AmplifyWrapper alloc] init] initialize];
    ...
}

Then later on you can do this:

  [[[AmplifyWrapper alloc] init] recordEventWithName:@"App Opened" category:@"Counts" accountId:""];

like image 162
davidgyoung Avatar answered Sep 20 '22 16:09

davidgyoung


Am I missing an installation step?

You're not missing anything. Unfortunately there's no Objective-C support on Amplify for iOS. Amplify was built in Swift using its full capabilities and Objective-C support is not currently being considered unless there's a strong demand from the community.


Out of curiosity: are you starting a new app in Objective-C? If so I would be curious to understand why not going with Swift given Apple's investment on Swift lately (Combine, SwiftUI, Swift language updates, etc).

If you're trying to integrate Amplify in an existing Objective-C app, then I'm afraid it won't be possible.

like image 20
Daniel Rochetti Avatar answered Sep 20 '22 16:09

Daniel Rochetti