facebookSDK,Swift3和AppDelegate处理登录

时间:2016-11-10 05:59:02

标签: ios swift facebook

关注docs here

我已经很好地实现了登录按钮,应用程序传递到fblogin完成正常,但是在我登录后我会传回给应用程序并且控制台总是读取

"用户取消登录"

网上的一些搜索显示我需要向AppDelegate.Swift添加内容,但是文档只有Objective-C项目的代码。

this guy's guide但我认为它是为swift 1.x

而写的

使用以下方式稍微改变了NSURL的使用情况:

import UIKit
import FBSDKCoreKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    func application(application: UIApplication,
                     open url: URL,
                     sourceApplication: String?,
                     annotation: AnyObject?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(
            application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)
    }
    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 invalidate graphics rendering callbacks. 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 active 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:.
    }


}

但是我收到错误的应用程序(:openURL:sourceApplication:annotation :)'已重命名为' application(:open:sourceApplication:annotation :)'

任何人都有任何想法如何让这个代表团工作并从Facebook登录获得正确的东西?

1 个答案:

答案 0 :(得分:0)

您需要在appdelegate中进行更改,如下所示:

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(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}

使用FacebookCore.ApplicationDelegate(我希望它将被命名为FBApplicationDelegate),因为它遵循

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    return FacebookCore.ApplicationDelegate.shared.application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey] ?? [])
}

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return FacebookCore.ApplicationDelegate.shared.application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

请参阅更多链接:https://github.com/facebook/facebook-sdk-swift/issues/35