我正在尝试使用重要的位置更改在iOS中为iOS 4编写应用程序,每次位置代理收到位置更新时都会触发本地通知。我的代码发布在下面。我将应用程序辞职并将其关闭。只要手机处于唤醒状态(屏幕亮起),应用程序就可以正常工作,触发通知,但是当我将手机置于睡眠状态(屏幕为黑色)时,在唤醒手机之前我不再收到通知通过按主页按钮,接收文本等,然后通知被触发。即使设备处于睡眠状态,我还需要做什么才能进行位置更新注册?提前谢谢。
SigLoc.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface SigLocViewController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
SigLoc.m
#import "SigLocViewController.h"
@implementation SigLocViewController
@synthesize locationManager;
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringSignificantLocationChanges];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *alarm = [[[NSClassFromString(@"UILocalNotification") alloc] init] autorelease];
if (alarm) {
alarm.fireDate = nil;
alarm.repeatInterval = 0;
alarm.alertBody = @"Location Update";
alarm.soundName = UILocalNotificationDefaultSoundName;
alarm.hasAction = NO;
[app presentLocalNotificationNow:alarm];
}
}
答案 0 :(得分:0)
我的猜测是,当您的应用进入后台时,您的视图无效,因此无法获取通知。我会将您的应用程序委托指定为位置管理器委托,然后将回调传递给您的SigLoc位置管理器委托。
我有一个类似的问题试图接收回调,直到我将位置管理器委托移动到我的应用委托后才解决。我会从那里开始,回到你的方法。