1
私はyoutube APIを呼び出して、ビデオのタイトルを取得します。私は、テーブルの画面にビデオのタイトルを表示したい。ブロックの実行が終了したら、title
にアクセスするにはどうすればよいですか?ios - ブロック内のUIを更新する
ここでタイトル
-(void)getVideoTitle:(NSString *)urlStr success:(void (^)(NSDictionary *responseDict))success{
urlStr = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/videos?part=contentDetails%%2C+snippet%%2C+statistics&id=%@&key={API_KEY}",urlStr];
NSURL *url = [[NSURL alloc] initWithString:urlStr];
// Create your request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// Send the request asynchronously
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *connectionError) {
// Callback, parse the data and check for errors
if (data && !connectionError) {
NSError *jsonError;
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if (!jsonError) {
success(jsonResult);
// NSLog(@"Response from YouTube: %@", jsonResult);
}
}
}] resume];
}
を取得するためのコードは、ここで私は上記の関数を呼び出す方法です:私は後にブロックの外側title
変数にアクセスし得るかどう
[self getVideoTitle:@"zB4I68XVPzQ" success:^(NSDictionary *responseDict){
NSArray *itemsArray = [responseDict valueForKey:@"items"];
NSDictionary *item = itemsArray[0];
NSDictionary* snippet = [item valueForKey:@"snippet"];
NSString *title = [snippet valueForKey:@"title"];
}];
ブロックは実行を終了しましたか?
ノー運あなたのコードで
dispatch_async(dispatch_get_main_queue(), ^{
[self updateMyUserInterfaceOrSomething];
});