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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容