ios - 关于重大位置变更的本地通知

标签 ios objective-c iphone core-location uilocalnotification

我正在开发一个原型(prototype),以在位置发生重大变化时通知用户。当应用程序关闭/终止时,我发送一个本地通知来通知用户。通知工作完美,但是仅一次。虽然我在 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位置更改教程,并向其中添加了我的通知方法。

在收到每次重大位置更改通知时,我哪里会滞后?

最佳答案

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

关于ios - 关于重大位置变更的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31332731/

相关文章:

ios - Html 字符串转换为属性字符串\U00002028\n ios

objective-c - Cocoa,如何在 NSTextView 中选择文本

ios - 自动合成属性 'delegate' 将使用合成实例变量 '_delegate' ,而不是 MBProgressHUD.m 中现有的实例变量 'delegate'

ios - 如何给 UITableViewController 添加 Accordion 效果?

swift - 如何创建通用的 UITableviewCell,它可以通过委托(delegate)或其他方式将通用数据类型传递给主 UIViewController?

iphone - 如何检测 iPhone 6 设备的运动? (确定 iPhone 设备是否移动 - x、y、z 上的最小可能运动-)

ios - 在一个屏幕上有一些 UITableViews 和一些 UICollectionViews

ios - iPhone 数据最佳实践 - 缓存与远程

objective-c - .m 文件中的空@interface 声明是做什么用的?

iphone - "Single NSMutableArray"与 "Multiple C-arrays"-- 哪个更高效/实用?