ajax请求出错 ios9
2015-11-13 20:53:19 xintailirui 阅读数 2880
//
//  ViewController.m
//  UI_15_session(iOS9网络请求)
//
//  Created by lanou3g on 15/11/13.
//  Copyright © 2015年 lirui. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDataDelegate>
@property(nonatomic,strong)NSMutableData *mutableData;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark session block 网络请求
- (IBAction)session_blockButtonDidClicked:(id)sender {
    
    NSString *baseString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
    //转化为URL
    NSURL *baseURL = [NSURL URLWithString:baseString];
    
    //根据 baseURL 创建网络请求对象
    NSMutableURLRequest *requset = [NSMutableURLRequest requestWithURL:baseURL];
    //设置参数:1.POST 2.参数体(body)
    [requset setHTTPMethod:@"POST"];
    //2.body参数
    NSString *bodyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
    NSData *badyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    //设置 body(POST参数)
    [requset setHTTPBody:badyData];
    
    //iOS 9 提供了 NSURLSession 来代替  NSURLConnection
    //首先,创建一个 NSURLSession 对象(如果要使用block来完成网络请求,这个对象可以使用 NSURLSession 自带的单例对象)
    NSURLSession *session = [NSURLSession sharedSession];
   
    // session发送网络请求对象
    
    NSURLSessionTask *task = [session dataTaskWithRequest:requset completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dict);
    }];
    //开始网络请求任务
    [task resume];
    
 
}

#pragma mark session 代理 实现网络请求
- (IBAction)session_delegateButtonDidClicked:(id)sender {
    
    NSString *baseString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
    NSURL *baseURL = [NSURL URLWithString:baseString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
    
    [request setHTTPMethod:@"POST"];
    NSString *bodyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
    NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    
    //创建 session
    //默认的 session 配置,从网络读取数据
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    //需要遵守 NSURLSessionDataDelegate
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
    
    //利用 session 发送网络请求
    NSURLSessionTask *task = [session dataTaskWithRequest:request];
    
    
    //初始化接收数据的容器
    self.mutableData = [NSMutableData data];
    
    //开始任务
    [task resume];


}

#pragma mark 1.收到服务器相应
//-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
//    
//    NSLog(@"1");
//    
//}




#pragma mark 2.服务器开始传输数据(一点一点返回,这个代理方法会被反复调用,返回 NSData 数据)
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    
     NSLog(@"2");
    [self.mutableData appendData:data];

}
#pragma mark 3.客户端接收数据完成的时候
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask willCacheResponse:(NSCachedURLResponse *)proposedResponse completionHandler:(void (^)(NSCachedURLResponse * _Nullable))completionHandler {
    
     NSLog(@"3");
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.mutableData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",dict);
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Session  block网络请求,适合不需要大文件(例如视频)持续下载。

Session delegate 网络请求,使用代理方法。适合断点下载。

2015-10-12 14:56:48 tq471214251 阅读数 631

在info.plist中添加 NSAppTransportSecurity dictionary

在NSAppTransportSecurity中添加  NSAllowsArbitraryLoads boolean  value=YES


2015-12-08 20:38:39 xujianbo11111 阅读数 396

iOS 9 请求网络数据出现:App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure

问题原因:在iOS9.0之后版本中,苹果将原http协议改成了https协议,使用 TLS1.2 SSL加密请求数据。
解决方案:(1)打开xcode项目中的Supporting Files文件夹;
        (2)选中Info.plist;
(3)在Info.plist中增加如下键值对:

(4)再次运行程序完美解决问题!
没有更多推荐了,返回首页