将应用程序从Xcode 6.2重建为6.3.2
解析链接工作正常,并使用指南中的虚拟代码进行测试。
我认为问题出在授权请求中,必须改变Parse访问用户信息的方式,但无法解决问题。
更新提示1: 如果我注册,没问题,但如果我停止并重新打开应用程序,问题就出现了,因为 saveInBackgroundWithBlock 上的自动完成失败,可能会出现问题,但在哪里? 更新提示2: 我在Parse的网站上看到,"会话"每次登录时都会继续增长
得到了这个错误:
我请求授权的代码:
import UIKit
import Parse
class LoginViewController: UIViewController, FBLoginViewDelegate {
@IBOutlet weak var fbLoginView: FBLoginView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func pressedLoginFB(sender: UIButton) {
//MARK: facebook personlized login from 440
//to start, request permissions
PFFacebookUtils.logInWithPermissions(["public_profile", "user_about_me", "user_birthday"], block: {
user, error in
if user == nil
{
println("BAD! user cancelled login")
//add a uialertcontrolelr before pushing to app store
return
}
else if user!.isNew
{
println("GOOD! - User signed up with Facebook")
//now, ask facebook for user's data
FBRequestConnection.startWithGraphPath("/me?fields=picture,first_name,birthday", completionHandler: {
connection, result, error in
var resultDictionary = result as! NSDictionary
user!["firstName"] = resultDictionary["first_name"]
user!["gender"] = resultDictionary["gender"]
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/MM/YYYY"
user!["birthday"] = dateFormatter.dateFromString(resultDictionary["birthday"] as! String)
let pictureURL = ((resultDictionary["picture"] as! NSDictionary)["data"] as! NSDictionary) ["url"] as! String
let url = NSURL(string: pictureURL)
let request = NSURLRequest(URL: url!) //in old Xcode was an optional
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
let imageFile = PFFile(name: "avatar.jpg", data: data)
user!["picture"] = imageFile as PFFile //MARK: not sure about this downcast
user!.saveInBackgroundWithBlock({
success, error in
println("hello")
println(success)
println(error)
})
})
}
)
}
else
{
println("GOOD - User logged in with Facebook")
}
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("mapNavVCID") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
})
}
}
这是我的个人用户结构,用于解耦Parse:
import Foundation
import MapKit
import Parse
struct User {
let id: String
// let pictureUrl: String
let name: String
private let pfUser :PFUser //pfUser is private because I will use it only with our backend
//this is a nested function, I use this design pattern because of getDataInBackgroundWithBlock
func getPhoto(callback:(UIImage) -> ()) {
let imageFile = pfUser.objectForKey("picture") as! PFFile
imageFile.getDataInBackgroundWithBlock({
data, error in
if let data = data{
callback(UIImage(data: data)!)
}
})
}
}
private func pfUserToUser(user: PFUser) -> User {
return User(id: user.objectId!, name: user.objectForKey("firstName") as! String , pfUser: user)
// I cut off this:
// "pictureUrl: user.objectForKey("picture") as String," because now it is an image thanks to gePhoto function
}
//test on optionals "if user exists, give the value to the user constant,
//and return a User instance via pfUserToUser function"
// the "-> User?" is the reason why I have an optional in loginViewController on " currentUser()? "
func currentUser() -> User? {
if let user = PFUser.currentUser() {
return pfUserToUser(user)
}
else {
//if optional user doesn't exist, return nil
return nil
}
}
提前致谢