iOS 網(wǎng)絡(luò)訪問(wèn)
- 蘋(píng)果 API
- NSURLConnection
iOS 9.0 之后就會(huì)建議使用 NSURLSession"Use NSURLSession (see NSURLSession.h)")
- NSURLSession(iOS 7.0)
- CFNetwork(底層實(shí)現(xiàn))
- NSURLConnection
- 第三方框架
- AFNetworking
- ASI
這個(gè)可能很多人已經(jīng)不用了茎活,因?yàn)樽髡咭呀?jīng)不再更新了,但是 ASI 的下載還是很好用的琢唾,而且 ASI 是通過(guò) CFNetwork 實(shí)現(xiàn)的载荔,首先效率很高,而且底層本來(lái)就很穩(wěn)定采桃,不更新也是很正常的
NSURLConnection
NSURL *url = [NSURL NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 需要修改請(qǐng)求頭或者請(qǐng)求體的時(shí)候懒熙,需要的是 mutable URL request
NSMutableURLRequest *requestM = [[NSMutableURLRequest alloc] initWithURL:url];
[requestM setValue:@"iPhone AppleWebKit" forHTTPHeaderField:@"User_Agent"];
__weak typeof(self) weakSelf = self;
// requestM 打開(kāi)的才是移動(dòng)端的網(wǎng)頁(yè)
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError == nil && data.length > 0) {
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[weakSelf.webView loadHTMLString:html baseURL:url];
}
}];
代碼其實(shí)挺簡(jiǎn)單的,而且 NSURLConnection 已經(jīng) 9.0 棄用了普办,如果還有想要嘗試的童鞋還是可以看看的工扎,畢竟是蘋(píng)果最早處理網(wǎng)絡(luò)訪問(wèn)的方法。
一般 connection 都會(huì)異步發(fā)送請(qǐng)求衔蹲,但是也有同步的時(shí)候肢娘,比如斷點(diǎn)續(xù)傳,下載文件之前需要知道上次下載文件大小舆驶,就可以同步發(fā)送請(qǐng)求橱健,而且可以發(fā)送 HEAD 請(qǐng)求。
HEAD 請(qǐng)求
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/videos.zip"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// HEAD 請(qǐng)求不會(huì)返回響應(yīng)體贞远,只是返回響應(yīng)頭畴博,可以查看數(shù)據(jù)大小
request.HTTPMethod = @"HEAD";
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
NSLog(@"HEAD response head -- %zd", response.expectedContentLength);
NSLog(@"HEAD response body -- %zd", data.length);
P.S.(Postscript)
GET 請(qǐng)求
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/login.php?username=username+&password=pwd"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError == nil && data.length > 0) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@", dict);
} else {
NSLog(@"%@", connectionError);
}
}];
POST 請(qǐng)求
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/login.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 默認(rèn)是通過(guò) GET 發(fā)送請(qǐng)求
request.HTTPMethod = @"POST";
NSString *requestBodyString = @"username=username&password=pwd";
NSData *requestBody = [requestBodyString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = requestBody;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError == nil && data.length > 0) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@", dict);
} else {
NSLog(@"%@", connectionError);
}
}];