gpt4 book ai didi

iphone - 如何在 iOS 上使用基于地理的推送通知?

转载 作者:IT王子 更新时间:2023-10-29 07:57:34 25 4
gpt4 key购买 nike

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

我有兴趣构建一个应用程序,用户将在其中选择 map 上的位置,然后如果他/她靠近该区域,则会触发本地基于地理的推送通知。

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

我在网上阅读的大部分信息更像是实现的一般想法,但没有任何具体内容。

最佳答案

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

  1. 来自 iOS app programming guide在“跟踪用户位置”下:

    The significant-change location service is highly recommended for apps that do not need high-precision location data. With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social apps or apps that provide the user with noncritical, location-relevant information. If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.

    然而,根据CLLocationManager class reference , 它不太准确并且更新不频繁:

    Note: Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

  2. Region Monitoring以类似的方式工作 - 包括在终止后重新启动应用程序 - 但准确性更高(取决于 Wifi 网络和手机信号塔的可用性):

    The specific threshold distances are determined by the hardware and the location technologies that are currently available. For example, if Wi-Fi is disabled, region monitoring is significantly less accurate. However, for testing purposes, you can assume that the minimum distance is approximately 200 meters.

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

    根据实际需求,区域监控可用于获取“粗略”位置,然后当用户在特定区域内时,在位置管理器上启动更准确的基于 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];
    }

    请记住将适当的条目添加到应用程序的 plist 文件中,以便应用程序在后台运行并访问适当的资源:

    MyApp-Info.plist:

    <key>UIBackgroundModes</key>
    <array>
    ...
    <string>location</string>
    </array>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
    ...
    <string>location-services</string>
    <string>gps</string>
    </array>

    以上代码假定使用iOS6和ARC

关于iphone - 如何在 iOS 上使用基于地理的推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15590655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com