1

我正在尝试创建一个应用程序,当我靠近某些位置时通知我公共汽车它不起作用但这里是我使用的代码

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];
 }
4

1 回答 1

1

看到这是 6 岁,我通过谷歌找到了这个,我将为任何其他正在寻找快速复制和粘贴解决方案的开发人员提供答案。

我假设您已经在应用程序 info.plist 中添加了正确的位置描述,并且您已经实现了正确的CLLocationManagerDelegate方法。

这也值得您花时间阅读:https ://developer.apple.com/documentation/corelocation/cllocationmanager?language=objc还有这个:https ://developer.apple.com/documentation/usernotifications?language=objc但是如果您已经完成了正确的设置,下面的代码应该可以正常工作。

第一步:
设置location manager,设置delegate,确保我们有使用权限:

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
}

此代码已在 Xcode 9.4.1 上针对 iOS 9 进行了测试。它使用模拟器、iOS 9 上的 iPhone 5 和运行 iOS 11.3.1 的 iPhone 7 Plus 工作。

于 2018-09-18T17:22:15.623 回答