有关重要位置变更的本地通知

时间:2015-07-10 04:55:28

标签: ios objective-c iphone core-location uilocalnotification

我正在开发一个原型来通知用户重要的位置变化。当应用程序关闭/终止时,我发送本地通知以通知用户。通知工作完美,但是,仅限一次。虽然我在didFinishLaunching:收到了位置更改,但我没有收到本地通知。下面是我的简单代码。

ViewController我注册通知。

#import "LocationViewController.h"

@interface LocationViewController ()

@end

@implementation LocationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Registerimg for Motification
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}

以下是我的didFinishLaunching:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"didFinishLaunchingWithOptions");

    self.shareModel = [LocationShareModel sharedModel];
    self.shareModel.afterResume = NO;

    [self addApplicationStatusToPList:@"didFinishLaunchingWithOptions"];

     UIAlertView * alert;

    //We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
    if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){

        alert = [[UIAlertView alloc]initWithTitle:@""
                                          message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
                                         delegate:nil
                                cancelButtonTitle:@"Ok"
                                otherButtonTitles:nil, nil];
        [alert show];

    }else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){

        alert = [[UIAlertView alloc]initWithTitle:@""
                                          message:@"The functions of this app are limited because the Background App Refresh is disable."
                                         delegate:nil
                                cancelButtonTitle:@"Ok"
                                otherButtonTitles:nil, nil];
        [alert show];

    } else{

        // When there is a significant changes of the location,
        // The key UIApplicationLaunchOptionsLocationKey will be returned from didFinishLaunchingWithOptions
        // When the app is receiving the key, it must reinitiate the locationManager and get
        // the latest location updates

        // This UIApplicationLaunchOptionsLocationKey key enables the location update even when
        // the app has been killed/terminated (Not in th background) by iOS or the user.

        if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
            NSLog(@"UIApplicationLaunchOptionsLocationKey");

            [[UIApplication sharedApplication] cancelAllLocalNotifications];

            //Establish notification details
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            notification.fireDate = [NSDate date];
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.repeatInterval = 0;
            notification.alertBody = [NSString stringWithFormat:@"Success"];
            notification.soundName = UILocalNotificationDefaultSoundName;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
            [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

            // This "afterResume" flag is just to show that he receiving location updates
            // are actually from the key "UIApplicationLaunchOptionsLocationKey"
            self.shareModel.afterResume = YES;

            self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
            self.shareModel.anotherLocationManager.delegate = self;
            self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
            self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

            if(IS_OS_8_OR_LATER) {
                [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
            }

            [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];

            [self addResumeLocationToPList];
        }
    }

    return YES;
}

我按照this教程进行位置更改,并将通知方法添加到其中。

在每次重要位置更改时,我在哪里都会滞后?

2 个答案:

答案 0 :(得分:0)

我猜您使用iOS 8或更高版本作为部署目标。 您是否在info.plist中添加了NSLocationWhenInUseUsageDescription?我在您的教程中没有看到这一点,并且这对于位置库是强制性的。

答案 1 :(得分:0)

经过一番调查,我得到了答案。

当应用程序正在收听重要的位置更改并且应用程序已关闭时,如果位置发生更改,则会调用didFinishLaunchingWithOptions:并获取本地通知,如我的问题所述。

这意味着应用程序已启动,但不在前台。

对于下一个位置更改,didFinishLaunchingWithOptions:将不会被调用,因为应用程序已经启动。因此,一般委托方法CLLocationManagerDelegatedidUpdateLocations:将在之后调用所有位置更改。