在iOS 10中,requestAccessForMediaType崩溃问题

20

我的应用程序中使用card.io来扫描信用卡。它在iOS 9上运行良好。但在iOS 10上,该应用程序会崩溃,我无法在xcode 8 beta 2控制台中找到崩溃日志,因为它抛出了很多垃圾消息。

之后,我检查了隐私->设置,看看相机是否被禁用了,但我的应用程序没有出现在那个部分中。似乎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>
我也尝试重置了位置和隐私设置,但是当尝试请求媒体访问时应用程序仍会崩溃。 有任何想法原因是什么?

你找到解决方案了吗? - Husein Behboudi Rad
不,我还在努力弄清楚它。 - Teja Nandamuri
就像你一样,我也没有收到任何崩溃日志!! - Husein Behboudi Rad
3个回答

27

我在我的info.plist文件中添加了"Privacy - Camera Usage Description" ,现在它可以正常工作。


之前我添加过这个但是上次没有成功,不过现在却成功了,有点奇怪。 - Teja Nandamuri
1
太棒了!iOS 10需要它。 - Castor

11
在中,您必须声明对任何用户私有数据类型的访问权限。您可以通过向应用程序的Info.plist添加使用键来实现此目的。有关详细信息,请查找下面的屏幕截图。
您需要向应用程序的Info.plist添加隐私-相机使用说明键以及其使用信息。
有关详细信息,请查看下面的GIF。 GIF 或者,如果您想通过info.plist添加,则需要添加NSCameraUsageDescription密钥。
只需将以下字符串复制并粘贴到info.plist中即可。
<key>NSCameraUsageDescription</key>
<string>Take the photo</string>
请查看下方GIF以获取更多信息。 GIF 欲了解更多信息,请参阅链接

8

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>

希望这能帮到您。 :)

如果有多个相机使用,比如我们有QR码和自拍功能,是否足够说明“此应用程序使用相机进行多种功能”? - byJeevan
是的,就像你请求位置权限一样,你需要定义一次,然后可以多次用于多个目的,你需要为你的权限提供通用描述。 - Aadil Keshwani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接