每次用户通过我的客户端的存储(大约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];
}
}
发布于 2016-01-20 08:05:37
在iOS中,您只能同时监视20个位置。见苹果的文档。另外,确保您正在通过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];
}
}
https://stackoverflow.com/questions/34904674
复制相似问题