내 사용자의 중요한 위치 변경을 모니터링하려고합니다. 따라서 앱에서 중요한 위치 변경 이벤트를 구독했지만 위임자 (위치 알림을 실행하는 코드를 작성한 장소)를 위치 관리자에게 할당하지 않았습니다. 나는 왜 다음 코드가 작동하는지 알 수 없다. 문서 here에 따르면위치 변경시 알림이 왜 발생합니까?
의 ViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (nil == locationManager){
locationManager = [[CLLocationManager alloc] init];
if ([locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[locationManager setAllowsBackgroundLocationUpdates:YES];
}
}
locationManager.delegate = self;
if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[locationManager requestAlwaysAuthorization];
[locationManager startMonitoringSignificantLocationChanges];
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation* location = [locations lastObject];
NSLog(@"latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
AppDelegate에
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
CLLocationManager *locationManager = [[CLLocationManager alloc]init];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.activityType = CLActivityTypeOtherNavigation;
[locationManager startMonitoringSignificantLocationChanges];
}
return YES;
}
. 이러한 서비스를 사용하는 앱을 종료하고 새로운 위치 이벤트가 도착하면 다시 시작할 수 있습니다. 앱 자체가 다시 시작되지만 위치 서비스는 자동으로 시작되지 않습니다. 이 데이터를 얻으려면 새 CLLocationManager 개체를 만들고 앱이 종료되기 전에 실행 한 위치 서비스를 다시 시작해야합니다. 이러한 서비스를 다시 시작하면 위치 관리자가 모든 보류중인 위치 업데이트를 해당 대리인에게 전달합니다.
그러나 앱 대리인에서 위치 관리자의 새 인스턴스에 대리인을 할당하지 않았으며 알림을 받고 있습니다.
'대리인 콜백'을 구현 했습니까? 'viewController'는'viewDidLoad'의'locationManager'가 생성되어'rootViewController'인가요, 아니면 일단 앱을 실행하면 인스턴스화됩니다. –
아니요 애플 리케이션 델리게이트에서 델리게이트 콜백을 구현하지 않았지만 네, ViewController는 내 rootViewController입니다 – mdanishs
'storyboard'를 사용하여'rootViewController'를로드하고 있기 때문에 그렇습니다. 'viewDidLoad'는 당신이 앱을 시작할 때 즉시 트리거되며 따라서 당신은 델리게이트 콜백을 받고 있습니다. –