2016-09-27 45 views
0

我一直在使用Swift 3为iOS 10上的Facebook登录创建自定义按钮。不幸的是Facebook登录将不会显示SafariViewController,直到我按下UIButton第二次。由于断点,我知道它调用loginManager.logIn(),但它从来没有进入回调,直到第二次按下UIButton。虽然如果我使用FacebookLoginButton()和我的自定义一切正常工作。我真的不知道有什么问题。希望您能够帮助我。谢谢!Facebook登录iOS 10的作品,直到第二次按下按钮

这里是我的ViewController:

import UIKit 
import FacebookCore 
import FacebookLogin 

class ViewController: UIViewController { 

override func viewDidLoad() { 

    self.view.backgroundColor = .blue 

    // Add a custom login button to your app 
    let myLoginButton = UIButton(type: .system) 
    myLoginButton.backgroundColor = .red 
    myLoginButton.frame = CGRect(x: 0, y: 0, width: 180, height: 40) 
    myLoginButton.center = view.center; 
    myLoginButton.setTitle("My Button", for: .normal) 

    // Handle clicks on the button 
    myLoginButton.addTarget(self, action: #selector(self.loginButtonClicked), for: .touchUpInside) 

    // Add the button to the view 
    view.addSubview(myLoginButton) 
} 

// Once the button is clicked, show the login dialog 
func loginButtonClicked() { 
    let loginManager = LoginManager() 
    loginManager.logIn([.publicProfile], viewController: self) { (loginResult) in 
     switch loginResult { 
     case .failed(let error): 
      print(error) 
     case .cancelled: 
      print("User cancelled login.") 
     case .success(let grantedPermissions, let declinedPermissions, let accessToken): 
      print("Logged in!") 
     } 
    } 
} 

而我的info.plist(唯一的变化是我的应用程序的名称和标识的占位符:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
<key>CFBundleDevelopmentRegion</key> 
<string>en</string> 
<key>CFBundleExecutable</key> 
<string>$(EXECUTABLE_NAME)</string> 
<key>CFBundleIdentifier</key> 
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> 
<key>CFBundleInfoDictionaryVersion</key> 
<string>6.0</string> 
<key>CFBundleName</key> 
<string>$(PRODUCT_NAME)</string> 
<key>CFBundlePackageType</key> 
<string>APPL</string> 
<key>CFBundleShortVersionString</key> 
<string>1.0</string> 
<key>CFBundleVersion</key> 
<string>1</string> 
<key>LSRequiresIPhoneOS</key> 
<true/> 
<key>UILaunchStoryboardName</key> 
<string>LaunchScreen</string> 
<key>UIMainStoryboardFile</key> 
<string>Main</string> 
<key>UIRequiredDeviceCapabilities</key> 
<array> 
    <string>armv7</string> 
</array> 
<key>UISupportedInterfaceOrientations</key> 
<array> 
    <string>UIInterfaceOrientationPortrait</string> 
    <string>UIInterfaceOrientationLandscapeLeft</string> 
    <string>UIInterfaceOrientationLandscapeRight</string> 
</array> 
<key>CFBundleURLTypes</key> 
<array> 
    <dict> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>fb[A Number]</string> 
     </array> 
    </dict> 
</array> 
<key>FacebookAppID</key> 
<string>[My Facebook App ID]</string> 
<key>FacebookDisplayName</key> 
<string>[My App Name]</string> 
<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>fbapi</string> 
    <string>fb-messenger-api</string> 
    <string>fbauth2</string> 
    <string>fbshareextension</string> 
</array> 

更新:这有已被facebook确认为bug https://developers.facebook.com/bugs/622990254549819/is

+0

试试这个让loginView:FBSDKLoginManager = FBSDKLoginManager() loginView.loginBehavior = FBSDKLoginBehavior.Web –

+0

谢谢你,但它仍然需要两个按钮触摸才能打开webView。 – Lalo

回答

1

这个问题是关系到驻留在Facebook的SDK的方法

- (void)logInWithBehavior:(FBSDKLoginBehavior)loginBehavior 
{ 
    __weak __typeof__(self) weakSelf = self; 
    [FBSDKServerConfigurationManager loadServerConfigurationWithCompletionBlock:^(FBSDKServerConfiguration *serverConfiguration, NSError *loadError) { 
    [weakSelf logInWithBehavior:loginBehavior serverConfiguration:serverConfiguration serverConfigurationLoadError:loadError]; 
    }]; 
} 

你会得到没有登录的观点,因为,你的按钮回调包括LoginManager初始化时,FBSDKServerConfigurationManager是在第一时间按下空登录按钮,而是在第二次init。

我已经解决了一个非常类似的问题,只需将manager声明为全局变量并在viewDidLoad中创建LoginManager的init即可。

+0

我发现相同的问题和相同的解决方案。 – cybergen

+0

这是解决方案,但Facebook只是证实了我这是一个错误,他们会解决它。 https://developers.facebook.com/bugs/622990254549819/ – Lalo