不推荐使用invokeApplication(_:didFinishLaunchingWithOptions :)

时间:2018-11-12 02:55:15

标签: amazon-web-services

只需将AWSMobileClient从2.6.8升级到2.7.0,然后看到以下警告消息行:

  

'interceptApplication(_:didFinishLaunchingWithOptions :)'已弃用:此方法将在下一个次要版本中删除。请更新为使用initialize使用AWSMobileClient。请访问https://aws-amplify.github.io以获取最新的iOS文档。

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

我不知道应该将interceptApplication更改为什么。确实有一个initialize函数。我们是否应该将其放在application(didFinishLaunchingWithOptions: )中并始终返回true

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

    AWSMobileClient.sharedInstance().initialize { (userState, error) in
        // ...
    }

    return true
}

1 个答案:

答案 0 :(得分:0)

本周我也遇到过同样的问题。在最新的documentation,特别是initialization部分中,您会看到它说:

  

打开Xcode项目的AppDelegate,或者在视图控制器中打开viewDidLoad()并调用初始化例程

所以我要做的是转到AppDelegate并替换:

return AWSMobileClient.sharedInstance().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)

使用:

return true

然后在主viewDidLoad中的UIViewController中,按照文档调用初始化例程:

import AWSMobileClient

override func viewDidLoad() {
    super.viewDidLoad()

    AWSMobileClient.sharedInstance().initialize { (userState, error) in
        if let userState = userState {
            print("UserState: \(userState.rawValue)")
        } else if let error = error {
            print("error: \(error.localizedDescription)")
        }
    }
}

然后,您将可以使用新的用户状态监听器进行应用程序的身份验证逻辑:

AWSMobileClient.sharedInstance().addUserStateListener(self) { (userState, info) in
    switch (userState) {
        case .guest:
            print("user is in guest mode.")
        case .signedOut:
            print("user signed out")
        case .signedIn:
            print("user is signed in.")
        case .signedOutUserPoolsTokenInvalid:
            print("need to login again.")
        case .signedOutFederatedTokensInvalid:
            print("user logged in via federation, but currently needs new tokens")
        default:
            print("unsupported")
    }
}

现在您也可以使用getTokens

AWSMobileClient.sharedInstance().getTokens { (tokens, error) in
    if let error = error {
        print("Error getting token \(error.localizedDescription)")
    }
    else if let tokens = tokens {
        print("id token: \(tokens.idToken!.tokenString!)")
        print("access token: \(tokens.accessToken!.tokenString!)")
    }
}

希望有帮助!