如何使用iOS上基于地理位置的推送通知? [英] How to use geo-based push notifications on iOS?

查看:868
本文介绍了如何使用iOS上基于地理位置的推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用基于地理位置的推送通知的iOS上,当应用程序被杀死(不是在后台)?

Is it possible to make use of geo-based push notifications on iOS when the application is killed (not in the background)?

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

I am interested in building an app, where the user will choose a position on a map, and then if he/she for example is close to that area a local geo-based push notification would be triggered.

然而就是这个理念甚至可能吗?该GPS可以运行和到位的时候,当应用程序被杀害比较坐标和运行,并通知用户?是否有一个教程/条/以上任何形式的对,我可以读出主题信息?

However is this "idea" even possible? Can the GPS run and compare coordinates when the app is killed and run and notify the user when is in place? Is there a tutorial/article/more information of any kind on the subject that i could read?

大部分时间我在网上读到的信息更像是实施没有具体的,虽然对此事的任何总体思路。

Most of the information I read online were more like general ideas of implementing without anything specific though on the matter.

推荐答案

有关跟踪用户的位置,而应用程序没有运行(即有previously被终止),有两种选择:

For tracking a user's location while the app is not running (ie. has previously been terminated), there are two options:


  1. 从<一个href=\"http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24\">iOS 跟踪用户位置下的应用程序编程指南:

该显著变化位置服务强烈建议那些不需要高precision位置数据的应用程序。有了这项服务,当用户的位置显著的变化只产生位置更新;因此,它是理想的提供非关键,位置相关的信息的用户社会化应用或应用。如果出现更新时应用程序被暂停,系统唤醒它在后台处理更新。 如果应用程序启动这个服务,然后终止,系统会自动将重新启动应用程序时,一个新的位置可用。该服务可在iOS 4的,后来,它仅适用于包含蜂窝无线设备。

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.

然而,根据本<一href=\"https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html\">CLLocationManager类引用,这不是太准确,更新罕见:

However, according to the CLLocationManager class reference, it's not too accurate and updates are infrequent:

请注意:应用程序能够尽快期待一个通知,该设备从previous通知移动五百米以上。它不应该指望比通知更加频繁,每五分钟一次。如果该装置能够从网络检索数据,所述位置管理器是更可能及时递送通知。

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.

<一个href=\"http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW13\">Region监控以类似的方式作品 - 包括被终止后重新启动应用程序 - 但精度更高(取决于WiFi网络和手机信号塔的可用性):

  • Region Monitoring works in a similar way - including restarting the app after being terminated - but with higher accuracy (depending on availability of Wifi networks and cell towers):

    的特定阈值的距离是由硬件和位置的技术,目前可用来确定。例如,如果Wi-Fi被禁用,区域监控显著不准确。但是,出于测试目的,你可以假设的最小距离约为200米。

    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.

    另一个区域监测考虑是,(根据<一href=\"https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html\">CLLocationManager类引用)区域的入口和出口的通知可能只穿越该地区的边界之后接收3-5分钟左右。

    Another region monitoring consideration is that (according to the CLLocationManager class reference) region entry and exit notifications might only be received 3-5 minutes or so after crossing the region's boundaries.

    根据实际需要,区域监测可以用于获得一个粗的位置,然后,当用户是一个特定的区域内,启动对位置管理的更精确的基于GPS的服务。当用户离开感兴趣的区域中,GPS服务关闭以preserve电池并恢复到粗位置监视服务(即区域监测)再次。这里有一个基本的实现:

    Depending on the actual requirements, region monitoring could be used for obtaining a "rough" location and then when the user is within a specific region, start up the more accurate GPS based service on the location manager. When the user leaves the region of interest, turn off the GPS service to preserve battery and revert to the rough location monitoring service (ie. region monitoring) once again. Here's a basic implementation:

    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文件让应用程序将在后台访问相应的资源运行:

    Remember to add the appropriate entries to the application's plist file so the app will run in the background with access to the appropriate resources:

    MyApp的-Info.plist中

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

    上面code假定使用iOS6的和ARC的

    这篇关于如何使用iOS上基于地理位置的推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