我想使用Facebook進行Cognito登錄流程,並且我遵循了很多AWS文檔,並查看了Stackoverflow中的教程和問題,但是我沒有找到解決方案我的問題。Swift - AWS Cognito-Facebook登錄
當用戶打開應用程序時,它將檢查用戶是否使用IdentityManager登錄。如果沒有,它將打開一個新的視圖,用戶可以在Facebook上使用Facebook SDK登錄。之後,如文檔所述,我使用自定義IdentityProvider存儲了令牌(不贊成使用credentialsProvider.logins)。似乎一切正常,但每次我重新打開應用程序,我的會話不會恢復。
我發現如果我使用AWSIdentityManager.defaultIdentityManager().resumeSessionWithCompletionHandler(handler)
我恢復了我的會話,但萬一用戶沒有登錄,它不會按預期顯示我的自定義登錄屏幕,而是顯示到Facebook的Safari網頁瀏覽。
這裏是我的代碼:
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let identityManager = AWSIdentityManager.defaultIdentityManager()
identityManager.resumeSessionWithCompletionHandler({
(result, error) -> Void in
if !identityManager.loggedIn {
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SignIn") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
})
return AWSMobileClient.sharedInstance.didFinishLaunching(application, withOptions: launchOptions)
}
SignInViewController.swift
@IBAction func openFacebookLoginScreen(sender: AnyObject) {
FBSDKLoginManager().logInWithReadPermissions(FACEBOOK_PERMISSIONS, fromViewController: self, handler: { (result, error) -> Void in
if error == nil {
let fbLoginResult : FBSDKLoginManagerLoginResult = result
if fbLoginResult.isCancelled {
print("Cancelled")
}
else {
if FBSDKAccessToken.currentAccessToken() != nil {
self.signInFacebook(FBSDKAccessToken.currentAccessToken().tokenString)
self.dismissSignInView()
}
}
}
})
}
func signInFacebook(fbToken: String){
let logins = [AWSIdentityProviderFacebook : fbToken]
let customProviderManager = CustomIdentityProvider(tokens: logins)
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType:.USEast1,
identityPoolId: COGNITO_IDENTITY_POOL_ID,
identityProviderManager: customProviderManager)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
}
class CustomIdentityProvider: NSObject, AWSIdentityProviderManager {
var tokens : [NSString : NSString]?
init(tokens: [NSString : NSString]) {
self.tokens = tokens
}
@objc func logins() -> AWSTask {
return AWSTask(result: tokens)
}
}