ios - 如何像 'Moment' 应用程序那样跟踪 iPhone 使用情况?

标签 ios core-location cllocation

如何跟踪 iPhone 的总使用情况,即使我们的应用程序在后台运行(未强制终止)。最近我遇到了应用程序,即 Moment此应用程序会跟踪您的 iPhone 使用情况,即使此应用程序在后台运行也是如此。实际上他们正在使用定位服务来获取执行时间。我的问题是当他们获得执行时间时,他们如何确定用户的 iPhone 屏幕是锁定还是解锁?

我有代码来检查屏幕是锁定还是解锁

-(void)registerAppforDetectLockState {

    int notify_token;
    notify_register_dispatch("com.apple.springboard.lockstate", &notify_token,dispatch_get_main_queue(), ^(int token) {

        uint64_t state = UINT64_MAX;
        notify_get_state(token, &state);

        if(state == 0) {
            NSLog(@"unlock device");

            UIAlertView *errorAlert = [[UIAlertView alloc]
                                       initWithTitle:@"unlock device" message:@"test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [errorAlert show];


        } else {
            NSLog(@"lock device");

            UIAlertView *errorAlert = [[UIAlertView alloc]
                                       initWithTitle:@"lock device" message:@"test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [errorAlert show];

        }

    });
}

但此代码仅在应用程序在前台运行时有效。

最佳答案

您的代码仅在应用程序在前台运行时才有效,因为当应用程序进入后台时,它不会被调用,您必须使用 background mode 之一唤醒您的应用程序可以执行您的代码。

关于ios - 如何像 'Moment' 应用程序那样跟踪 iPhone 使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816470/

相关文章:

ios - 我可以在后台使用 startupdatinglocation 吗?

objective-c - 您如何在没有用户共享/授予权限的情况下使用 Core Location?

iphone - iOS:更新后核心位置关闭?

iphone - C定位速度

ios - swift 3 : reverseGeocodeLocation does not call its completion handler

ios - 如何使用Frank Cucumber从UIImagePickerController选择图片?

iphone - 如何在 UINavigationController 上方添加 View ?

swift - 计算两个位置之间的方位

ios - "Only valid bearer authentication supported"- 错误 400

ios - 如何在UIImageView中设置透明背景色