Iphone 如何在iOS上使用基于地理位置的推送通知?
当应用程序被终止时(不是在后台),是否可以在iOS上使用基于地理位置的推送通知 我感兴趣的是构建一个应用程序,用户将在地图上选择一个位置,然后如果他/她(例如)靠近该区域,将触发本地基于地理位置的推送通知 然而,这个“想法”可能吗?当应用程序被关闭时,GPS能否运行并比较坐标,并在安装到位时运行并通知用户?有关于这个主题的教程/文章/更多信息我可以阅读吗Iphone 如何在iOS上使用基于地理位置的推送通知?,iphone,ios,geolocation,push-notification,apple-push-notifications,Iphone,Ios,Geolocation,Push Notification,Apple Push Notifications,当应用程序被终止时(不是在后台),是否可以在iOS上使用基于地理位置的推送通知 我感兴趣的是构建一个应用程序,用户将在地图上选择一个位置,然后如果他/她(例如)靠近该区域,将触发本地基于地理位置的推送通知 然而,这个“想法”可能吗?当应用程序被关闭时,GPS能否运行并比较坐标,并在安装到位时运行并通知用户?有关于这个主题的教程/文章/更多信息我可以阅读吗 我在网上读到的大多数信息更像是在没有任何具体内容的情况下实施的一般想法。要在应用程序未运行(即之前已终止)时跟踪用户的位置,有两种选择: 从“
我在网上读到的大多数信息更像是在没有任何具体内容的情况下实施的一般想法。要在应用程序未运行(即之前已终止)时跟踪用户的位置,有两种选择:
...
@interface SomeViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLRegion *someRegion;
@end
@implementation SomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
CLLocationDistance radius = 10; // 10 metre sensitivity
self.someRegion = [[CLRegion alloc] initCircularRegionWithCenter:someCoordinates radius:radius identifier:@"Smithtown Dry Cleaners"];
self.locationManager.delegate = self;
[self.locationManager startMonitoringForRegion:self.someRegion];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 10;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self.locationManager stopUpdatingLocation];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
// If the user's current location is not within the region anymore, stop updating
if ([self.someRegion containsCoordinate:location.coordinate] == NO) {
[self.locationManager stopUpdatingLocation];
}
NSString *locationData = [NSString stringWithFormat:@"latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude];
NSLog(@"%@", locationData);
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = locationData;
localNotification.alertAction = @"Location data received";
localNotification.hasAction = YES;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
<key>UIBackgroundModes</key>
<array>
...
<string>location</string>
</array>
<key>UIRequiredDeviceCapabilities</key>
<array>
...
<string>location-services</string>
<string>gps</string>
</array>
。。。
@接口SomeViewController()
@属性(非原子,强)CLLocationManager*locationManager;
@性质(非原子,强)CLRegion*someRegion;
@结束
@视图控制器的实现
-(无效)viewDidLoad
{
[超级视图下载];
self.locationManager=[[CLLocationManager alloc]init];
CLLocationDistance半径=10;//10米灵敏度
self.someRegion=[[CLRegion alloc]initcircularregionwhichcenter:somecoordinations半径:半径标识符:@“Smithtown干洗店”];
self.locationManager.delegate=self;
[self.locationManager startMonitoringForRegion:self.someRegion];
self.locationManager.desiredAccuracy=KCallocationAccuracyBest;
self.locationManager.distanceFilter=10;
[self.locationManager startUpdatingLocation];
}
-(无效)locationManager:(CLLocationManager*)经理区域:(CLRegion*)区域
{
[self.locationManager startUpdatingLocation];
}
-(无效)locationManager:(CLLocationManager*)经理所在区域:(CLRegion*)区域
{
[self.locationManager停止更新位置];
}
//CLLocationManagerDelegate协议中的委托方法。
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置
{
CLLocation*location=[locations lastObject];
//如果用户的当前位置不再在该区域内,请停止更新
if([self.someRegion containsCoordinate:location.coordinate]==否){
[self.locationManager停止更新位置];
}
NSString*locationData=[NSString stringWithFormat:@“纬度%+.6f,经度%+.6f\n”,
位置坐标纬度,
位置.坐标.经度];
NSLog(@“%@”,位置数据);
UILocalNotification*localNotification=[[UILocalNotification alloc]init];
localNotification.alertBody=locationData;
localNotification.alertAction=@“接收到位置数据”;
localNotification.hasAction=YES;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotification];
}
请记住将适当的条目添加到应用程序的plist文件中,以便应用程序可以在后台运行并访问适当的资源:
MyApp Info.plist:
...
@interface SomeViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLRegion *someRegion;
@end
@implementation SomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
CLLocationDistance radius = 10; // 10 metre sensitivity
self.someRegion = [[CLRegion alloc] initCircularRegionWithCenter:someCoordinates radius:radius identifier:@"Smithtown Dry Cleaners"];
self.locationManager.delegate = self;
[self.locationManager startMonitoringForRegion:self.someRegion];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 10;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self.locationManager stopUpdatingLocation];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
// If the user's current location is not within the region anymore, stop updating
if ([self.someRegion containsCoordinate:location.coordinate] == NO) {
[self.locationManager stopUpdatingLocation];
}
NSString *locationData = [NSString stringWithFormat:@"latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude];
NSLog(@"%@", locationData);
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = locationData;
localNotification.alertAction = @"Location data received";
localNotification.hasAction = YES;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
<key>UIBackgroundModes</key>
<array>
...
<string>location</string>
</array>
<key>UIRequiredDeviceCapabilities</key>
<array>
...
<string>location-services</string>
<string>gps</string>
</array>
ui背景模式
...
位置
UIRequiredDeviceCapabilities
...
定位服务
全球定位系统
以上代码假定使用iOS6和ARC为什么不想在后台运行应用程序(意味着在关闭应用程序后)?我在我的应用程序中应用了相同的概念和后台运行功能。你能详细说明一下吗?例如。假设用户做了一个提醒,他想看到它