1)我正在尝试访问twitter个人资料照片并将其上传到解析用户对象
2)下面是我正在使用的代码。
if PFTwitterUtils.isLinkedWithUser(user){
//copy data to parse user
let screenName = PFTwitterUtils.twitter()?.screenName!
let requestString = ("https://api.twitter.com/1.1/users/show.json?screen_name=" + screenName!)
let verify: NSURL = NSURL(string: requestString)!
let request: NSMutableURLRequest = NSMutableURLRequest(URL: verify)
PFTwitterUtils.twitter()?.signRequest(request)
var response: NSURLResponse?
var error: NSError?
let data: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)!
if error == nil {
let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)
//let names: String! = result?.objectForKey("name") as! String
//let separatedNames: [String] = names.componentsSeparatedByString(" ")
//var firstName = separatedNames.first!
//var lastName = separatedNames.last!
let urlString = result?.objectForKey("profile_image_url_https") as! String
let hiResUrlString = urlString.stringByReplacingOccurrencesOfString("_normal", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
let twitterPhotoUrl = NSURL(string: hiResUrlString)
let imageData = NSData(contentsOfURL: twitterPhotoUrl!)
if(imageData != nil) {
let profileFileObject = PFFile(data:imageData!)
user.setObject(profileFileObject, forKey: "profilePicture")
}
user.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in
if(success) {
//println("User details are now updated")
user.pinInBackgroundWithBlock({ (pinUserSuccess:Bool, pinUserError:NSError?) -> Void in
if (pinUserSuccess){
println("User successfully pinned in twitter")
}else {
println("Error in pining the user")
}
})
}
})
} else {
println(error?.description)
}
3)我正在使用Parse用户界面使用twitter登录。那很有效。我可以访问屏幕名称
4)我在模拟器上测试它
问题1 -
在上面的代码中,我收到以下代码的错误。请帮忙!
let data: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)!
错误域= NSURLErrorDomain代码= -1012 \"无法完成操作。 (NSURLErrorDomain错误-1012。)\" UserInfo = 0x7fd11aab87a0 {NSErrorFailingURLStringKey = https://api.twitter.com/1.1/users/show.json?screen_name=jayskapadia,NSUnderlyingError = 0x7fd11aab51b0 \"操作无法完成。 (kCFErrorDomainCFNetwork错误-1012。)\",NSErrorFailingURLKey = https://api.twitter.com/1.1/users/show.json?screen_name=jayskapadia}
问题2 - 我还想从twitter个人资料中访问电子邮件ID。我该怎么做?
答案 0 :(得分:1)
照片部分现在可以使用以下代码
if PFTwitterUtils.isLinkedWithUser(user){
//copy data to parse user.
let screenName = PFTwitterUtils.twitter()?.screenName!
let verify = NSURL(string: "https://api.twitter.com/1.1/account/verify_credentials.json")
var request = NSMutableURLRequest(URL: verify!)
PFTwitterUtils.twitter()!.signRequest(request)
var response: NSURLResponse?
var error: NSError?
var data:NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)!
if error == nil {
let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)
//let names: String! = result?.objectForKey("name") as! String
//let separatedNames: [String] = names.componentsSeparatedByString(" ")
//var firstName = separatedNames.first!
//var lastName = separatedNames.last!
let urlString = result?.objectForKey("profile_image_url_https") as! String
let hiResUrlString = urlString.stringByReplacingOccurrencesOfString("_normal", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
let twitterPhotoUrl = NSURL(string: hiResUrlString)
let imageData = NSData(contentsOfURL: twitterPhotoUrl!)
if (screenName != nil){
user.setObject(screenName!, forKey: "username")
}
if(imageData != nil) {
let profileFileObject = PFFile(data:imageData!)
user.setObject(profileFileObject, forKey: "profilePicture")
}
user.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in
if(success) {
//println("User details are now updated")
user.pinInBackgroundWithBlock({ (pinUserSuccess:Bool, pinUserError:NSError?) -> Void in
if (pinUserSuccess){
println("User successfully pinned in twitter")
}else {
println("Error in pining the user")
}
})
}
})
} else {
println(error?.description)
}
}