完了ハンドラーは、ジオコーダーがジオコーディングを終了した後に実行されます。つまり、ジオコーディングタスクの完了時に実行されます。ジオコーダーの実行中に他のタスクを完了するためのものではありません。
完了ハンドラーには、目印とエラーが含まれています。ジオコーディングが成功すると、目印の配列が得られます。そうでない場合は、エラーが発生します。
ドキュメントからのメモ:
このメソッドは、指定された位置データを非同期でジオコーディングサーバーに送信し、戻ります。完了ハンドラブロックはメインスレッドで実行されます。フォワードジオコーディングリクエストを開始した後は、別のフォワードジオコーディングリクエストまたはリバースジオコーディングリクエストを開始しないでください。
ジオコーディングリクエストはアプリごとにレート制限されているため、短期間にリクエストが多すぎると、一部のリクエストが失敗する可能性があります。最大レートを超えると、ジオコーダーは値kCLErrorNetworkのエラーオブジェクトを完了ハンドラーに渡します。
@interface MyGeocoderViewController ()
@property (nonatomic, strong) CLGeocoder *geocoder;
@end
@implementation MyGeocoderViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Create a geocoder and save it for later.
self.geocoder = [[CLGeocoder alloc] init];
}
- (void)geocodeAddress:(NSString *)addressString
{
// perform geocode
[geocoder geocodeAddressString:addressString
completionHandler:^(NSArray *placemarks, NSError *error) {
if ((placemarks != nil) && (placemarks.count > 0)) {
NSLog(@"Placemark: %@", [placemarks objectAtIndex:0]);
}
// Should also check for an error and display it
else {
UIAlertView *alert = [[UIAlertView alloc] init];
alert.title = @"No places were found.";
[alert addButtonWithTitle:@"OK"];
[alert show];
}
}];
}
@end