【问题标题】:Getting Twitter profile image with Parse in Swift 2在 Swift 2 中使用 Parse 获取 Twitter 个人资料图片
【发布时间】:2016-01-24 09:53:11
【问题描述】:

我正在尝试使用 swift 2 获取 Twitter 个人资料图片。

这个链接描述了它是如何为 swift 完成的: Getting Twitter profile image with Parse in Swift

但问题是 NSURLConnection 现在已弃用并由 NSURLSession 替换。 PFTwitterUtils.twitter()?.signRequest 需要 NSMutableURLRequest,但从提到的替换同步请求的方法中,我得到了 NSURLRequest。如何使代码工作?

我尝试编写此代码以使其正常工作,但被难住了:

     if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()!) {

        let screenName = PFTwitterUtils.twitter()?.screenName!


        let requestString = NSURL(string: "https://api.twitter.com/1.1/users/show.json?screen_name=" + screenName!)
        let request = NSURLRequest(URL: requestString!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)


        PFTwitterUtils.twitter()?.signRequest(request)

        var response: NSURLResponse?
        var error: NSError?



        let session = NSURLSession.sharedSession()

        session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
            print(data)
            print(response)
            print(error)
        }).resume()



        if error == nil {

            let result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)


            let names: String! = result?.objectForKey("name") as! String

            let separatedNames: [String] = names.componentsSeparatedByString(" ")

            //self.firstName = separatedNames.first!
            //self.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!)
            let twitterImage: UIImage! = UIImage(data:imageData!)
        }

NSURLConnection throws after updating to Swift 2.0

【问题讨论】:

    标签: ios swift twitter parse-platform


    【解决方案1】:

    根据您的代码,我能够使用以下方法检索 Twitter 图像:

    if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()!) {
    
        let screenName = PFTwitterUtils.twitter()?.screenName!
        let requestString = NSURL(string: "https://api.twitter.com/1.1/users/show.json?screen_name=" + screenName!)
        let request = NSMutableURLRequest(URL: requestString!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)
        PFTwitterUtils.twitter()?.signRequest(request)
        let session = NSURLSession.sharedSession()
    
        session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
            print(data)
            print(response)
            print(error)
    
            if error == nil {
                var result: AnyObject?
                do {
                    result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
                } catch let error2 as NSError? {
                    print("error 2 \(error2)")
                }
    
                let names: String! = result?.objectForKey("name") as! String
                let separatedNames: [String] = names.componentsSeparatedByString(" ")
    
                //self.firstName = separatedNames.first!
                //self.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!)
                let twitterImage: UIImage! = UIImage(data:imageData!)
            }
        }).resume()
    }
    

    我将结果处理滚动到数据任务中,并修复了 Swift 2 的 JSONObjectWithData:options: 调用。

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 2015-05-07
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      相关资源
      最近更新 更多