Skip to content

Instantly share code, notes, and snippets.

@stdrc
Last active August 29, 2015 14:25
Show Gist options
  • Save stdrc/48543ed23872fb9b6d7a to your computer and use it in GitHub Desktop.
Save stdrc/48543ed23872fb9b6d7a to your computer and use it in GitHub Desktop.
NSURLConnection 发送异步请求
// 代码如下:
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//异步请求完成后的操作
}];
// 如果要在 completionHandler 中更改界面, 需要用以下代码:
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
//更改界面的操作
}];
// 示例如下:
[acticityIndicator startAnimating];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[acticityIndicator stopAnimating];
}];
if (connectionError == nil) {
[data writeToFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"data.plist"]
atomically:YES];
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment