Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 我们可以在位置更改时在后台重新启动应用程序吗?在强制退出后台后_Iphone_Objective C_Core Location_Cllocationmanager_Cllocation - Fatal编程技术网

Iphone 我们可以在位置更改时在后台重新启动应用程序吗?在强制退出后台后

Iphone 我们可以在位置更改时在后台重新启动应用程序吗?在强制退出后台后,iphone,objective-c,core-location,cllocationmanager,cllocation,Iphone,Objective C,Core Location,Cllocationmanager,Cllocation,我有一个要求,如果我的应用程序从后台模式终止,并且在此之后,如果发生任何重大的位置变化,它应该在后台模式下启动。这正是startMonitoringSignificantLocationChanges 如果您启动此服务,并且您的应用程序随后 终止后,系统会自动将应用程序重新启动到 新事件到达时的背景。在这种情况下,选项 传递给应用程序的字典:didFinishLaunchingWithOptions: 应用程序委托的方法包含密钥 UIApplicationLaunchActionSlocatio

我有一个要求,如果我的应用程序从后台模式终止,并且在此之后,如果发生任何重大的位置变化,它应该在后台模式下启动。这正是
startMonitoringSignificantLocationChanges

如果您启动此服务,并且您的应用程序随后 终止后,系统会自动将应用程序重新启动到 新事件到达时的背景。在这种情况下,选项 传递给应用程序的字典:didFinishLaunchingWithOptions: 应用程序委托的方法包含密钥 UIApplicationLaunchActionSlocationKey,指示您的 应用程序因位置事件而启动。重新启动后, 您仍然必须配置位置管理器对象并调用此 方法继续接收位置事件。重新启动时 位置服务,当前事件将传递给您的代表 马上。此外,您所在位置的location属性 使用最近的位置对象填充管理器对象 在开始定位服务之前

所以我把这个密码

- (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.");
    }
}
在此之后,它也不会在后台模式下重新启动,尽管我在四处漫游后试图更改我的位置

我在这里遗漏或误导了什么?该应用程序不会在后台重新启动

有什么建议吗

编辑2013年7月12日

经过这么多的试用,我发现在后台用崩溃日志重新启动后,应用程序立即崩溃

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
通过谷歌搜索,我找到了它,因为我正在向发布的对象发送更新位置。 为此,我更改了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.");
    }

这是我要走的路吗?或者崩溃日志表明了其他情况?

现在,你的应用程序的位置行为取决于你的
viewController
是否可用。您已经结合了MVC的“V”和“C”部分。我会考虑从您的视图控制器中取出 CalOpCaseMeals<代码>,并将其设置为AppDe委托上的属性。这样,您就不必实例化视图控制器来对位置更改做出反应,并且它可以建立一个模式来访问应用程序中其他位置的位置。首先配置您的
位置管理器
,然后将其传递到您的
视图控制器

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];

配置了你的info.plist?在模拟器或设备中测试?我已将info.plist配置为键
所需的后台模式
->
应用程序注册以进行位置更新
使用设备测试。在我四处漫游以获取位置更新后,我也已从后台删除了应用程序。但没有在后台重新启动我的应用程序