我正在尝试创建一个应用程序,当我靠近某些位置的公交车时它通知我它没有工作但这里是我使用的代码
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = 5;
[locationManager startUpdatingLocation];
CLLocationCoordinate2D loc;
loc.latitude = 30.794253 ;
loc.longitude = 31.012369 ;
[CLLocationManager regionMonitoringEnabled];
alslamMosque =[[CLRegion alloc]initCircularRegionWithCenter:loc radius:800 identifier:@"alslam"];
[locationManager startMonitoringForRegion:alslamMosque desiredAccuracy:50];
[locationManager startMonitoringSignificantLocationChanges];
和代表
(void)locationManager:(CLLocationManager *)managerdidEnterRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
UIAlertView *alr=[[UIAlertView alloc]
initWithTitle:@"Reminder didEnterRegion"
message:region.identifier delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[alr show];
[alr release];
}
(void)locationManager:(CLLocationManager *)managerdidExitRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
UIAlertView *alr=[[UIAlertView alloc]
initWithTitle:@"Reminder didExitRegion"
message:region.identifier delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[alr show];
[alr release];
}
答案 0 :(得分:0)
看到这已经有6年了,并且我是通过Google找到的,因此我将为正在寻求快速复制和粘贴解决方案的任何其他开发人员提供答案。
我假设您已在应用程序info.plist中添加了有关位置使用的正确描述,并且已经实现了正确的CLLocationManagerDelegate
方法。
同样值得您花时间阅读:https://developer.apple.com/documentation/corelocation/cllocationmanager?language=objc和以下内容:https://developer.apple.com/documentation/usernotifications?language=objc,但是只要您已进行了正确的设置,下面的代码就可以工作。
第一步:
设置位置管理器,设置代理,并确保我们有权使用它:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[locationManager setDistanceFilter:50];
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) [locationManager requestAlwaysAuthorization];
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) [locationManager requestWhenInUseAuthorization];
第2步
为位置管理器分配一个要监视的区域
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(32.6099, -85.4808); // Auburn, Alabama Home of the Auburn Tigers, War Eagle!
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:1609 identifier:@"myGeofenceId"]; // Radius is set to a be a mile, given in meters
region.notifyOnEntry = YES; // Tell it to notify you on entry
[locationManager startMonitoringForRegion:region];
第3步
尽管您的代码创建了一个警报视图,但问题是要发出本地通知,因此我们将创建一个警报并在设备进入该区域时触发它。我的代码也仅在应用程序在后台时才发送本地通知,但如果您不希望这样做,则可以注释掉if语句。
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region {
// This is optional, but useful if you only want a notification if the phone is in the background.
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
[self createLocalNotificationWithDescription:@"You've entered the region!" AndId:@"Notification ID"];
}
}
// This just makes it a little easier and more tidy
-(void) createLocalNotificationWithDescription:(NSString *)description AndId:(NSString *)notificationID {
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
[userInfo setObject:notificationID forKey:@"notification_id"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo; // set the push id
localNotification.alertBody = [NSString stringWithFormat:@"%@", description]; // Set the body of the push
localNotification.soundName = [NSString stringWithFormat:@"Notification_sound_file.mp3"]; // Give it the alert sounds
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:0]; // Fire the notification now
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; // Ask the app to send the local push
}
此代码已在面向iOS 9的Xcode 9.4.1上进行了测试。它可以使用模拟器,iOS 9上的iPhone 5和运行iOS 11.3.1的iPhone 7 Plus进行工作。