我將通過sendAsynchronousRequest
嘗試使用NSURLConnection
這樣的...
NSOperationQueue *myQueue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//do something
}];
大多數情況下不言自明的是,當處理程序塊被解僱時,你就擁有了你的內容。
另一種選擇是調用NSURLConnectionDataDelegate
。當你打電話給你的網址時,它會觸發一些方法讓你知道什麼時候完成。
- (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
,讓你有你的數據在那裏。祝你好運。
來源
2013-06-20 03:52:56
Dan