【发布时间】:2012-01-23 07:15:25
【问题描述】:
我正在尝试在 iOS 中为 iPhone 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];
}
}
【问题讨论】:
标签: iphone ios core-location cllocationmanager locationmanager