Ich würde gerne wissen, wenn Benutzer in der Nähe von meinem Haus sind. Ich verwende CoreLocation und möchte immer wissen, ob sich der Benutzer in meiner Nähe befindet (ca. 100 m), selbst wenn die App geschlossen ist.iOS Geofencing mit CoreLocation
Dafür ist es mir in meinem Code zu wissen, ob der Benutzer etwa 100m (mit CLRegionStateInside
) ist. DidEnterRegion
und DidExitRegion
funktioniert auch gut. Ich verwende kCLAuthorizationStatusAuthorizedAlways
, um Benutzer zu lokalisieren.
Ich möchte Batterielebensdauer sparen, so fügte ich self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
hinzu, um Standort zu aktualisieren, nachdem Benutzer 3km macht.
Sagen Sie mir, wie kann ich mehr Akkulaufzeit sparen, und wie kann ich den Standort aktualisieren, selbst wenn die App im Hintergrund/geschlossen ist (vielleicht muss ich etwas in meinem AppDelegate ändern)?
Und ich würde gerne wissen, ob mein Code für meine Bedürfnisse funktioniert?
Hier ist mein Code:
#import "ViewController.h"
@import UserNotifications;
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Request Notification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
}
- (void)setUpGeofences {
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(49.451096,
1.095425);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center
radius:100.0
identifier:@"Home"];
[self.locationManager startMonitoringForRegion:region];
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
self.locationManager.allowsBackgroundLocationUpdates = YES;
}
- (void)showSorryAlert {
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"Info"
message:@"You are using UIAlertController"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusNotDetermined:
[self.locationManager requestAlwaysAuthorization];
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
[self.locationManager startUpdatingLocation];
[self setUpGeofences];
break;
case kCLAuthorizationStatusAuthorizedAlways:
[self.locationManager startUpdatingLocation];
[self setUpGeofences];
break;
case kCLAuthorizationStatusRestricted:
// restricted by e.g. parental controls. User can't enable Location Services
break;
case kCLAuthorizationStatusDenied:
// user denied your app access to Location Services, but can grant access from Settings.app
break;
default:
break;
}
}
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region {
NSLog(@"didEnter : %@", region);
[self displayNotif:@"Bienvenue !" withBody:@"Passez nous voir !"];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[self displayNotif:@"Au revoir !" withBody:@"A bientôt !"];
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
NSLog(@"Start monitoring for region: %@", region.identifier);
[self.locationManager requestStateForRegion:region];
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
NSLog(@"Error: %@", error);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
NSLog(@"NEW LOCATION");
// Stop location updates when they aren't needed anymore
[self.locationManager stopUpdatingLocation];
// Disable background location updates when they aren't needed anymore
self.locationManager.allowsBackgroundLocationUpdates = NO;
}
- (void)locationManager:(CLLocationManager *)manager
didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
// When regions are initialized, see if we are already within the geofence.
switch (state) {
case CLRegionStateInside: [self displayNotif:@"Bienvenue" withBody:@"Passez nous voir !"];
break;
case CLRegionStateUnknown: NSLog(@"Unknown");
case CLRegionStateOutside: NSLog(@"Outside");
default: break;
}
}
- (void)displayNotif:(NSString *)title withBody:(NSString *)body {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:title arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:body
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:1.f repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"OneSecond"
content:content trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add NotificationRequest succeeded!");
}
}];
}
@end
Wenn Sie eine CLCircularRegion brauchen Sie nicht verwenden Standortaktualisierungen oder stellen Sie die gewünschte Genauigkeit zu starten, Entfernung Filter usw. nur in der Region hinzufügen und die Überwachung zu starten. Das ist es. iOS überwacht den Standort des Benutzers und ruft bei Bedarf die Regionsdelegiertenmethoden auf – Paulw11