IOS项目中,必不可少的,需要访问HTTP请求,由于IOS9有了新的安全机制,导致项目不能直接访问HTTP请求,在此需要在info.plist项目描述信息中做如下操作:
在Info.plist中添加NSAppTransportSecurity
类型Dictionary
。
在NSAppTransportSecurity
下添加NSAllowsArbitraryLoads
类型Boolean
,值设为YES
如果没有做以上配置的话,访问HPPT请求会有如下报错信息:
Error =Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x7f9bea4c6af0 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSErrorFailingURLStringKey=http://www.baidu.com/, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSErrorFailingURLKey=http://www.baidu.com/}}, NSErrorFailingURLStringKey=http://www.baidu.com/, NSErrorFailingURLKey=http://www.baidu.com/, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}
配置好了Info.plist之后,就可以愉快的写代码了:
// MyViewController.h // 网络NSUIrConnect // #import <UIKit/UIKit.h> @interface MyViewController : UIViewController< //连接服务的普通协议 NSURLConnectionDelegate, //连接服务器对象的数据代理协议 NSURLConnectionDataDelegate > { //定义一个URL连接对象,通过网络地址可以进行客户端和服务器的连接工作 //按照Http协议进行传输数据工作 NSURLConnection *connect; //创建一个可变二进制数据对象,接受服务器传回的数据 NSMutableData *mydata; } @end
// MyViewController.m // 网络NSUIrConnect // // #import "MyViewController.h" @interface MyViewController () @end @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor whiteColor]; UIButton *buttton=[UIButton buttonWithType:UIButtonTypeRoundedRect]; buttton.frame=CGRectMake(100 , 100, 80, 40); [buttton setTitle:@"连接数据" forState:UIControlStateNormal]; [buttton addTarget:self action:@selector(getdata:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:buttton]; } -(void)getdata:(UIButton *)sender{ NSLog(@"连接………………"); NSString *str=@"http://www.baidu.com"; //将一个字符串转换成一个地址对象 NSURL *url=[NSURL URLWithString:str]; //定义一个请求对象 //在连接之前的信息的封装 NSURLRequest *request=[NSURLRequest requestWithURL:url]; //创建一个网络连接对象 //参数1:连接的请求对象 //参数2:代理对象,用来实现回传数据的代理协议 connect=[NSURLConnection connectionWithRequest:request delegate:self]; mydata=[[NSMutableData alloc]init]; } //处理错误信息的代理协议 //如果有任何的连接错误,调用此协议,进行错误的打印 //p1.连接对象 //p2.错误信息 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"Error =%@",error); } //处理协议返回的响应码 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ //将响应码转化为Http响应码 NSHTTPURLResponse *res=(NSHTTPURLResponse*)response; if(res.statusCode==200){ NSLog(@"连接成功………………"); }else if(res.statusCode==404){ NSLog(@"没有找到地址!"); }else if(res.statusCode==500){ NSLog(@"服务器异常"); } } //接受到了数据时 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ //将每次回传的数据连接起来 [mydata appendData:data]; } //接收完数据之后 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ //将Data转成String NSString *str=[[NSString alloc]initWithData:mydata encoding:NSUTF8StringEncoding]; NSLog(@" %@",str); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end