//老方法
- (void)viewDidLoad {
[superviewDidLoad];
//獲取文件的訪問路徑
NSString*path=@"http://1.studyios.sinaapp.com/getAllClass.php";
//2、封裝URL
NSURL*url=[NSURLURLWithString:path];
//3叭莫、創(chuàng)建請求命令
NSURLRequest*request=[NSURLRequestrequestWithURL:url];
//4、響應(yīng)的對象
__autoreleasingNSURLResponse*response;
//5材蛛、錯誤信息
__autoreleasingNSError*error;
//6、通過同步請求的方式返回data對象
NSData*data=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:&error];
//7挥吵、json解析
NSArray*arr=[NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:&error];
NSLog(@"%@",arr);
}
下面是新方法
- (void)viewDidLoad {
[superviewDidLoad];
NSString*path=@"http://1.studyios.sinaapp.com/getAllClass.php";
NSURL*url=[NSURLURLWithString:path];
//創(chuàng)建請求命令
NSURLRequest*request=[NSURLRequestrequestWithURL:url];
//創(chuàng)建回話對象通過單例方法實現(xiàn)
NSURLSession*session=[NSURLSessionsharedSession];
//執(zhí)行回話的任務(wù)
NSURLSessionTask*task=[sessiondataTaskWithRequest:requestcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror){
//json解析
NSArray*arr=[NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:&error];
NSLog(@"%@",arr);
}];
//真正的執(zhí)行任務(wù)
[taskresume];
}
iOS9引入了新特性App Transport Security (ATS)草讶。
新特性要求App內(nèi)訪問的網(wǎng)絡(luò)必須使用HTTPS協(xié)議僻弹。
但是要使用HTTP協(xié)議用以下解決辦法:
找到Info.plist文件并在其中添加App Transport Security Settings類型Dictionary蔓榄。
在App Transport Security Settings下添加Allow Arbitrary Loads類型,Boolean值設(shè)為YES