didEnterRegion works in foreground but not background or other VCs

后端 未结 4 593
夕颜
夕颜 2020-12-16 03:56

If the app is running and the CLLocationManagerDelegate class is the foreground (i.e. visible) then the didEnterRegions triggers and I get both the NSLog as well as the Aler

4条回答
  •  一向
    一向 (楼主)
    2020-12-16 04:52

    Things you are doing wrong:

    1. Background modes - App registers for location updates. This is not needed. This is need when you want to gather info for significant changes in location etc. So, go to Targets > Your app > Capabilites , and select the desired option under Background modes. This will automatically update the plist for you. For now, disable it.
    2. You are trying to create an alert when the user enters a region. While this while work when app is working, an alert is of no use when your app is in background. Do - Rather trigger a local notification or an api call.

    eg. of a notification:

        -(void)triggerLocalNotification:(CLRegion *)region{
        UILocalNotification *notification = [[UILocalNotification alloc]init];
        [notification setAlertBody:[NSString stringWithFormat:@"Welcome to %@", [region identifier]]];
        [notification setRepeatInterval:0];
        [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
        [notification setTimeZone:[NSTimeZone  defaultTimeZone]];
        [[UIApplication sharedApplication]scheduleLocalNotification:notification];
        NSLog(@"notification triggered with notification %@", notification);
    }
    

提交回复
热议问题