这些基于地理位置的通知方法在iOS上的区别是什么?

时间:2017-01-16 12:30:16

标签: ios geolocation cllocationmanager uilocalnotification

我正在寻找一种在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];
    }

1 个答案:

答案 0 :(得分:1)

为了在应用被解雇时显示通知,您应该使用UILocalNotification方法。这是因为CLLocationManager的startMonitoring将在您的应用程序被解除后停止工作。您在使用UILocalNotification时基本上设置了地理围栏。

您必须实现CLLocationManager的didEnterRegiondidExitRegion委托方法。在这些方法中,您将设置本地通知。

请注意:自iOS 10起,UILocalNotification已弃用。请改用UNNotificationRequest

https://developer.apple.com/reference/uikit/uilocalnotification