facebook解析报名迅速 [英] facebook parse sign up swift

查看:88
本文介绍了facebook解析报名迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力在swift中创建一个facebook登录/注册功能。我一直在寻找一个教程,但一直找不到任何东西。所以我一直在尝试自己做。它似乎工作,但为什么不在数据库中保存我的facebookName,性别和图像?它将它保存在模拟器中但不是在我使用我的ios 8设备时?

I've been struggling to create a facebook login/signup function in swift. i've been searching for a tutorial, but have not been able to find anything. So i've been trying to do it myself. It seem to work, but why is it not saving my facebookName, Gender and image in the database? It's saving it in the simulator but not when i'm using my ios 8 device?

我收到此日志消息用户注册并通过Facebook登录! ,并且新的用户被添加到解析类,但没有添加到名称,图像和性别......

i'm receiving this log message "User signed up and logged in through Facebook!", and a new user is added to the parse class, but not the name, image and gender...

@IBAction func login(sender: UIButton!) {




    var permissionArray = ["user_about_me", "user_relationships", "user_birthday", "user_location"]

    PFFacebookUtils.initializeFacebook()

    PFFacebookUtils.logInWithPermissions(permissionArray, block:  { (user: PFUser!, error: NSError!) in
        println(user)
        if user == nil {
            println(error.localizedDescription)


        } else {



            if user.isNew {



                var userQuery = PFUser.query()
                userQuery.getObjectInBackgroundWithId(PFUser.currentUser().objectId) {
                    (userObject: PFObject!, error: NSError!) -> Void in


                    var fbRequest = FBRequest.requestForMe()
                    fbRequest.startWithCompletionHandler { (connection: FBRequestConnection!, result:AnyObject!, error: NSError!) in


                        if error == nil {

                            //FACEBOOK DATA IN DICTIONARY
                            var userData = result as NSDictionary
                            var faceBookId = userData.objectForKey("id") as NSString
                            var faceBookName = userData.objectForKey("first_name") as NSString
                            var faceBookMiddle = userData.objectForKey("middle_name") as NSString
                            var faceBookGender = userData.objectForKey("gender") as NSString

                            var url:NSURL = NSURL.URLWithString(NSString(format:"https://graph.facebook.com/%@/picture?width=320", faceBookId))
                            var err: NSError?
                            var imageData :NSData = NSData.dataWithContentsOfURL(url, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)

                            var imageFile = PFFile(name: "image.jpg", data: imageData) as PFFile

                            println(userData)

                            userObject.setObject(faceBookName, forKey: "name")
                            userObject.setObject(imageFile, forKey: "file")
                            userObject.setObject(faceBookGender, forKey: "gender")

                            userObject.saveInBackground()



                            var sharedInstance:userSingleton = userSingleton.sharedInstance

                            sharedInstance.userName = (userObject.objectForKey("name") as NSString)
                            sharedInstance.userGender = (userObject.objectForKey("gender") as NSString)


                            (userObject.objectForKey("file") as PFFile).getDataInBackgroundWithBlock {
                                (theImageData: NSData!, error: NSError!) -> Void in

                                println(error)
                                if error == nil {

                                    sharedInstance.userImage = UIImage(data:theImageData)
                                }
                                self.performSegueWithIdentifier("LoginSegue", sender: self)
                            }


                        }
                    }
                }





                println("User signed up and logged in through Facebook!")
            } else {



                println("User logged in through Facebook!")
            }


        }

    })

}

}


推荐答案

假设你使用Parse,这是我是怎么做的。就个人而言,我创建了一个类 Utils.swift ,我把所有我想要重用的东西(或者我不希望在我的ViewControllers中使用):

Assuming that you use Parse, here is how I do it. Personally I create a class Utils.swift where I put all the stuff that I want to reuse (or that I don't want to have in my ViewControllers):

class Utils {

    class func notLoggedIn() -> Bool {
        let user = PFUser.currentUser()
        // here I assume that a user must be linked to Facebook
        return user == nil || !PFFacebookUtils.isLinkedWithUser(user)
    }
    class func loggedIn() -> Bool {
        return !notLoggedIn()
    } 


    class func logInWithFacebook() {
        PFFacebookUtils.logInWithPermissions(["public_profile", "user_friends"]) {
            (user: PFUser!, error: NSError!) -> Void in
            if user == nil {
                NSLog("The user cancelled the Facebook login (user is nil)")
            } else {
                NSLog("The user successfully logged in with Facebook (user is NOT nil)")
                // HERE I SET A USER POINTER TO THE INSTALLATION
                // That way we can send push notifications to specific users
                if let installation = PFInstallation.currentInstallation() {
                    installation["user"] = PFUser.currentUser()
                    installation.saveEventually()
                }
                // THEN I GET THE USERNAME AND fbId
                Utils.obtainUserNameAndFbId()
            }
        }
    }


    class func obtainUserNameAndFbId() {
        if notLoggedIn() {
            return
        }
        let user = PFUser.currentUser() // Won't be nil because is logged in
        // RETURN IF WE ALREADY HAVE A USERNAME AND FBID
        // Note that we check the fbId because Parse automatically fills in the username with random numbers
        if let fbId = user["fbId"] as? String {
            if !fbId.isEmpty {
                println("we already have a username and fbId -> return")
                return
            }
        }
        // REQUEST TO FACEBOOK
        println("performing request to FB for username and IDF...")
        if let session = PFFacebookUtils.session() {
            if session.isOpen {
                println("session is open")
                FBRequestConnection.startForMeWithCompletionHandler({ (connection: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
                    println("done me request")
                    if error != nil {
                        println("facebook me request - error is not nil :(")
                    } else {
                        println("facebook me request - error is nil :)")
                        println(result)
                        // You have 2 ways to access the result:
                        // 1)
                        println(result["name"])
                        println(result["id"])
                        // 2)
                        println(result.name)
                        println(result.objectID)
                        // Save to Parse:
                        PFUser.currentUser().username = result.name
                        PFUser.currentUser().setValue(result.objectID, forKey: "fbId")
                        PFUser.currentUser().saveEventually() // Always use saveEventually if you want to be sure that the save will succeed
                    }
                })
            }
        }
    }

}

然后,当您想要执行登录时,您只需调用 Utils.logInWithFacebook()

Then you can simply call Utils.logInWithFacebook() when you want to perform the login.

请注意,因为facebook我的请求可能会失败,所以不能保证你会成功将用户名和Facebook ID保存到Parse。这就是为什么我创建方法 Utils.obtainUserNameAndFbId(),我在应用程序中调用(_:didFinishLaunchingWithOptions)(它可以在每次启动时调用,因为它只会在FB成功之前执行请求。)

Note that, because the facebook me request can fail, it's not guaranteed that you will successfully save the username and facebook ID to Parse. That's why I create the method Utils.obtainUserNameAndFbId(), which I call in application(_: didFinishLaunchingWithOptions) (it can be called every launch because it will only perform the request to FB until it succeeds).

这篇关于facebook解析报名迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