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

IOS--网络请求基础

2016-03-02 17:12 501 查看
网络权限配置 Info.plist

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


TestController.m

#import "TestController.h"

@interface TestController()

@property(nonatomic,strong)UIButton *button;

@end

@implementation TestController

- (void)viewDidLoad
{
[super viewDidLoad];

_button = [UIButton buttonWithType:UIButtonTypeSystem];

_button.frame = CGRectMake(0, 20, 100, 20);
[_button setTitle:@"Hello" forState:UIControlStateNormal];

[_button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_button];

}

-(void)start:(UIButton*)sender
{
//获取URL
NSURL *url = [NSURL URLWithString:@"http://www.cnblogs.com/yuge790615"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

//多线程队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];

//异步请求
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"获取内容为:%@",content);
}];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: