我试图在每次用户通过我的客户商店(大约1700家商店)时发送通知但是它无效。
有人可以告诉我为什么吗?
我的代码:
Store.m
-(CLCircularRegion*)createCircularRegion
{
CLCircularRegion *region=[[CLCircularRegion alloc] initWithCenter:self.geoPoint radius:1000 identifier:self.identifier];
region.notifyOnEntry=YES;
return region;
}
viewController.m
-(void)startMonitoringAllStores
{
if (![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
NSLog(@"Monitoring is not available for CLCircularRegion class");
}
for (Store *currentStore in self.allStores) {
CLCircularRegion *region=[currentStore createCircularRegion];
[self.locationManager startMonitoringForRegion:region];
}
}
-(void)getStoresFromParse
{
[self findObjectsWithClassName:@"Stores"];
[self.allStores addObjectsFromArray:self.allStores];
[self startMonitoringAllStores];
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.locationManager.delegate=self;
[self.locationManager requestAlwaysAuthorization];
return YES;
}
-(void)handleRegionEvent:(CLRegion*)region
{
NSLog(@"Geofence triggered");
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
if ([region isKindOfClass:[CLCircularRegion class]]) {
[self handleRegionEvent:region];
}
}
答案 0 :(得分:0)
您只能在iOS中同时监控20个位置。见Apple's documentation。另外,请确保您通过requestAlwaysAuthorization
请求权限。
解决此限制的一种方法是找到最靠近用户的19个商店并监控这些区域,然后使用第20个区域作为用户当前位置的外边界,其半径是到达用户的距离。第19家商店。当用户越过外边界时,重新计算最近的19个商店和边界,然后重复。在代码中,你会做这样的事情。 (请注意,我还没有测试过这段代码;它只是一个例子。)
- (void)installClosestGeofencesTo:(CLLocation *)currentLocation {
[self removeAllGeofences];
NSArray *closestStores = [self findClosestStoresTo:currentLocation limit:19];
for (Store *store in closestStores) {
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:store.location radius:100 identifier:store.identifier];
[self.locationManager startMonitoringForRegion:region];
}
Store *furthestStore = [closestStores lastObject];
CLCircularRegion *boundaryRegion = [[CLCircularRegion alloc] initWithCenter:currentLocation.coordinate radius:furthestStore.distance identifier:@"boundary"];
[self.locationManager startMonitoringForRegion:boundaryRegion];
}
- (void)removeAllGeofences {
NSArray *monitoredRegions = [self.locationManager monitoredRegions];
for (CLRegion *region in monitoredRegions) {
[self.locationManager stopMonitoringForRegion:region];
}
}
- (NSArray *)findStoresClosestTo:(CLLocation *)location limit:(NSUInteger)limit {
...
}
在CLLocationManagerDelegate
中,您可以执行以下操作:
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
if ([region.identifier isEqual:@"boundary"]) {
[self installClosestGeofencesTo:self.locationManager.location];
}
}