2017-01-16 16 views
0

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

回答

1

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

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

请注意:截至iOS 10,UILocalNotification已弃用。请使用UNNotificationRequest代替。

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