取消Facebook登录未显示(Swift)

时间:2015-03-23 04:58:30

标签: facebook swift

我已经包含了在我的游戏中登录Facebook的选项,它运行正常,如下所示。但是,如果用户不想登录或创建新的Facebook帐户而只是想玩Facebook免费游戏,用户如何取消?

Facebook login

如果点击上面的按钮,我会看到一个屏幕,要求我登录或创建一个新帐户。没有选择在任何地方取消。

我发现this answer这个问题,但作为一个直接从Swift开始的新手,我不知道如何在那里找到答案。

我在我的GameViewController.swift文件中加载了我的按钮,就像这样

override func viewDidLoad() {
        super.viewDidLoad()
        self.authenticateLocalPlayer()
        self.fbLoginView.delegate = self
        self.fbLoginView.readPermissions = ["public_profile", "email", "user_friends"]}

在我的AppDelegate.swift文件中有以下功能

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        FBLoginView.self
        FBProfilePictureView.self
        return true
    }

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: NSString?, annotation: AnyObject) -> Bool {
        var wasHandled:Bool = FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication)
        return wasHandled
    }

我应该在这里调整一些东西吗?什么?

1 个答案:

答案 0 :(得分:0)

这个主题帮助了我Swift Facebook Login

我将这里提到的代码的这部分添加到我的GameViewController.swift文件

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Whenever a person opens the app, check for a cached session
    if FBSession.activeSession().state == FBSessionState.CreatedTokenLoaded {
        // If there's one, just open the session silently, without showing the user the login UI
        FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: false, completionHandler: {
            (session, state, error) -> Void in
            self.sessionStateChanged(session, state: state, error: error)
        })
    }
    return true
}

func sessionStateChanged(session : FBSession, state : FBSessionState, error : NSError?) {
// If the session was opened successfully
    if  state == FBSessionState.Open {
    println("Session Opened")
    }
// If the session closed
    if state == FBSessionState.Closed {
    println("Closed")
    }
}

此代码显示Facebook登录视图,显示取消按钮,与我之前的按钮不同。

然后我编辑了我的handleError委托函数,如此

func loginView(loginView : FBLoginView!, handleError:NSError) {
    println("Error: \(handleError.localizedDescription)")
    loginView.removeFromSuperview()
}

所以现在当用户点击取消键时,整个Facebook登录信息就会消失。