2016-11-05 104 views
0

我在我的ios應用程序中使用facebook登錄API(SDK version Optional("4.1.0")Swift。我在我的視圖控制器上放置了fb登錄按鈕,當用戶點擊它時 - 它會在safari中打開Facebook登錄面板。當用戶將憑據,並點擊登錄 - 他看到彈出消息有一個問題:「在APP_NAME中打開此頁面」,同時在Swift中使用facebook登錄api

Open this page in APP_NAME? 

Cancel    OPEN 

我想擺脫這一點,並直接打開應用程序。 我的代碼來處理按鈕的行爲是這樣的:

class ViewController: UIViewController, FBSDKLoginButtonDelegate { 
    @IBOutlet weak var fbLoginButton: FBSDKLoginButton! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    fbLoginButton.readPermissions = ["public_profile", "email"] 
    fbLoginButton.layer.cornerRadius = 5 
    fbLoginButton.delegate = self 
    self.fbLoginButton.delegate = self 
    FBSDKProfile.enableUpdates(onAccessTokenChange: true) 
} 

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    if (FBSDKAccessToken.current() != nil){ 

     let tokenAsAParam = [ 
      "access_token":FBSDKAccessToken.current().tokenString! 
     ] 

     let loginManager = FBSDKLoginManager() 

     FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in 
      if (error == nil){ 
       let data:[String:AnyObject] = result as! [String : AnyObject] 
       let defaults = UserDefaults.standard 
       let firstName:String = data["first_name"] as! String! 

       defaults.set(firstName, forKey: "facebook_display_name") 
       defaults.synchronize() 
      }else { 
       print("fb error") 
      } 
     }) 

     Alamofire.request("\(serverURL)/auth/facebook", method: .post, parameters: tokenAsAParam) 
      .validate() 
      .responseJSON { response in 

       switch(response.result) { 

       case .success: 
        self.performSegue(withIdentifier: "openMainApp", sender: self) 
        break 
       case .failure: 
        print(response.result.error) 
        loginManager.logOut() 
       break 
       } 
     } 
    } 

在上面的代碼,我獲取從Facebook用戶的數據併發送令牌到我的web服務,以驗證有用戶是否是真的還是假的並做一些後端的東西。

我Info.plist文件是:

<array> 
    <dict> 
     <key>CFBundleTypeRole</key> 
     <string>Editor</string> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>fbXXXXXXXXXX</string> 
     </array> 
    </dict> 
</array> 
<key>FacebookAppID</key> 
<string>2XXXXXXXXXXX</string> 
<key>FacebookDisplayName</key> 
<string>xxxxxxxx</string> 
<key>LSApplicationCategoryType</key> 
<string></string> 
<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>fbapi</string> 
    <string>fb-messenger-api</string> 
    <string>fbauth2</string> 
    <string>fbshareextension</string> 
</array> 

而且,在我的appDelegate我有以下幾種方法:

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

    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 
    print("SDK version \(FBSDKSettings .sdkVersion())") 

... 
    return true 
} 



func application(_ application: UIApplication, 
       open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 

    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) 
} 
// [END openurl] 

@available(iOS 9.0, *) 
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]) 
} 

但它仍然表明這種怪異的poppup而不是正常打開的應用程式。

有誰知道我能做些什麼來解決這個問題?

回答

相關問題