停用iOS应用程序上的地理位置 [英] Geolocation on deactivated iOS application
问题描述
当应用程序关闭时,可以将位置感知添加到应用程序中?
例如,我想创建一个接近服务,该服务在我的应用程序关闭时工作,并在用户到达某个区域时向用户推送本地通知。
我读到CLRegion,我可以获得类似的结果,但只有当应用程序处于活动状态(或在后台)时才会如此。
我需要类似于由用户位置激活的远程通知服务。但这听起来很奇怪。 iOS中可能有类似的服务吗?
有两种选择:
改变约翰在上面提到的服务。
另一个是 startRegionMonitoring
两者都在位置Awarness文档来自苹果
两者都可以在短时间内打开已终止(关闭)的应用程序,并且您可以发送本地推送通知来提醒用户。
应用程序无法进入正常状态,但如果用户点击通知,应用程序将打开,以便您可以实现目标。
这是一个拦截didFinishLaunchingWithOptions委托中的事件并触发本地通知的示例代码。
通常你不应该在这里发布通知,而是重新初始化你的位置管理器,获取新的位置并显示用户输入的区域的消息(本地通知)。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//查看应用程序是否已启动从一个地点事件!
if([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]){
//如果是的话,我们需要重新初始化我们的位置管理器!
[[LocationManager sharedLocationManager] startSignificantLocationMonitoring];
//发出通知
UILocalNotification * notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
NSTimeZone * timezone = [NSTimeZone defaultTimeZone];
notification.timeZone = timezone;
notification.alertBody = @app did wake up;
notification.alertAction = @确定;
notification.soundName = @yes.caf;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
It is possible to add location awareness to an application when it's closed?
For example I'd like to create a proximity service that works when my application is closed and pushes a local notification to the user when he reaches an area.
I read that with CLRegion I can achieve a similar result but only whenever the application is active (or in background).
I need something similar to a remote notification service activated by the user location. But it sounds really strange. Is it possible to have a similar service in iOS?
There are 2 options:
One is the significant location changes service which John mentioned above.
The other one is the startRegionMonitoring
Both are covered in the Location Awarness Documentation From Apple
Both can open an terminated (closed) app for a brief moment and you can deliver a local push notification to alert the user. The app cannot get in a normal state, but if the user clicks the notification the app will open, so you can achieve your goal.
Here is a sample code on intercepting the event in the didFinishLaunchingWithOptions delegate and firing a local notification. Normally you should not fire the notification here, but rather reinitialize your location manager, get the new position and display the message (local notification) for the area the user did enter...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//see if application was launched from a location event!
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
//if so, we need to reinitialize our location manager!
[[LocationManager sharedLocationManager] startSignificantLocationMonitoring];
//fire a notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
notification.timeZone = timezone;
notification.alertBody = @"app did wake up";
notification.alertAction = @"OK";
notification.soundName = @"yes.caf";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
这篇关于停用iOS应用程序上的地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!