gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:14:14 25 4
gpt4 key购买 nike

我正在开发一个原型(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com