Swift和Parse(和xCode)问题 [英] Swift and Parse (and xCode) problems

查看:113
本文介绍了Swift和Parse(和xCode)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Parse和Swift遇到麻烦。
我试图用Parse class PFFacebookUtils使用Facebook登录。
这是代码:

  //登录或登录FACEBOOK 


@IBAction func FacebookLogin(){
PFFacebookUtils.logInWithPermissions(permissions,{(user:PFUser !, error:NSError!) - > Void in
如果错误== nil {
如果用户== nil {
NSLog(呃哦,用户取消了Facebook登录)
让userCancelledAlert = UIAlertController(标题:错误,消息:你取消了Facebook登录 ,preferredStyle:.Alert)
let okButton = UIAlertAction(title:OK,style:.Default,handler:nil)
userCancelledAlert.addAction(okButton)
let cancelButton = UIAlertAction :Cancel,style:.Default,handler:nil)
userCancelledAlert.addAction(cancelButton)
self.presentViewController(userCancelledAlert,animated:true,completion:nil)
} else如果用户。就是新的{
NSLog(用户注册并通过Facebook登录!)
FBRequestConnection.startWithGraphPath(me?fields = email,completionHandler:{(connection,result,error))>如果错误== Nil {
let fbGraphicObject = result as FBGraphObject
println(fbGraphicObject)
}
})
} else {
NSLog(用户通过Facebook登录!)
dispatch_async(dispatch_get_main_queue(),{() - > Void in
var homeViewController = self.storyboard?.instantiateViewControllerWithIdentifier(homePage)as HomeViewController
self.presentViewController(homeViewController,animated:true,completion:nil)
})
// end login(success)
}
} else {//如果登录不工作
println(错误!)
/ *
让facebookError = UIAlertController(标题:错误,消息:不连接到Facebook,检查你的连接,preferredStyle:。警报)
let okButton = UIAlertAction(title:OK,style:.Default,handler:nil)
facebookError.addAction(okButton)
let cancelButton = UIAlertAction(title:Cancel,style :.Default,handler:nil)
facebookError.addAction(cancelButton)
self.presentViewController(facebookError,animated:true,completion:nil)
* /
}
})
}

所以现在我的问题是:为什么我不能看到什么是我注册时的权限?
我的意思是,权限是一个数组。

  let permissions = [email,user_birthday] 

但是,当我注册的消息是[APP-NAME]想要访问您的基本信息和朋友列表
为什么不是电子邮件或user_birthday
那么,我如何将这些信息存储在我的Parse数据库中?我的意思是我看到id,用户名和密码(显然,我没有),authData等
我有电子邮件字段,但它是空的
我猜这个代码有什么问题,任何人都可以找到?
最后,不知道为什么,但xCode解析类的自动完成(我使用Swift)不起作用,我导入了框架(我添加了桥接头),并在几天前工作。

解决方案

我正在做同样的事情,试图让Parse存储Facebook电子邮件作为用户名,而不是自动生成的ID。由于Facebook将Graph API更新为v2.0,似乎喜欢那里不是很多,告诉你如何做到这一点。这就是我所做的:



首先,我设置了从Facebook获取电子邮件的正确权限。这是在didLoginUser方法中完成的。

  func logInViewController(logInController:PFLogInViewController,didLogInUser user:PFUser){
if!PFFacebookUtils.isLinkedWithUser(user){
let facebookLinkSuccess :(成功:Bool,错误:NSError?) - >


PFFacebookUtils.linkUserInBackground(user, withReadPermissions:[public_profile,email,user_friends,user_birthday,user_location],block:facebookLinkSuccess)
} ...

其次,我发现这个stackoverflow可以帮助您调用FB Graph API,然后使用所需的值更新Parse PFUser对象: Facebook iOS SDK& Swift - 如何创建依赖批次请求?



我设置了如下呼叫:

  if(FBSDKAccessToken.currentAccessToken()!= nil){
var userProfileRequestParams = [fields:id,name,email]
let userProfileRequest = FBSDKGraphRequest(graphPath:me,参数:userProfileRequestParams)
let graphConnection = FBSDKGraphRequestConnection()
graphConnection.addRequest(userProfileRequest,completionHandler:{(connection:FBSDKGraphRequestConnection !, result:AnyObject !, error:NSError! ) - > Void in
if(error!= nil){
println(error)
}
else {
let fbEmail = result.objectForKey(email )as!String
let fbUserId = result.objectForKey(id)as!String
if(fbEmail!=){
PFUser.cu rrentUser()?. username = fbEmail
PFUser.currentUser()?。saveEventually(nil)
}
println(Email:\(fbEmail))
println FBUserId:\(fbUserId))
}
})
graphConnection.start()
}

