[iOS]NSURLConnection的一个小例子

直接上代码了。。。


在iOS7之后可以使用NSURLSession,但是考虑到兼顾iOS6还是使用NSURLConnection。



@interface ViewController() <NSURLConnectionDataDelegate>
@property (nonatomic,strong) NSMutableData *receivedData;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:10.0];

    NSURLConnection *theConncetion=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConncetion) {
        _receivedData = [NSMutableData data];
    }

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_receivedData appendData:data];
    NSLog(@"Downloading...");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Finished");
}

@end


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。