API連線並更新主線程
要先在info裡面開啟權限
App Transport Security Settings -> Allow Arbitrary Loads value:YES
App Transport Security Settings -> Allow Arbitrary Loads value:YES
NSURL *url = [NSURL URLWithString:urlStr]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dt = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (!error) { //NSLog(@"%@", data); dancePrograms = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"%@", dancePrograms); //更新主線程UI dispatch_async(dispatch_get_main_queue(), ^{ //重新刷新tableview [tbView reloadData]; }); } else { //處理Error } }]; //連接資料 [dt resume];
留言
張貼留言