2017-01-16 4 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을 사용할 때 Geofence를 설정합니다.

CLLocationManager의 didEnterRegiondidExitRegion 대리자 메서드를 구현해야합니다. 이러한 방법으로 지역 알림을 설정합니다.

참고 : iOS 10부터 UILocalNotification은 더 이상 사용되지 않습니다. 대신 UNNotificationRequest을 사용하십시오.

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