IOS--網絡請求基礎

網絡權限配置 Info.plist網絡

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3 <plist version="1.0">
4 <dict>
5     <key>NSAppTransportSecurity</key>
6     <dict>
7         <key>NSAllowsArbitraryLoads</key>
8         <true/>
9     </dict>

TestController.m多線程

 1 #import "TestController.h"
 2 
 3 @interface TestController()
 4 
 5 @property(nonatomic,strong)UIButton *button;
 6 
 7 @end
 8 
 9 @implementation TestController
10 
11 - (void)viewDidLoad
12 {
13     [super viewDidLoad];
14     
15     _button = [UIButton buttonWithType:UIButtonTypeSystem];
16     
17     _button.frame = CGRectMake(0, 20, 100, 20);
18     [_button setTitle:@"Hello" forState:UIControlStateNormal];
19     
20     [_button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
21 
22     
23     [self.view addSubview:_button];
24 
25 }
26 
27 -(void)start:(UIButton*)sender
28 {
29     //獲取URL
30     NSURL *url = [NSURL URLWithString:@"http://www.cnblogs.com/yuge790615"];
31     NSURLRequest *request = [NSURLRequest requestWithURL:url];
32     
33     //多線程隊列
34     NSOperationQueue *queue = [[NSOperationQueue alloc]init];
35     
36     //異步請求
37     [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
38         
39         
40         NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
41         NSLog(@"獲取內容爲:%@",content);
42     }];
43 }
44 
45 @end
相關文章
相關標籤/搜索