我會建議對以下是上述建議的推動模式並切換到拉範例。 AWSIdentityProviderManager的用途是僅在SDK需要時才提示您輸入令牌,而不是讓您在SDK的外部定期進行設置,以確定SDK是否需要它。這樣,您不必自己管理令牌過期,只需確保您的令牌在調用登錄名時有效,如果不是,則可以使用AWSCompletionSource獲取新令牌。
假設你已經整合Facebook登錄,您的IdentityProviderManager應該是這個樣子:
import Foundation
import AWSCore
import FacebookLogin
import FacebookCore
class FacebookProvider: NSObject, AWSIdentityProviderManager {
func logins() -> AWSTask<NSDictionary> {
if let token = AccessToken.current?.authenticationToken {
return AWSTask(result: [AWSIdentityProviderFacebook:token])
}
return AWSTask(error:NSError(domain: "Facebook Login", code: -1 , userInfo: ["Facebook" : "No current Facebook access token"]))
}
}
要使用它:
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.YOUR_REGION, identityPoolId: "YOUR_IDENTITY_POOL_ID", identityProviderManager: FacebookProvider())
let configuration = AWSServiceConfiguration(region: AWSRegionType.usEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
再進行試驗,獲得證書:
AWSServiceManager.default().defaultServiceConfiguration.credentialsProvider.credentials().continue(with: AWSExecutor.default(), with: { (task) -> Any? in
print(task.result ?? "nil")
return task
})
順便說一句,我需要將此添加到我的應用程序委託來讓Facebook登錄工作w斯威夫特這裏沒有提到https://developers.facebook.com/docs/swift/login:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}