我正在尋找一種方法在iOS上實施基於地理位置的通知。
我找到了兩種方法,但我無法弄清楚哪一個是最好的和不同的。這些基於地理位置的通知方式在iOS上有什麼不同?
1:使用CLLocationManager的startMonitoring 像本教程一樣。 https://www.raywenderlich.com/136165/core-location-geofencing-tutorial
2:使用CLLocationManager的startMonitoring和UILocalNotification的區域。
這樣的代碼:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.alertBody = [NSString stringWithFormat:@"Hello!"];
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
CLCircularRegion *region = nil;
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(geoPoint.latitude,
geoPoint.longitude);
if (CLLocationCoordinate2DIsValid(location)){
region = [[CLCircularRegion alloc] initWithCenter:location
radius:50.0
identifier:@"region1"];
region.notifyOnExit = NO;
localNotif.region = region;
localNotif.regionTriggersOnce = YES;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
} else {
NSDictionary *info = @{NSLocalizedDescriptionKey:@"Invalid coordinate info."};
NSError *error = [NSError errorWithDomain:@"InvalidCLLocationError"
code:1999
userInfo:info];
}