AppleWatchOS2的网络请求

在AppleWatchOS2的版本中,苹果新增加了watch连接wifi的功能,那么在Watch Extension中我们就可以直接通过网络请求申请数据了。话不多说直接上代码。

NSURL *url = [NSURL URLWithString:@"urlstr"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

NSString *bodyStr = @"xx=xx&xx=xx";

NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:bodyData];

[request setValue:[NSString stringWithFormat:@"string"] forHTTPHeaderField:@"heardstring"];

NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request

completionHandler:

^(NSData *data, NSURLResponse *response, NSError *error) {

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

NSLog(@"解析到的数据%@",dic);

}];

// 使用resume方法启动任务

[dataTask resume];

你可能感兴趣的:(AppleWatchOS2的网络请求)