如何使用NSURLSession正确发送异步请求?

时间:2015-09-17 06:35:28

标签: ios swift parse-platform nsurlconnection

由于NSURLConnection已在iOS 9中弃用,我尝试将我的代码覆盖到NSURLSession但没有成功。这是我的旧代码:

class Book: NSObject {

    var pfBook : PFObject
    var coverImage : UIImage!

    init(pfBook: PFObject) {
        self.pfBook = pfBook
    }

    func fetchCoverImage(completion: (image: UIImage?, error: NSError?) -> Void) {
        let urlString = self.pfBook["s3thumbnailUrl"] as! String
        let url = NSURL(string: urlString)
        let request = NSURLRequest(URL: url!)
        let queue = dispatch_get_main_queue()

        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
            if error == nil {
                dispatch_async(dispatch_get_main_queue()){
                self.coverImage = UIImage(data: data!)
                completion(image: self.coverImage, error: nil)
                }
            } else {
                completion(image: nil, error: error)
            }
        }
    }
}

这是我尝试过的新NSURLSession:

class Book: NSObject {

    var pfBook : PFObject
    var coverImage : UIImage!

    init(pfBook: PFObject) {
        self.pfBook = pfBook
    }

    func fetchCoverImage(completion: (image: UIImage?, error: NSError?) -> Void) {
        let urlString = self.pfBook["s3thumbnailUrl"] as! String
        let url = NSURL(string: urlString)
        let request = NSURLRequest(URL: url!)
        let session = NSURLSession.sharedSession()

        let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            if error == nil {
                dispatch_async(dispatch_get_main_queue()){
                    self.coverImage = UIImage(data: data!)
                    completion(image: self.coverImage, error: nil)
                }
            }else {
                completion(image: nil, error: error)
            }

        })

        task.resume()
    }
}

当我运行应用程序时,我的调试器只是喷出错误,如下所示:

`2015-09-17 03:05:15.270 MyApp[562:59901] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
2015-09-17 03:05:15.331 My App[562:59936] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.
 Stack:(
    0   CoreFoundation                      0x0000000183da4f74 <redacted> + 148
    1   libobjc.A.dylib                     0x0000000198997f80 objc_exception_throw + 56
    2   CoreFoundation                      0x0000000183da4ea4 <redacted> + 0
    3   Foundation                          0x0000000184dbe5d8 <redacted> + 88
    4   Foundation                          0x0000000184c40a1c <redacted> + 36
    5   UIKit                               0x00000001893ff958 <redacted> + 64
    6   UIKit                               0x00000001893ffb9c <redacted> + 548
    7   UIKit                               0x00000001893ff968 <redacted> + 80
    8   UIKit                               0x00000001892f62d8 <redacted> + 240
    9   UIKit                               0x000000018943f35c <redacted> + 640
    10  UIKit                               0x00000001893fc79c <redacted> + 144
    11  UIKit                               0x00000001893fc79c <redacted> + 144
    12  UIKit                               0x00000001893fc6d4 <redacted> + 68
    13  UIKit                               0x00000001893fc228 <redacted> + 120
    14  UIKit                               0x00000001893fc194 <redacted> + 280
    15  UIKit                               0x0000000189400d68 <redacted> + 1048
    16  UIKit                               0x00000001893fcc3c <redacted> + 244
    17  UIKit                               0x0000000189b25e8c <redacted> + 460
    18  UIKit                               0x0000000189b260f4 <redacted> + 200
    19  UIKit                               0x0000000189b25dbc <redacted> + 252
    20  UIKit                               0x0000000189b260f4 <redacted> + 200
    21  UIKit                               0x0000000189b25dbc <redacted> + 252
    22  UIKit                               0x00000001893ff968 <redacted> + 80
    23  UIKit                               0x0000000189b260c8 <redacted> + 156
    24  UIKit                               0x000000018940083c <redacted> + 100
    25  UIKit                               0x00000001893ff968 <redacted> + 80
    26  UIKit                               0x0000000189400470 <redacted> + 244
    27  UIKit                               0x0000000189b2397c <redacted> + 352
    28  UIKit                               0x0000000189af0f24 <redacted> + 200
    29  UIKit                               0x0000000189aefdec <redacted> + 72
    30  UIKit                               0x0000000189aefd88 <redacted> + 48
    31  UIKit                               0x0000000189ad6cf4 <redacted> + 104
    32  UIKit                               0x0000000189ad7264 <redacted> + 1108
    33  UIKit                               0x0000000189ae26f4 <redacted> + 436
    34  UIKit                               0x00000001894dd6f8 <redacted> + 4108
    35  UIKit                               0x0000000189ae4128 <redacted> + 132
    36  UIKit                               0x0000000189309dc0 <redacted> + 80
    37  UIKit                               0x0000000189ae3830 <redacted> + 2656
    38  UIKit                               0x0000000189ae1af8 <redacted> + 12448
    39  UIKit                               0x00000001894d87d8 <redacted> + 364
    40  MyApp                   0x0000000100101934 _TFFC17MyApp29ThirdCollectionViewController14collectionViewFS0_FTCSo16UICollectionView22cellForItemAtIndexPathCSo11NSIndexPath_CSo20UICollectionViewCellU_FTGSqCSo7UIImage_GSqCSo7NSError__T_ + 316
    41  MyApp                   0x00000001000b25fc`