我把这段代码放在
func logInViewController(logInController:PFLogInViewController,didLogInUser user:PFUser){...



如果您对该部分有疑问,请查看此视频,它有一个类似的想法,但是使用Twitter。 https://www.youtube.com/watch?v=lnf7KHHeiO0



希望这有助于某人。我刚刚花了将近5个小时。


Get some troubles with Parse and Swift. I'm trying to use Facebook Login with Parse class PFFacebookUtils. That's the code:

// LOG IN or SIGN UP with FACEBOOK


@IBAction func FacebookLogin() {
    PFFacebookUtils.logInWithPermissions(permissions, {(user: PFUser!, error: NSError!) -> Void in
        if error == nil {
            if user == nil {
                NSLog("Uh oh. The user cancelled the Facebook login.")
                let userCancelledAlert = UIAlertController(title: "Error", message: "You cancelled the Facebook Login", preferredStyle: .Alert)
                let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
                userCancelledAlert.addAction(okButton)
                let cancelButton = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
                userCancelledAlert.addAction(cancelButton)
                self.presentViewController(userCancelledAlert, animated: true, completion: nil)            
            } else if user.isNew {
                NSLog("User signed up and logged in through Facebook!")
                FBRequestConnection.startWithGraphPath("me?fields=email", completionHandler: { (connection, result, error) -> Void in
                    if error == nil {
                        let fbGraphicObject = result as FBGraphObject
                        println(fbGraphicObject)
                    }
                })
            } else {
                NSLog("User logged in through Facebook!")            
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    var homeViewController = self.storyboard?.instantiateViewControllerWithIdentifier("homePage") as HomeViewController
                    self.presentViewController(homeViewController, animated: true, completion: nil)
                })
            // end login (success)
            }
        } else {  // if login doesnt work
            println(error!)
            /*
            let facebookError = UIAlertController(title: "Error", message: "Cant connect to Facebook, check your connectivity", preferredStyle: .Alert)
            let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            facebookError.addAction(okButton)
            let cancelButton = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
            facebookError.addAction(cancelButton)
            self.presentViewController(facebookError, animated: true, completion: nil)
            */
        }
    })
}

So now my question is: why can't i see what are the permissions when i sign up? I mean, permissions is an array.

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

But when i sign up the message is something like "[APP-NAME] wants to access to your basic info and list of friend. Why not the email or the user_birthday. Then, how can i store those infos in my Parse database? I mean, I see id, username, password (obviously, i have not), authData etc. I have the email field, but it's empty. I guess there's something wrong in the code. Anyone can find? Finally, don't know why, but xCode autocompletion for Parse class (I'm using Swift) doesn't work. I imported the framework (i added the bridging header), and it worked some days ago.

解决方案

I was working on this same thing, trying to get Parse to store the Facebook email as the username instead of the auto generated id. Since Facebook updated the Graph API to v2.0, it seems like there isn't much out there to tell you how to do it. This is what I did:

First, I set up the proper permissions to retrieve the email from Facebook. This is done within the "didLoginUser" method.

func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) {
    if !PFFacebookUtils.isLinkedWithUser(user) {
        let facebookLinkSuccess : (succeeded: Bool, error: NSError?) -> Void = { succeeded, error in
            if(succeeded){
                println("PF User linked with facebook")
            }
        }
        PFFacebookUtils.linkUserInBackground(user, withReadPermissions: ["public_profile", "email", "user_friends","user_birthday","user_location"], block: facebookLinkSuccess)
    } ...

Second, I found this stackoverflow, which helps you make the FB Graph API call, and then update the Parse PFUser object with the desired values: Facebook iOS SDK & Swift - How do I create dependent batch requests?

I set up the call as follows:

        if (FBSDKAccessToken.currentAccessToken() != nil){
            var userProfileRequestParams = [ "fields" : "id, name, email"]
            let userProfileRequest = FBSDKGraphRequest(graphPath: "me", parameters: userProfileRequestParams)
            let graphConnection = FBSDKGraphRequestConnection()
            graphConnection.addRequest(userProfileRequest, completionHandler: { (connection: FBSDKGraphRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
                if(error != nil){
                    println(error)
                }
                else {
                    let fbEmail = result.objectForKey("email") as! String
                    let fbUserId = result.objectForKey("id") as! String
                    if(fbEmail != "") {
                        PFUser.currentUser()?.username = fbEmail
                        PFUser.currentUser()?.saveEventually(nil)
                    }
                    println("Email: \(fbEmail)")
                    println("FBUserId: \(fbUserId)")
                }
            })
            graphConnection.start()
        }

I put this code in func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) { ...

If you have questions about that part, check out this video, it has a similar idea, but with Twitter. https://www.youtube.com/watch?v=lnf7KHHeiO0

Hope this helps somebody. I just spent almost 5 hours on this.

这篇关于Swift和Parse(和xCode)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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