您的位置:首页 > 理论基础 > 计算机网络

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息