在AppleWatchOS2的版本中,蘋果新增加了watch連接wifi的功能,那么在Watch Extension中我們就可以直接通過網(wǎng)絡(luò)請求申請數(shù)據(jù)了。話不多說直接上代碼。
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(@"解析到的數(shù)據(jù)%@",dic);
}];
// 使用resume方法啟動任務
[dataTask resume];