gpt4 book ai didi

ios - 如何在 Objective-C 中设置 Amazon Amplify iOS?

转载 作者:行者123 更新时间:2023-12-01 15:46:28 26 4
gpt4 key购买 nike

docs仅显示 Swift 代码。尝试使用 Objective-C 时,我无法访问任何 Amplify 库。我错过了安装步骤吗?

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
}
如何在 Objective-C 中做同样的事情?

最佳答案

是的,老旧的 Objective C 应用程序仍然存在。他们必须得到维护。期望开发人员在 Swift 中重写它们以便他们可以使用 Amplify 确实是不合理的。我最近被要求将 Amplify 添加到 2015 年初在 Swift 出现之前构建的应用程序中。 (天哪——真的有超过 5 年的应用程序吗?!)
幸运的是,如果你能硬着头皮为你的 Objective C 项目添加 Swift 支持,那么从 Objective C 中创建一个使用的 Swift 包装类并不难。这是我免费创建的。如果亚马逊的高薪人员能够友善地帮助我们提供这样的示例,那就太好了。

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)
}
}

像这样使用表单Objective C:
#import "PutYourAppNameHere-Swift.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[[AmplifyWrapper alloc] init] initialize];
...
}
然后稍后你可以这样做:
  [[[AmplifyWrapper alloc] init] recordEventWithName:@"App Opened" category:@"Counts" accountId:""];

关于ios - 如何在 Objective-C 中设置 Amazon Amplify iOS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62805269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com