【发布时间】:2014-08-31 05:10:14
【问题描述】:
我已按照WWDC 2014 session "What's New in iOS Notifications" 中给出的示例设置跨越区域边界时的UILocalNotification 交付。
但是,我的通知永远不会在运行 iOS beta 3 的设备或模拟器上触发。当我使用 fireDate 而不是 region 进行测试时,通知工作正常(不能同时使用这两个 - 这是不允许的)。
我在Info.plist 中设置了NSLocationWhenInUseUsageDescription。
这是我的 Obj-C 代码:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *notifLocation;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// test points at Apple HQ
self.notifLocation = [[CLLocation alloc] initWithLatitude:37.331741 longitude:-122.030333];
[self setupLocationMonitoring];
}
- (void) setupLocationMonitoring
{
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 10; // meters
// iOS 8+ request authorization to track the user’s location
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
- (void)registerLocationNotification
{
UILocalNotification *locNotification = [[UILocalNotification alloc] init];
locNotification.alertBody = @"Hello!";
// doesn't work
locNotification.regionTriggersOnce = NO;
locNotification.region = [[CLCircularRegion alloc] initWithCenter:self.notifLocation.coordinate radius:50 identifier:@"PlaceName"];
// works:
// locNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
UIApplication *application = [UIApplication sharedApplication];
[application cancelAllLocalNotifications];
[application scheduleLocalNotification:locNotification];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
// check status to see if we’re authorized
BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
if (canUseLocationNotifications) {
[self registerLocationNotification];
}
}
@end
注意:这个问题是关于 iOS 8 的新位置通知,而不是关于旧版本 iOS 上的区域监控。
【问题讨论】:
标签: objective-c core-location uilocalnotification ios8