5

I have the requirement that if my app is terminated from the background mode and after that if any significant location change occurs it should start in the background mode. That is what is exactly stated in the documents of startMonitoringSignificantLocationChanges

If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives. In such a case, the options dictionary passed to the application:didFinishLaunchingWithOptions: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.

So I put this code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

 if ([CLLocationManager significantLocationChangeMonitoringAvailable]) {
        // Stop normal location updates and start significant location change updates for battery efficiency.
        [viewController.locationManager stopUpdatingLocation];
        [viewController.locationManager startMonitoringSignificantLocationChanges];

        //NSLog(@"significantLocationChangeMonitoringAvailable.");
    }
    else {
        //NSLog(@"Significant location change monitoring is not available.");
    }
}

After that also its not being relaunch in background mode though I tried to change my location after roaming here and there.

What is I am missing or misleading here? that app is not being relaunch in background

Any suggestions?

EDIT 12July 2013

After so many trials I have figured out that app is being crash immediately after relaunching in background with the crash log

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib          0x3ba3feb4 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3ba40048 mach_msg + 36
2   CoreFoundation                  0x337ff040 __CFRunLoopServiceMachPort + 124
3   CoreFoundation                  0x337fdd9e __CFRunLoopRun + 878
4   CoreFoundation                  0x33770eb8 CFRunLoopRunSpecific + 352
5   CoreFoundation                  0x33770d44 CFRunLoopRunInMode + 100
6   Foundation                      0x34093f92 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 250
7  AppNm                        0x0010a6a4 0xe4000 + 157348
8   AppNm                       0x000efc2a 0xe4000 + 48170
9   AppNm                       0x000e9796 0xe4000 + 22422
10  CoreLocation                    0x33cd1064 -[CLLocationManager onClientEventLocation:] + 2244
11  CoreLocation                    0x33cc8eaa __CLClientInvokeCallback_block_invoke_0 + 62
12  CoreFoundation                  0x337ff6ae __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 10
13  CoreFoundation                  0x337fed80 __CFRunLoopDoBlocks + 156
14  CoreFoundation                  0x337fdca8 __CFRunLoopRun + 632
15  CoreFoundation                  0x33770eb8 CFRunLoopRunSpecific + 352
16  CoreFoundation                  0x33770d44 CFRunLoopRunInMode + 100
17  GraphicsServices                0x373492e6 GSEventRunModal + 70
18  UIKit                           0x356862fc UIApplicationMain + 1116
19  AppNm                       0x000e5854 0xe4000 + 6228
20  AppNm                       0x000e57a4 0xe4000 + 6052

Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 1:
0   libsystem_kernel.dylib          0x3ba40648 kevent64 + 24
1   libdispatch.dylib               0x3b970974 _dispatch_mgr_invoke + 792
2   libdispatch.dylib               0x3b970654 _dispatch_mgr_thread$VARIANT$mp + 32

Thread 2:
0   libsystem_kernel.dylib          0x3ba50d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3b99ecf6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3b99ea12 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3b99e8a0 start_wqthread + 4

Thread 3 name:  WebThread
Thread 3:
0   libsystem_kernel.dylib          0x3ba500fc __psynch_mutexwait + 24
1   libsystem_c.dylib               0x3b999124 pthread_mutex_lock + 388
2   WebCore                         0x39786418 _WebTryThreadLock(bool) + 184
3   WebCore                         0x3978634a WebRunLoopLock(__CFRunLoopObserver*, unsigned long, void*) + 42
4   CoreFoundation                  0x337ff6ca __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18
5   CoreFoundation                  0x337fd9bc __CFRunLoopDoObservers + 272
6   CoreFoundation                  0x337fdde8 __CFRunLoopRun + 952
7   CoreFoundation                  0x33770eb8 CFRunLoopRunSpecific + 352
8   CoreFoundation                  0x33770d44 CFRunLoopRunInMode + 100
9   WebCore                         0x39784500 RunWebThread(void*) + 440
10  libsystem_c.dylib               0x3b9a930e _pthread_start + 306
11  libsystem_c.dylib               0x3b9a91d4 thread_start + 4

