Xcode / iOS 模拟器:手动触发重要的位置更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6158304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode / iOS simulator: Trigger significant location change manually
提问by Nick
Is there a way to manually tell the device to dispatch a significant location change notification that will wake up any applications registered for this notification? This is for testing only, and I realize this private API call would get rejected upon submission to the app store.
有没有办法手动告诉设备发送一个重要的位置更改通知,该通知将唤醒为此通知注册的任何应用程序?这仅用于测试,我意识到这个私有 API 调用在提交到应用商店时会被拒绝。
回答by MagicSeth
So two answers:
所以有两个答案:
A) Hop on a train :-)
A) 跳上火车 :-)
B) Use the simulator. In iOS 5.x simulator, there is a debug menu that has a location submenu. Choose freeway drive. This will start the simulator on an imaginary journey down the scenic 280 in Northern California. It gives you everything but the view: your app will get Significant Location Change updates, and will also be launched in the backgroundif it has been suspended.
B) 使用模拟器。在 iOS 5.x 模拟器中,有一个带有位置子菜单的调试菜单。选择高速公路驱动器。这将启动模拟器在北加州风景如画的 280 英里的假想旅程中。它为您提供除视图之外的所有内容:您的应用将获得重要的位置更改更新,并且如果它已被暂停,也会在后台启动。
To verify that you are actually moving, launch safari in the simulator, and go to maps.google.com and click the little track my location button. You should be moving.
要验证您是否确实在移动,请在模拟器中启动 safari,然后转到 maps.google.com 并单击小跟踪我的位置按钮。你应该搬家了。
Awesome! Now how to debug the lifecycle problem of being launched by the system? Easy! Have xCode wait for your app to be launched to start debugging. From the Scheme menu, choose edit scheme. In the Run Scheme, and the Info tab, for the "Launch" setting choose : "Wait for My.app to launch".
惊人的!现在如何调试被系统启动的生命周期问题?简单!上马有无Xcode中等待你的应用程序开始调试。从方案菜单中,选择编辑方案。在 Run Scheme 和 Info 选项卡中,对于“Launch”设置,选择:“ Wait for My.app to launch”。
Run your app once in the simulator, so that it starts monitoring for location updates, then force quit it, so that it is suspended. Add a break pointin your application did finish launching function, and wait. As soon as your simulator has gone far enough, your application will be woken up, your breakpoint hit, and you are in the money.
在模拟器中运行您的应用程序一次,以便它开始监视位置更新,然后强制退出它,使其暂停。 在您的应用程序中添加一个断点确实完成了启动功能,然后等待。一旦您的模拟器运行得足够远,您的应用程序就会被唤醒,您的断点命中,您就可以赚钱了。
But really, the train ride is more fun.
但实际上,坐火车更有趣。
回答by Chris
Well, I've found that I can do this by toggling on and off Airplane mode and/or WiFi. Perhaps start the app with the device in airplane mode, then close the app and turn airplane mode off. That will turn the GPS on and force a location update to be dispatched.
嗯,我发现我可以通过打开和关闭飞行模式和/或 WiFi 来做到这一点。也许在设备处于飞行模式时启动应用程序,然后关闭应用程序并关闭飞行模式。这将打开 GPS 并强制发送位置更新。
回答by aslisabanci
I also wanted to test the relaunch of my terminated app which uses significant change monitoring. I wrote a piece of code so that it would display a local notification when it gets launched by a location key in the launch options dictionary.
我还想测试使用重大更改监控的已终止应用程序的重新启动。我编写了一段代码,以便在它通过启动选项字典中的位置键启动时显示本地通知。
I ran my app in the simulator. Then killed it from the multitasking bar. Then I set the location of the iOS simulator to a custom location. I quit the simulator and started it again. My app received the significant location update and showed the local notification.
我在模拟器中运行了我的应用程序。然后从多任务栏杀死它。然后我将 iOS 模拟器的位置设置为自定义位置。我退出模拟器并重新启动它。我的应用收到了重要的位置更新并显示了本地通知。
回答by Bardh Lohaj
I was struggling with the same issue, how to test 'startMonitoringSignificantLocationChanges' and check if my app is receiving location updates when suspended.
我正在努力解决同样的问题,如何测试“startMonitoringSignificantLocationChanges”并检查我的应用程序在暂停时是否收到位置更新。
I couldn't manage to catch the execution on a breakpoint but I managed to see the results of my implementation working by sending the new location data to the server.
我无法在断点处捕获执行,但我设法通过将新位置数据发送到服务器来查看我的实现结果。
The whole flow: - Implemented with 'startMonitoringSignificantLocationChanges' and an API call to my server to update the location latitude and longitude - Set the location updates background mode capability to true - Run the app so the location manager is initiated and the app is listening for location changes - Force closed the app - Set the debug->location on simulator to freeway drive - Opened maps to see if the location is changing - Waited on the server to see for location updates and was getting new results every about 3 minutes
整个流程: - 使用“startMonitoringSignificantLocationChanges”和对我的服务器的 API 调用来实现,以更新位置纬度和经度 - 将位置更新后台模式功能设置为 true - 运行应用程序,以便启动位置管理器并且应用程序正在侦听位置更改 - 强制关闭应用程序 - 将模拟器上的调试 -> 位置设置为高速公路驱动器 - 打开地图以查看位置是否正在更改 - 在服务器上等待位置更新并每大约 3 分钟获得一次新结果
However, I'm still not sure if this is fine enough on a real device.
但是,我仍然不确定这在真实设备上是否足够好。
I'm working on Xcode Version 6.0.1 (6A317); tested on Simulator iPhone 5s (8.0).
我正在使用 Xcode 版本 6.0.1 (6A317);在模拟器 iPhone 5s (8.0) 上测试。
回答by Eugene Tulushev
Adding to MagicSeth's answer, if you need to test this on a real device instead of a simulator, you can trigger a background launch with UIApplication.LaunchOptionsKey.location
key by disabling and re-enabling Location Services in General > Privacy screen in the Settings app.
添加到 MagicSeth 的答案中,如果您需要在真实设备而不是模拟器上进行测试,您可以UIApplication.LaunchOptionsKey.location
通过在“设置”应用程序的“常规”>“隐私”屏幕中禁用和重新启用定位服务来触发后台启动。
回答by Ke Sun
One thing I noticed in iOS 7 and Xcode 5.1.1 - If you are expecting SLC events to fire up your app into background mode, it may or may not hit the breakpoints you set. For me, sometimes the NSLog message are not even showing.
我在 iOS 7 和 Xcode 5.1.1 中注意到的一件事 - 如果您希望 SLC 事件将您的应用程序启动到后台模式,它可能会也可能不会达到您设置的断点。对我来说,有时甚至没有显示 NSLog 消息。
If that's the case for you, you can view NSLog outputs from the System Log. You can open the System Log from iOS Simulator's Debug menu.
如果是这种情况,您可以从系统日志中查看 NSLog 输出。您可以从 iOS 模拟器的调试菜单中打开系统日志。
回答by sliver
Depending on your scenario I would suggest two solutions:
根据您的情况,我会建议两种解决方案:
Use a Timer or LocalNotification that periodically calls stopMonitoringSignificantLocationChanges followed by startMonitoringSignificantLocationChanges which should trigger a new location to be sent to your code (might be the same Location as before).
Build your own GPS Simulator that you start in debug builds and that will call the same delegate methods like CLLocationManager would do.
使用 Timer 或 LocalNotification 定期调用 stopMonitoringSignificantLocationChanges 后跟 startMonitoringSignificantLocationChanges 这应该触发一个新位置发送到您的代码(可能与之前的位置相同)。
构建您自己的 GPS 模拟器,您可以在调试版本中启动该模拟器,该模拟器将调用与 CLLocationManager 相同的委托方法。
回答by Jane Sales
In iOS 4, you can register for significant location changes. From the Apple docs: With this service, location updates are generated only when the user's location changes significantly; thus, it is ideal for social applications or applications that provide the user with noncritical, location-relevant information. If the application is suspended when an update occurs, the system wakes it up in the background to handle the update. If the application starts this service and is then terminated, the system relaunches the application automatically when a new location becomes available. This service is available in iOS 4 and later, only on devices that contain a cellular radio.
在 iOS 4 中,您可以注册重大位置更改。来自 Apple 文档:使用此服务,仅当用户的位置发生显着变化时才会生成位置更新;因此,它非常适用于社交应用程序或为用户提供非关键位置相关信息的应用程序。如果应用程序在发生更新时挂起,系统会在后台唤醒它以处理更新。如果应用程序启动此服务然后终止,系统会在新位置可用时自动重新启动应用程序。此服务在 iOS 4 及更高版本中可用,仅适用于包含蜂窝无线电的设备。
See the Apple docs hereand here.
Here is some example code to register for signification location updates:
以下是一些用于注册表示位置更新的示例代码:
- (void)startSignificantChangeUpdates {
// Create the location manager if it doesn't exist
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
}
The docs say: if you leave this service running and your application is subsequently suspended or terminated, the service automatically wakes up your application when new location data arrives. At wake-up time, your application is put into the background and given a small amount of time to process the location data. Because your application is in the background, it should do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your application may be terminated.
文档说:如果您保持此服务运行并且您的应用程序随后被暂停或终止,则该服务会在新位置数据到达时自动唤醒您的应用程序。在唤醒时,您的应用程序被置于后台并有少量时间来处理位置数据。因为您的应用程序在后台,它应该做最少的工作并避免任何可能阻止它在分配的时间到期之前返回的任务(例如查询网络)。如果没有,您的申请可能会被终止。
回答by onmyway133
回答by Saurabh Passolia
Well, this is not possible as the application scope is limited to its own space and such kind of notification cannot be generated with Apple Documented APIs list. Of course.. if any undocumented API is used, application will get rejection from apple due to the use of undocumented/private API.
嗯,这是不可能的,因为应用程序范围仅限于它自己的空间,并且无法使用 Apple 文档化 API 列表生成此类通知。当然......如果使用任何未公开的API,应用程序将因使用未公开/私有API而被苹果拒绝。