iOS9 http 网络请求
2015-10-15 00:54
567 查看
NSURLConnection算是iOS很早的网络请求组件了,用的可能也比较少了,应该算是初学者的入门课吧.不说没用的,直接上代码,注释中有写iOS9以后允许http访问的设置方法
下面的代码是直接从demo中复制粘贴过来的,就允许我偷点懒吧 =_=
//
// ViewController.m
// NSURLConection_text
//
// Created by 李睿 on 15/10/14.
// Copyright © 2015年李睿. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()<NSURLConnectionDataDelegate>
{
NSMutableData *_data;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//====================================================================
//iOS 9
以后把所有的HTTP请求都变成了HTTPS,导致数据加载不出来
//解决办法是:在plist文件中增加字段NSAppTransportSecurity:Dictionary
//再在新增字段下添加 -NSAllowsArbitraryLoads:Boolean YES
//====================================================================
//====================================================================
//NSURLConnection同步加载数据
//====================================================================
[selfsendSynchronousRequest];
//====================================================================
//NSURLConnection异步加载数据(一)使用代理方法
//====================================================================
[selfsendAsynchronousRequest];
//====================================================================
//NSURLConnection异步加载数据(二)使用Block方法
//====================================================================
[selfsendRequestAsyByBlock];
}
//====================================================================
//NSURLConnection同步网络请求
//====================================================================
- (void)sendSynchronousRequest {
NSURL *url = [NSURLURLWithString:@"http://img2.3lian.com/img2007/19/33/005.jpg"];
NSURLRequest *request = [[NSURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];
NSURLResponse *response =
nil;
NSError *error =
nil;
NSData *resultData = [NSURLConnectionsendSynchronousRequest:request
returningResponse:&response
error:&error];
NSString *dataString = [[NSStringalloc]initWithData:resultDataencoding:NSUTF8StringEncoding];
NSLog(@"%@",dataString);
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,
200, 200)];
UIImage *image = [UIImageimageWithData:resultData];
imageView.backgroundColor = [UIColorredColor];
imageView.image = image;
[self.viewaddSubview:imageView];
}
//====================================================================
//NSURLConnection异步加载数据(一)使用代理方法
//====================================================================
- (void)sendAsynchronousRequest {
_data = [[NSMutableDataalloc]init];
NSURL *url = [NSURLURLWithString:@"http://img2.3lian.com/img2007/19/33/005.jpg"];
NSMutableURLRequest *request = [[NSMutableURLRequestalloc]init];
request.URL = url;
request.cachePolicy =NSURLRequestUseProtocolCachePolicy;
request.timeoutInterval =
10;
NSURLResponse *response =
nil;
[NSURLConnectionconnectionWithRequest:request
delegate:self];
}
//====================================================================
//NSURLConnection异步加载数据(二)使用Block方法
//====================================================================
- (void)sendRequestAsyByBlock {
NSOperationQueue *operationQueue = [[NSOperationQueuealloc]init];
NSURL *url = [[NSURLalloc]initWithString:@"http://img2.3lian.com/img2007/19/33/005.jpg"];
NSMutableURLRequest *request = [[NSMutableURLRequestalloc]initWithURL:url];
request.timeoutInterval =
10;
request.cachePolicy =NSURLRequestUseProtocolCachePolicy;
[NSURLConnection
sendAsynchronousRequest:request queue:operationQueuecompletionHandler:^(NSURLResponse *_Nullable response,
NSData * _Nullable data,
NSError * _Nullable connectionError) {
NSLog(@"%@",data);
}];
}
#pragma - mark NSURLConnection异步请求时掉用的代理方法
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_data appendData:data];
} //接收数据时调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *dataString = [[NSStringalloc]initWithData:_dataencoding:NSUTF8StringEncoding];
NSLog(@"%@",dataString);
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,
200, 200)];
UIImage *image = [UIImageimageWithData:_data];
imageView.backgroundColor = [UIColorredColor];
imageView.image = image;
[self.viewaddSubview:imageView];
} //接收数据结束时调用
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
下面的代码是直接从demo中复制粘贴过来的,就允许我偷点懒吧 =_=
//
// ViewController.m
// NSURLConection_text
//
// Created by 李睿 on 15/10/14.
// Copyright © 2015年李睿. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()<NSURLConnectionDataDelegate>
{
NSMutableData *_data;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//====================================================================
//iOS 9
以后把所有的HTTP请求都变成了HTTPS,导致数据加载不出来
//解决办法是:在plist文件中增加字段NSAppTransportSecurity:Dictionary
//再在新增字段下添加 -NSAllowsArbitraryLoads:Boolean YES
//====================================================================
//====================================================================
//NSURLConnection同步加载数据
//====================================================================
[selfsendSynchronousRequest];
//====================================================================
//NSURLConnection异步加载数据(一)使用代理方法
//====================================================================
[selfsendAsynchronousRequest];
//====================================================================
//NSURLConnection异步加载数据(二)使用Block方法
//====================================================================
[selfsendRequestAsyByBlock];
}
//====================================================================
//NSURLConnection同步网络请求
//====================================================================
- (void)sendSynchronousRequest {
NSURL *url = [NSURLURLWithString:@"http://img2.3lian.com/img2007/19/33/005.jpg"];
NSURLRequest *request = [[NSURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];
NSURLResponse *response =
nil;
NSError *error =
nil;
NSData *resultData = [NSURLConnectionsendSynchronousRequest:request
returningResponse:&response
error:&error];
NSString *dataString = [[NSStringalloc]initWithData:resultDataencoding:NSUTF8StringEncoding];
NSLog(@"%@",dataString);
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,
200, 200)];
UIImage *image = [UIImageimageWithData:resultData];
imageView.backgroundColor = [UIColorredColor];
imageView.image = image;
[self.viewaddSubview:imageView];
}
//====================================================================
//NSURLConnection异步加载数据(一)使用代理方法
//====================================================================
- (void)sendAsynchronousRequest {
_data = [[NSMutableDataalloc]init];
NSURL *url = [NSURLURLWithString:@"http://img2.3lian.com/img2007/19/33/005.jpg"];
NSMutableURLRequest *request = [[NSMutableURLRequestalloc]init];
request.URL = url;
request.cachePolicy =NSURLRequestUseProtocolCachePolicy;
request.timeoutInterval =
10;
NSURLResponse *response =
nil;
[NSURLConnectionconnectionWithRequest:request
delegate:self];
}
//====================================================================
//NSURLConnection异步加载数据(二)使用Block方法
//====================================================================
- (void)sendRequestAsyByBlock {
NSOperationQueue *operationQueue = [[NSOperationQueuealloc]init];
NSURL *url = [[NSURLalloc]initWithString:@"http://img2.3lian.com/img2007/19/33/005.jpg"];
NSMutableURLRequest *request = [[NSMutableURLRequestalloc]initWithURL:url];
request.timeoutInterval =
10;
request.cachePolicy =NSURLRequestUseProtocolCachePolicy;
[NSURLConnection
sendAsynchronousRequest:request queue:operationQueuecompletionHandler:^(NSURLResponse *_Nullable response,
NSData * _Nullable data,
NSError * _Nullable connectionError) {
NSLog(@"%@",data);
}];
}
#pragma - mark NSURLConnection异步请求时掉用的代理方法
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_data appendData:data];
} //接收数据时调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *dataString = [[NSStringalloc]initWithData:_dataencoding:NSUTF8StringEncoding];
NSLog(@"%@",dataString);
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,
200, 200)];
UIImage *image = [UIImageimageWithData:_data];
imageView.backgroundColor = [UIColorredColor];
imageView.image = image;
[self.viewaddSubview:imageView];
} //接收数据结束时调用
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- iOS9网络请求失败问题
- Linux 命令 - ftp: 网络文件传输工具
- 关于java网络编程中 java.net.SocketException: Connection reset 异常的原因
- Java网络编程
- cadence设置不同网络颜色
- android之HttpURLConnection
- http上传
- 在android上监听网络状态的变更
- Android 下载网络图片注意的问题
- httpClient----java基础
- angular 之 $http
- Android 判断网络类型
- chkconfig --add httpd Error服务不支持
- java Webservice(一)HttpClient使用(一)
- 标准java http和 apach httpclient总结
- LinuxMint下Apache Http源码安装过程
- http长连接和短连接-转
- java中网络socket编程简单例子,用于读写字符
- python网络编程实例简析
- HttpURLConnection的总结