Iphone 如何在iOS上使用基于地理位置的推送通知?

Iphone 如何在iOS上使用基于地理位置的推送通知?,iphone,ios,geolocation,push-notification,apple-push-notifications,Iphone,Ios,Geolocation,Push Notification,Apple Push Notifications,当应用程序被终止时(不是在后台),是否可以在iOS上使用基于地理位置的推送通知 我感兴趣的是构建一个应用程序,用户将在地图上选择一个位置,然后如果他/她(例如)靠近该区域,将触发本地基于地理位置的推送通知 然而,这个“想法”可能吗?当应用程序被关闭时,GPS能否运行并比较坐标,并在安装到位时运行并通知用户?有关于这个主题的教程/文章/更多信息我可以阅读吗 我在网上读到的大多数信息更像是在没有任何具体内容的情况下实施的一般想法。要在应用程序未运行(即之前已终止)时跟踪用户的位置,有两种选择: 从“

当应用程序被终止时(不是在后台),是否可以在iOS上使用基于地理位置的推送通知

我感兴趣的是构建一个应用程序,用户将在地图上选择一个位置,然后如果他/她(例如)靠近该区域,将触发本地基于地理位置的推送通知

然而,这个“想法”可能吗?当应用程序被关闭时,GPS能否运行并比较坐标,并在安装到位时运行并通知用户?有关于这个主题的教程/文章/更多信息我可以阅读吗


我在网上读到的大多数信息更像是在没有任何具体内容的情况下实施的一般想法。

要在应用程序未运行(即之前已终止)时跟踪用户的位置,有两种选择:

  • 从“跟踪用户位置”下的:

    对于不需要高精度位置数据的应用程序,强烈建议使用“重大更改位置”服务。使用此服务,仅当用户的位置发生显著变化时,才会生成位置更新;因此,它非常适合社交应用程序或为用户提供非关键、位置相关信息的应用程序。如果更新发生时应用程序被挂起,系统会在后台将其唤醒以处理更新如果应用程序启动此服务,然后终止,则系统会在新位置可用时自动重新启动应用程序。此服务在iOS 4及更高版本中可用,并且仅在包含蜂窝无线电的设备上可用

    然而,根据调查,这并不太准确,而且更新也很少:

    注意:一旦设备距离上次通知移动500米或以上,应用程序就会收到通知。它不应期望通知频率超过每五分钟一次。如果设备能够从网络中检索数据,则位置管理器更有可能及时发送通知

  • 工作方式类似-包括在应用程序终止后重新启动-但精确度更高(取决于Wifi网络和手机发射塔的可用性):

    具体阈值距离由当前可用的硬件和定位技术确定。例如,如果禁用Wi-Fi,则区域监控的准确性会明显降低。但是,出于测试目的,可以假设最小距离约为200米

    区域监控的另一个考虑因素是(根据)区域进入和退出通知可能仅在跨越区域边界后3-5分钟左右收到

    根据实际需求,区域监控可用于获取“粗略”位置,然后当用户位于特定区域内时,在location manager上启动更精确的基于GPS的服务。当用户离开感兴趣的区域时,关闭GPS服务以保留电池,并再次恢复到粗略位置监控服务(即区域监控)。下面是一个基本实现:

    SomeViewController.m

    ...
    @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


  • 为什么不想在后台运行应用程序(意味着在关闭应用程序后)?我在我的应用程序中应用了相同的概念和后台运行功能。你能详细说明一下吗?例如。假设用户做了一个提醒,他想看到它