2017-01-16 14 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