【问题标题】:iPhone - requesting Motion activityiPhone - 请求 Motion 活动
【发布时间】:2015-02-12 07:24:01
【问题描述】:

我正在开发一个使用 Motion Activity Manager 的 iOS 应用程序(更详细地说 - 计步器)。当应用程序启动时,我需要检查用户是否允许 Motion Activity。我通过这样做来做到这一点

_motionActivityManager = [[CMMotionActivityManager alloc] init];
_pedometer = [[CMPedometer alloc] init];
[_pedometer queryPedometerDataFromDate : [NSDate date]
                                toDate : [NSDate date]
                           withHandler : ^(CMPedometerData *pedometerData, NSError *error) {
                                   // BP1
                                    if (error != nil) {
                                        // BP2
                                    }
                                    else {
                                        // BP3
                                    }
                                }];

正如这里所讨论的 ☛ iOS - is Motion Activity Enabled in Settings > Privacy > Motion Activity

据我了解,此代码将触发“警报窗口”,要求用户选择加入/退出。

在我的情况下发生的情况是,当我运行应用程序第一次(又名。所有警告都被重置)时,应用程序在“BP1”之前挂起(回调永远不会执行),然后如果我停止应用程序使用 xCode 或按主页按钮出现“警报窗口”。如果我选择加入,一切都很好,在第二次运行时会达到“BP3”(如果我选择退出,则达到“BP2”)。

我尝试做的事情:

  • 我使用异步执行实现了另一种检查方式

    [_pedometer queryPedometerDataFromDate : [NSDate date]
                                    toDate : [NSDate date]
                               withHandler : ^(CMPedometerData *pedometerData, NSError *error) {
                                // Because CMPedometer dispatches to an arbitrary queue, it's very important
                                // to dispatch any handler block that modifies the UI back to the main queue.
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    authorizationCheckCompletedHandler(!error || error.code != CMErrorMotionActivityNotAuthorized);
                                });
    }];
    

这不会挂起应用程序,但永远不会显示“警报窗口”

  • 我稍后在代码中执行了这个“检查 sn-p” - 但又一次 - 应用程序挂起

【问题讨论】:

    标签: ios iphone xcode


    【解决方案1】:

    本质上,当第一个视图出现时,使用可以首先确保警报视图不会阻塞您的应用程序,即。在 onViewDidAppear 中。

    例如:

    -(void) viewDidAppear:(BOOL)animated {
        if ([MyActivityManager checkAvailability]) { // motion and activity availability checks
            [myDataManager checkAuthorization:^(BOOL authorized) { // is authorized
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (authorized) {
                        // do your UI update etc...
                    }
                    else {
                        // maybe tell the user that this App requires motion and tell him about activating it in settings...
                    }
                });
            }];
        }
    }
    

    这是我自己做的。我也将我的应用程序基于 Apple 示例代码,并注意到该示例也存在您所描述的问题。

    【讨论】:

    • 成功了 - 所以底线 - 在调用检查之前加载视图
    猜你喜欢
    • 2020-12-30
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    相关资源
    最近更新 更多