使用 Swift 登录 Facebook 后加载另一个 ViewController
Posted
技术标签:
【中文标题】使用 Swift 登录 Facebook 后加载另一个 ViewController【英文标题】:Load Another ViewController after Facebook Login with Swift 【发布时间】:2016-02-11 12:48:31 【问题描述】:我正在尝试在使用 swift 成功登录 Facebook 后加载另一个视图控制器。我一直在关注这篇博文:http://studyswift.blogspot.com.tr/2016/01/facebook-sdk-and-swift-create-facebook.html
我配置了一个名为“redirectAfterLogin”的segue,并尝试使用performSegueWithIdentifier
您可以在下面看到我的 ViewController.swift 和 AppDelegate.swift 文件。不知何故,我错过了一些东西。有什么建议吗?
ViewController.swift
import UIKit
import FBSDKLoginKit
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let loginButton = FBSDKLoginButton()
loginButton.center = view.center
view.addSubview(loginButton)
//self.performSegueWithIdentifier("redirectAfterLogin", sender: self)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
/*
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result:FBSDKLoginManagerLoginResult!, error: NSError!)
if error == nil
print("login complete")
self.performSegueWithIdentifier("redirectAfterLogin", sender: self)
else
print(error.localizedDescription)
*/
AppDelegate.swift
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
// Override point for customization after application launch.
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
func applicationWillResignActive(application: UIApplication)
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
func applicationDidEnterBackground(application: UIApplication)
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
func applicationWillEnterForeground(application: UIApplication)
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
func applicationDidBecomeActive(application: UIApplication)
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
FBSDKAppEvents.activateApp()
func applicationWillTerminate(application: UIApplication)
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
这是我登录模拟器后的输出:
2016-02-11 14:43:49.712 TMR-Prototype1[22260:3512428] -canOpenURL:URL 失败:“fbauth2:/” - 错误:“(null)”
2016-02-11 14:43:49.717 TMR-Prototype1[22260:3512428] -canOpenURL:URL 失败:“fbauth2:/” - 错误:“(null)”
这是屏幕截图。它卡在第一个视图控制器上:
我还将我的 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>CFBundleSignature</key>
<string>????</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>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<!-- following stuff added by me as for facebook login -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fbXXXYYYZZZ</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>XXXYYYZZZ</string>
<key>FacebookDisplayName</key>
<string>TMR</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>
</dict>
</plist>
【问题讨论】:
【参考方案1】:您应该将FBSDKLoginButtonDelegate
实现到您的ViewController
,并包含委托方法。
class ViewController: UIViewController, FBSDKLoginButtonDelegate
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let loginButton = FBSDKLoginButton()
loginButton.center = view.center
loginButton.delegate = self // Remember to set the delegate of the loginButton
view.addSubview(loginButton)
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
/*
Check for successful login and act accordingly.
Perform your segue to another UIViewController here.
*/
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
// Actions for when the user logged out goes here
【讨论】:
我好像忘记设置登录按钮的委托了。谢谢以上是关于使用 Swift 登录 Facebook 后加载另一个 ViewController的主要内容,如果未能解决你的问题,请参考以下文章
使用 Facebook 应用程序时 Facebook 登录回调不起作用 - Swift
如果用户已经使用 facebook 登录,则使用 swift 调用 segue