【问题标题】:iOS Significant Location Change Only Works When Device is AwakeiOS 重大位置更改仅在设备唤醒时有效
【发布时间】: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


    【解决方案1】:

    我的猜测是当您的应用程序进入后台时您的视图未激活,因此无法获取通知。我会将您的应用代理分配为位置管理器代理,然后将回调传递给您的 SigLoc 位置管理器代理。

    我在尝试接收回调时遇到了类似的问题,直到我将位置管理器委托移动到我的应用委托后才解决。我会从那里开始,然后回到你的方法。

    【讨论】:

    • 我是一名高中生,仍在尝试掌握编程的窍门,但我对委托和传递回调并不十分熟悉。我尝试做的是从上面获取我的代码并将其移动到 AppDelegate.h 和 AppDelegate.m 中,但我仍然遇到了同样的问题。解决这个问题的正确方法是什么,或者我在哪里可以找到关于如何传递这些回调的好教程?
    猜你喜欢
    • 1970-01-01
    • 2014-02-22
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    相关资源
    最近更新 更多