1
我想實現這個,但它不打印任何東西。logInWithReadPermissions(fromViewController)處理程序不叫
loginManager.logInWithReadPermissions(["email"], fromViewController: self.parentViewController) { (result, error) -> Void in
print("hi")
}
我可以在此方法調用之前和之後打印,但不能在處理程序中打印。在控制檯的唯一錯誤是:
-canOpenURL: failed for URL: "fbauth2:/"
其中,根據FB,可以忽略不計(https://developers.facebook.com/docs/ios/ios9)
這裏就是整個ViewController
:
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if FBSDKAccessToken.currentAccessToken() != nil {
// User already has access token
logUserData()
} else {
let loginButton = FBSDKLoginButton()
loginButton.center = view.center
loginButton.readPermissions = ["email", "posts"]
view.addSubview(loginButton)
loginButton.delegate = self
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - FBSDKLoginButtonDelegate methods
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
print("logged in")
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
print("logged out")
}
private func logUserData() {
let loginManager = FBSDKLoginManager()
loginManager.logInWithReadPermissions(["email"], fromViewController: self.parentViewController) { (result, error) -> Void in
print("hi")
}
}
}
編輯 以下是我添加到我的.plist中的內容:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb983308525071081</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>983308525071081</string>
<key>FacebookDisplayName</key>
<string>Demo</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>facebook.com</key>
<dict>
<key>NSIncludesSubdomains</key> <true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
</dict>
<key>fbcdn.net</key>
<dict>
<key>NSIncludesSubdomains</key> <true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
</dict>
<key>akamaihd.net</key>
<dict>
<key>NSIncludesSubdomains</key> <true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
</dict>
</dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
你指定下列鍵在你的.plist中:LSApplicationQueriesSchemes,FacebookAppID? – Terry
是的,請參閱上面的編輯 – Morgan