requestAccessForMediaType 在 iOS 10 时崩溃

requestAccessForMediaType crashing in iOS 10

在我的应用程序中,我使用 card.io 扫描信用卡。它在 iOS 9 中工作正常。在 iOS 10 中,应用程序崩溃了,我在 xcode 8 beta 2 控制台中找不到崩溃日志,因为它会抛出大量垃圾消息。

然后我检查了隐私->设置,看看我的应用程序是否禁用了相机,但是我的应用程序没有在 section.Seems 中列出,iOS 10 没有授予我的许可使用相机的应用程序。

我使用以下代码请求权限:

-(BOOL)checkCameraPermissions{

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusAuthorized)
    {
        // start card-io
        return YES;
    }
    else if(authStatus == AVAuthorizationStatusNotDetermined)
    {

        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
         {
             if(granted)
             {
                 //Start card-io
                 [self testIsNewCard];
             }

         }];
    }
    else if (authStatus == AVAuthorizationStatusRestricted)
    {
        //Alert
        // Alert camera denied

        UIAlertController *aCon=[UIAlertController alertControllerWithTitle:@"Camera denied" message:@"Camera cannot be used" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok =[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [aCon dismissViewControllerAnimated:YES completion:nil];
        }];
        [aCon addAction:ok];
        [self presentViewController:aCon animated:YES completion:nil];

        return NO;

    }

    return NO;

}

当我 运行 此代码时,authStatus 返回为 AVAuthorizationStatusNotDetermined

并且应用程序在进入块后立即崩溃 requestAccessForMediaType:AVMediaTypeVideo

控制台中显示了太多垃圾日志,我不知道如何找到崩溃消息。

编辑: 我在 xcode 8 中找到了禁用所有不必要日志的选项。答案已发布 但仍然 xcode即使在禁用回溯调试后也没有显示任何崩溃日志。

我的 xcode8 刚刚显示此消息,应用程序刚刚退出:

  App[1124:226447] [access] <private>

我也尝试重置位置和隐私,但在尝试请求媒体访问时应用程序仍然崩溃。

知道为什么会这样吗?

我将 "Privacy - Camera Usage Description" 密钥 添加到我的 info.plist 文件中,现在可以使用了。

iOS 10 中,您必须声明对任何用户私有数据类型的访问权限。您可以通过向应用程序的 Info.plist 添加使用密钥来执行此操作。有关更多信息,请查看下面的屏幕截图。

您需要将 Privacy - Camera Usage Description 键添加到您应用的 Info.plist 及其使用信息中。

更多信息请查看下面的 GIF。

或者,如果您想通过 info.plist 添加,则需要添加 NSCameraUsageDescription 键。

只需复制并粘贴到 info.plist 中的字符串下方即可。

<key>NSCameraUsageDescription</key>
<string>Take the photo</string>

请找到下面的 GIF 以获取更多信息。

有关详细信息,请查看 link

iOS 10 延续了隐私政策并实施了新的隐私规则。我们应该记住在下一个项目中实施它们。

对于您的问题,您需要将以下行添加到 info.plist

<!--  Camera -->
<key>NSCameraUsageDescription</key>
<string><Your description goes here></string>

以下是隐私规则的其余部分:

<!--  Photo Library -->
<key>NSPhotoLibraryUsageDescription</key>
<string><Your description goes here></string>

<!--  Camera -->
<key>NSCameraUsageDescription</key>
<string><Your description goes here></string>

<!--  Microphone -->
<key>NSMicrophoneUsageDescription</key>
<string><Your description goes here></string>

<!--  Location -->
<key>NSLocationUsageDescription</key>
<string><Your description goes here></string>

<!--  Location When In Use -->
<key>NSLocationWhenInUseUsageDescription</key>
<string><Your description goes here></string>

<!--  Location Always -->
<key>NSLocationAlwaysUsageDescription</key>
<string><Your description goes here></string>

<!--  Calendars -->
<key>NSCalendarsUsageDescription</key>
<string><Your description goes here></string>

<!-- ⏰ Reminders -->
<key>NSRemindersUsageDescription</key>
<string><Your description goes here></string>

<!--  Motion -->
<key>NSMotionUsageDescription</key>
<string><Your description goes here></string>

<!--  Health Update -->
<key>NSHealthUpdateUsageDescription</key>
<string><Your description goes here></string>

<!--  Health Share -->
<key>NSHealthShareUsageDescription</key>
<string><Your description goes here></string>

<!-- ᛒ Bluetooth Peripheral -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string><Your description goes here></string>

<!--  Media Library -->
<key>NSAppleMusicUsageDescription</key>
<string><Your description goes here></string>

希望这对您有所帮助。 :)