Reputation: 373
I'm trying to create an application which notifies me when i'm near some locations bus it didn't work yet here is the code i used
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];
and the delegates
(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];
}
Upvotes: 1
Views: 1261
Reputation: 839
Seeing that this is 6 years old, and I found this via Google, I'll provide an answer for any other developer that's looking for a quick copy and paste solution.
I'm assuming that you've added the proper descriptions for location use in your apps info.plist and that you've implemented the proper CLLocationManagerDelegate
methods.
It's also worth your time to read: https://developer.apple.com/documentation/corelocation/cllocationmanager?language=objc and also this: https://developer.apple.com/documentation/usernotifications?language=objc but the below code should just work given you've done the proper set up.
Step one:
Set up the location manager, set the delegate, and make sure we have permission to use it:
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];
Step 2
Give the location manager a region to monitor
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];
Step 3
Although your code creates an alert view, the question is asking for a local notification, so we'll create one and trigger it when the device enters the region. My code also sends a local notification only if the app is in the background but you can comment out the if statement if you don't want that.
- (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
}
This code has been tested on Xcode 9.4.1 while targeting iOS 9. It worked using the Simulator, an iPhone 5 on iOS 9 and an iPhone 7 Plus running iOS 11.3.1.
Upvotes: 1