Thread 4 name:  com.apple.NSURLConnectionLoader
Thread 4:
0   libsystem_kernel.dylib          0x3ba3feb4 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3ba40048 mach_msg + 36
2   CoreFoundation                  0x337ff040 __CFRunLoopServiceMachPort + 124
3   CoreFoundation                  0x337fdd9e __CFRunLoopRun + 878
4   CoreFoundation                  0x33770eb8 CFRunLoopRunSpecific + 352
5   CoreFoundation                  0x33770d44 CFRunLoopRunInMode + 100
6   Foundation                      0x340bd3d0 +[NSURLConnection(Loader) _resourceLoadLoop:] + 304
7   Foundation                      0x34140e80 __NSThread__main__ + 968
8   libsystem_c.dylib               0x3b9a930e _pthread_start + 306
9   libsystem_c.dylib               0x3b9a91d4 thread_start + 4

Thread 5 name:  com.apple.CFSocket.private
Thread 5:
0   libsystem_kernel.dylib          0x3ba50594 __select + 20
1   CoreFoundation                  0x338031f2 __CFSocketManager + 674
2   libsystem_c.dylib               0x3b9a930e _pthread_start + 306
3   libsystem_c.dylib               0x3b9a91d4 thread_start + 4

Thread 6:
0   libsystem_kernel.dylib          0x3ba50d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3b99ecf6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3b99ea12 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3b99e8a0 start_wqthread + 4

Thread 7:
0   libsystem_kernel.dylib          0x3ba50d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3b99ecf6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3b99ea12 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3b99e8a0 start_wqthread + 4

Thread 8:
0   libsystem_kernel.dylib          0x3ba50d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3b99ecf6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3b99ea12 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3b99e8a0 start_wqthread + 4

Thread 9:
0   libsystem_kernel.dylib          0x3ba50d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3b99ecf6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3b99ea12 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3b99e8a0 start_wqthread + 4

Thread 10:
0   libsystem_kernel.dylib          0x3ba50d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3b99ecf6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3b99ea12 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3b99e8a0 start_wqthread + 4

Unknown thread crashed with unknown flavor: 5, state_count: 1

From googling I found its because I am sending update location to released object. For that I changed the code of AppDelegate DidFinishlaunchingWithOptions

if ([CLLocationManager significantLocationChangeMonitoringAvailable]) {

//        if(!viewController.locationManager){
//            viewController.locationManager = [[CLLocationManager alloc] init];
//            viewController.locationManager.delegate = viewController;
//            viewController.locationManager.distanceFilter = 200.0f;
//            viewController.locationManager.desiredAccuracy = 200.0f;
//        }

        // Stop normal location updates and start significant location change updates for battery efficiency.
        viewController.locationManager.delegate = nil;
        [viewController.locationManager stopUpdatingLocation];
        viewController.locationManager.delegate = viewController;
        [viewController.locationManager startMonitoringSignificantLocationChanges];

        NSLog(@"significantLocationChangeMonitoringAvailable.");
    }
    else {

        NSLog(@"Significant location change monitoring is not available.");
    }

Is this the right way I am going? Or crash log indicates something else?

Heena
  • 2,348
  • 3
  • 32
  • 58
  • Configured your info.plist? Testing in the simulator or a device? – Wain Jul 10 '13 at 07:46
  • I have configured the info.plist with key `Required Background Modes` -> `App registers for location updates` – Heena Jul 10 '13 at 08:39
  • Testing with device.I have removed app from background also then after I roam around to get the location update.but didn't get my app relaunch in the background – Heena Jul 10 '13 at 08:40

2 Answers2

0

Right now your app's location behavior relies on your viewController being available. You've combined the "V" and "C" part of MVC. I would consider pulling the CLLocationManager out of your view controller and setting it as a property on your AppDelegate. That way you don't have to instantiate a view controller to react to location changes, and it establishes a pattern to access location elsewhere in your app. Configure your locationMananger first, then pass it into your viewController.

Drewsmits
  • 1,384
  • 2
  • 14
  • 24
0

Appdelegate

(void)applicationDidBecomeActive:(UIApplication *)application
{

    if ([CLLocationManager significantLocationChangeMonitoringAvailable]) {

        [viewController.locationManager stopMonitoringSignificantLocationChanges];
    }

    if ([CLLocationManager locationServicesEnabled])
    {

        [viewController.locationManager startUpdatingLocation];
    }

}

(void)applicationDidEnterBackground:(UIApplication *)application
{

    [viewController.locationManager stopUpdatingLocation];
    [viewController.locationManager startMonitoringSignificantLocationChanges];



}

ViewController.m

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.pausesLocationUpdatesAutomatically = NO;
locationManager.distanceFilter = 300;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
bummi
  • 27,123
  • 14
  • 62
  • 101
Gopal Raju
  • 246
  • 3
  • 12