XCode / iOS simulator: Trigger significant locatio

2019-01-10 04:16发布

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.

9条回答
太酷不给撩
2楼-- · 2019-01-10 04:57

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.

查看更多
再贱就再见
3楼-- · 2019-01-10 05:04

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.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-10 05:06

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.

See the Apple docs here and 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.

查看更多
再贱就再见
5楼-- · 2019-01-10 05:07

Freeway drive

enter image description here

// MARK: - MKMapViewDelegate

  func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    guard let location = userLocation.location else { return }

    print(location.speed)
  }
查看更多
走好不送
6楼-- · 2019-01-10 05:13

I was struggling with the same issue, how to test 'startMonitoringSignificantLocationChanges' and check if my app is receiving location updates when suspended.

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

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

查看更多
SAY GOODBYE
7楼-- · 2019-01-10 05:15

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.

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.

查看更多
登录 后发表回答