2017-05-07 13 views
7

我正在使用;facebook swift sdk 0.2.0 swift 3.1登錄頁面保持打開狀態

  • 迅速SDK 0.2.0和
  • SWIFT 3.1

所以,在我的viewcontrollerinitial.swift)我

import UIKit 
import FacebookCore 
import FacebookLogin 
... 
override func viewDidLoad() { 
if let accessToken = AccessToken.current { 
    print("User is already logged in") 
    ... 
} 
else { 
    let loginButton = LoginButton(readPermissions: [ .publicProfile, .email ]) 
    loginButton.center = view.center 

    //AFTER ACTION 
    let loginManager = LoginManager() 
    loginManager.logIn([ .publicProfile, .email ], viewController: self) 
     { loginResult in 
      switch loginResult { 
      case .failed(let error): 
       print(error) 
       break 
      case .cancelled: 
       print("User cancelled login.") 
      case .success(let grantedPermissions, let declinedPermissions, let accessToken): 
       print("Logged in!") 
      } 
     } 
}  

登錄按鈕顯示爲預期,但允許後頁面,白頁保持打開,當我點擊「完成」手動應用程序認爲用戶取消登錄。
也有類似的線程暗示失蹤的應用程序delegate函數(34734885),但我想這是一個解決方案FBSDKCoreKitFBSDKLoginKit,不適用於新的facebook swift sdk。我猜也是。任何建議?

編輯
我也試過這個;

let myLoginButton = UIButton() 
myLoginButton.backgroundColor = UIColor.darkGray 
myLoginButton.frame = CGRect(0, 0, 180, 40); 
myLoginButton.center = view.center; 
myLoginButton.setTitle("My Login Button", for: UIControlState.normal) 
// Add the button to the view 
view.addSubview(myLoginButton) 
// Handle clicks on the button 
myLoginButton.addTarget(self, action: #selector(self.FBloginButtonClicked), for: UIControlEvents.touchUpInside) 


@objc func FBloginButtonClicked() { 
    //AFTER ACTION 
    let loginManager = LoginManager() 
    loginManager.logIn([ .publicProfile, .email ], viewController: self) 
    { loginResult in 
     switch loginResult { 
     case .failed(let error): 
      print(error) 
      break 
     case .cancelled: 
      print("User cancelled login.") 
     case .success(let grantedPermissions, let declinedPermissions, let accessToken): 
      print("Logged in!") 
     } 
    } 
} 
+0

您是否編寫了在AppDelegate中返回應用程序的方法? –

回答

0

據我所知,你做的一切都是據此來facebook guide?

包括此部分:

// Handle clicks on the button 
    myLoginButton.addTarget(self, action: @selector(self.loginButtonClicked) forControlEvents: .TouchUpInside) 

而且

// Once the button is clicked, show the login dialog 
@objc func loginButtonClicked() { 
    let loginManager = LoginManager() 
    ... 
    } 
+0

我也試過這個,用這個塊更新問題。 –

0

按照理解你的問題,你似乎寫一個方法轉換爲appdelegate。這將在成功登錄後返回到您的應用程序。

import FacebookCore // (FBSDKCore's alternative for swift) 

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 
    let isFBOpenUrl = SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) 

    if isFBOpenUrl { return true } 
    return false 
} 

SWIFT 4:

func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey:Any] = [:]) -> Bool { 
     return SDKApplicationDelegate.shared.application(application, open: url, options: options) 
    } 

希望這件事會幫助你。