启动 CMMotionActivityManager 给 SIGABRT

Starting CMMotionActivityManager gives SIGABRT

我正在尝试检测本机 Xamarin.iOS 应用程序中的运动活动,运行在 iPhone 6s 上。我是这样启动管理器的:

var _manager = new CMMotionActivityManager();
_manager.StartActivityUpdates(new NSOperationQueue(), (activity) =>
{
    // do something with activity 
});

当我 运行 应用程序时,它因以下错误而失败:

2017-07-01 11:38:51.722 iosapp[1095:318780] critical: Native stacktrace:

2017-07-01 11:38:51.813 iosapp[1095:318780] critical: 0
libmonosgen-2.0.dylib 0x00000001007d5920 mono_handle_native_crash + 260 2017-07-01 11:38:51.813 iosapp[1095:318780] critical: 1 libsystem_platform.dylib
0x000000018ca1131c _sigtramp + 52 2017-07-01 11:38:51.813 iosapp[1095:318780] critical: 2 libsystem_kernel.dylib
0x000000018c9484d0 + 100 2017-07-01 11:38:51.813 iosapp[1095:318780] critical: 3 libsystem_kernel.dylib
0x000000018c9484fc system_set_sfi_window + 0 2017-07-01 11:38:51.813 iosapp[1095:318780] critical: 4 TCC
0x000000018fb64498 + 0 2017-07-01 11:38:51.813 iosapp[1095:318780] critical: 5 TCC
0x000000018fb643b8 + 0 2017-07-01 11:38:51.813 iosapp[1095:318780] critical: 6 TCC
0x000000018fb673d4 + 340 2017-07-01 11:38:51.813 iosapp[1095:318780] critical: 7 libxpc.dylib
0x000000018ca46f38 + 80 2017-07-01 11:38:51.814 iosapp[1095:318780] critical: 8 libxpc.dylib
0x000000018ca46ea8 + 40 2017-07-01 11:38:51.814 iosapp[1095:318780] critical: 9 libdispatch.dylib
0x000000018c80a9a0 + 16 2017-07-01 11:38:51.814 iosapp[1095:318780] critical: 10 libdispatch.dylib
0x000000018c8190d4 + 644 2017-07-01 11:38:51.814 iosapp[1095:318780] critical: 11 libdispatch.dylib
0x000000018c81aa50 + 540 2017-07-01 11:38:51.814 iosapp[1095:318780] critical: 12 libdispatch.dylib
0x000000018c81a7d0 + 124 2017-07-01 11:38:51.814 iosapp[1095:318780] critical: 13 libsystem_pthread.dylib
0x000000018ca13100 _pthread_wqthread + 1096 2017-07-01 11:38:51.814 iosapp[1095:318780] critical: 14 libsystem_pthread.dylib
0x000000018ca12cac start_wqthread + 4 2017-07-01 11:38:51.814 iosapp[1095:318780] critical: ================================================================= Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your

application.

我实例化管理器的方式有问题吗?

可能需要更多代码,但请尝试使用主 NSOperationQueue

public void StartMotionUpdates (Action<ActivityType> handler)
        {
            motionActivityMgr.StartActivityUpdates (NSOperationQueue.MainQueue, ((activity) => {
                handler (ActivityDataManager.ActivityToType (activity));
            }));
        }

注意*我在构造函数中初始化 motionActivityMgr = new CMMotionActivityManager (); 我要离开样本: https://github.com/xamarin/ios-samples/blob/master/ios8/MotionActivityDemo/MotionActivityDemo/ActivityDataManager.cs

已解决。这是 info.plist 的权限问题。

来自iOS Core Motion docs

Important An iOS app linked on or after iOS 10.0 must include usage description keys in its Info.plist file for the types of data it needs. Failure to include these keys will cause the app to crash. To access motion and fitness data specifically, it must include NSMotionUsageDescription.

在我将此密钥包含在 info.plist 之后:

<key>NSMotionUsageDescription</key>
<string>MotionActivity App needs Motion Access</string>

它就像一个魅力。