`_TFFC17MyApp4Book15fetchCoverImageFS0_FFT5imageGSqCSo7UIImage_5errorGSqCSo7NSError__T_T_U_FTGSqCSo6NSData_GSqCSo13NSURLResponse_GSqS2___T_ + 580

    2015-09-17 03:05:15.353 MyApp[562:59936] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.

我应该调整什么?

4 个答案:

答案 0 :(得分:3)

首先,我想提一下几点,

在OS X 10.11和iOS 9.0中不推荐使用NSURLConnection,但是:

  • 不会消失,使用它的应用程序仍然可以使用

  • 新功能将添加到NSURLSession

  • watchOS不支持

<强>其次,

使用NSURLSession并使用NSURLConnection发送异步调用就像使用NSURLConnection一样简单。

这是发送异步请求的示例代码。希望它有所帮助。

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
          completionHandler:^(NSData *data,
                              NSURLResponse *response,
                              NSError *error) {
            // handle response

  }] resume]; 

dataTaskWithURL 旨在取代 NSURLConnection 中的 sendAsynchronousRequest

答案 1 :(得分:1)

使用swift3,

$block = $conn->prepare("SELECT `ip` FROM `firewall` WHERE `ip` = :ip");
$block->bindParam(':ip', $_SERVER['REMOTE_ADDR']);
$block->execute();
$block = $block->fetchAll(PDO::FETCH_ASSOC);
if (!empty($block)) {
    die("Error!");
}

答案 2 :(得分:0)

下面的功能对我来说很好。

func fetchCoverImage(完成:(图片:UIImage?,错误:NSError?) - &gt; Void){

    //        let url = NSURL(string: urlString)
    var request = NSMutableURLRequest(URL: NSURL(string: "http://www.fnordware.com/superpng/pnggrad16rgb.png")!)
    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        if error == nil {
            dispatch_async(dispatch_get_main_queue()){
                self.coverImage?.image = UIImage(data: data!)
                completion(image: self.coverImage?.image, error: nil)
            }
        }else {
            completion(image: nil, error: error)
        }

    })

    task.resume()
}   

}

如果您在使用此问题时遇到任何问题,请与我们联系。我只是将网址更改为某个png图片。

答案 3 :(得分:0)

您的代码可能没有任何问题。如果您针对iOS 9 SDK进行编译,NSURLConnection将执行相同的操作。您正在运行App Transport Security限制。在这里阅读:

https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/

基本上,如果您想要访问没有强加密的网址,您必须修改您的Info.plist文件,告诉iOS让您这样做。