Swift - 立即解析 Facebook 登录默认为用户取消登录

Posted

技术标签:

【中文标题】Swift - 立即解析 Facebook 登录默认为用户取消登录【英文标题】:Swift - Parse Facebook Login Immediately Defaults to User Cancelled Login 【发布时间】:2015-03-11 02:48:03 【问题描述】:

我只是想检查以确保我的 Parse 和 Facebook SDK 设置对于 Facebook 注册和登录是正确的,但我在测试期间遇到了一个问题,我在视图中设置了 Parse 注册功能加载,在过去构建应用程序时,它让我进入了验证 Facebook 凭据页面,但我将这条线打印到用户已取消的控制台上,但我不确定为什么会发生这种情况。

打印的行 = "Uh oh. The user cancelled the Facebook login."

AppDelegate.swift:

//
//  AppDelegate.swift
// 
//
//  Created by on 3/10/15.
//  Copyright (c) 2015 . All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
        // Override point for customization after application launch.


        Parse.setApplicationId("xxxxx", clientKey:"xxxxx")
            PFFacebookUtils.initializeFacebook()


        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) 
        FBAppCall.handleDidBecomeActiveWithSession(PFFacebookUtils.session())
    


    func applicationWillTerminate(application: UIApplication) 
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    

    func application(application: UIApplication,
        openURL url: NSURL,
        sourceApplication: String?,
        annotation: AnyObject?) -> Bool 
            return FBAppCall.handleOpenURL(url, sourceApplication:sourceApplication,
                withSession:PFFacebookUtils.session())
    




LoginViewController.swift:

import UIKit

class LoginViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view.


        var permissions = ["public_profile","email","user_friends"]

        PFFacebookUtils.logInWithPermissions(permissions, 
            (user: PFUser!, error: NSError!) -> Void in
            if let user = user 
                if user.isNew 
                    println("User signed up and logged in through Facebook!")
                 else 
                    println("User logged in through Facebook!")
                
             else 
                println("Uh oh. The user cancelled the Facebook login.")
            
        )


    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    


    @IBAction func facebookLogin(sender: AnyObject) 




    


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    
    */


【问题讨论】:

【参考方案1】:

如果您正确设置框架并按照 Parse 的 Facebook 设置指南 (https://parse.com/docs/ios/guide#users-facebook-users) 的步骤操作,此代码将起作用。我注意到这与您的代码之间的两个主要区别是您的视图控制器中 PFFacebookUtils Login 函数的语法,并且您的应用程序委托末尾的 func 应用程序放在我的 func applicationWillTerminate 之前。

AppDelegate.swift

import UIKit
import CoreData
import Parse

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 

    //X's replace my actual Parse information

    Parse.setApplicationId("XXXXXXXXXXXXXXXXXXXXXX", clientKey: "XXXXXXXXXXXXXXXXXXXXXX")
    PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)

    PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions)

    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)



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) 

    FBSDKAppEvents.activateApp()


/*
func application(application: UIApplication, url: NSURL, sourceApplication: NSString, annotation: AnyObject) -> Bool 

    return FBSDKApplicationDelegate.sharedInstance().application(
        application,
        openURL: url,
        sourceApplication: sourceApplication as String,
        annotation: annotation)

*/

func application(application: UIApplication,
    openURL url: NSURL,
    sourceApplication: String?,
    annotation: AnyObject?) -> Bool 
        return FBSDKApplicationDelegate.sharedInstance().application(application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)


func applicationWillTerminate(application: UIApplication) 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    self.saveContext()

ViewController.swift

import UIKit

class ViewController: UIViewController 

override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


let permissions = [ "email","user_birthday", "public_profile", "user_friends"]

@IBAction func loginButtonPressed(sender: AnyObject) 
    PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) 
        (user: PFUser?, error: NSError?) -> Void in
        if let user = user 
            if user.isNew 
                println("User signed up and logged in through Facebook!")
             else 
                println("User logged in through Facebook!")
            
         else 
            println("Uh oh. The user cancelled the Facebook login.")
        
    



@IBAction 是一个简单的 UIButton,我放在 Main.storyboard 中空白视图控制器的中心

Objective-C 桥接头:

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <ParseFacebookUtilsV4/PFFacebookUtils.h>
#import <Parse/Parse.h>

希望这能解决您的问题!

【讨论】:

以上是关于Swift - 立即解析 Facebook 登录默认为用户取消登录的主要内容,如果未能解决你的问题,请参考以下文章

iOS [Swift]:解析 Facebook 登录“使用未解析的标识符‘权限’”

在 Swift 中链接/合并 Facebook 用户和 PFUser(解析)

Facebook 使用 Parse SDK 登录(iOS 9、Xcode 7.1、Swift 2.1)

使用 Swift 使用 Parse 存储和保存 Facebook 数据

swift 2 解析facebookSDK ios9

取消 Facebook 登录不显示 (Swift)