我会尝试像这样NSURLConnection
使用sendAsynchronousRequest
...
NSOperationQueue *myQueue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//do something
}];
当触发处理程序块时,您拥有自己的内容,这主要是不言自明的。
另一种选择是调用NSURLConnectionDataDelegate
. 当你调用你的 URL 时,它会触发一些方法来让你知道事情何时完成。
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//Fired on error
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
//Fired First
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//Fired Second
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//Fired Third
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//Fired Fourth
}
使用您可能想要利用的委托方法,didReceiveData
以便您的数据就在那里。祝你好运。