在 iPad 中启用相机限制会在打开相机时引发 NSException
Posted
技术标签:
【中文标题】在 iPad 中启用相机限制会在打开相机时引发 NSException【英文标题】:Enabling restrictions for camera in iPad throws NSException while opening camera 【发布时间】:2014-02-19 09:51:43 【问题描述】:我在 iPad 的设置和隐私选项卡中启用了限制并禁止使用相机和 FaceTime,我为我的应用程序禁用了对照片的访问。
我正在使用以下代码打开相机和照片库,
//For camera
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing = NO;
[self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];
//For Photo library
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = NO;
[self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];
在打开照片库时,它会清楚地显示一条访问被拒绝的消息,并且可以在设置应用程序中进行更改。但是在打开相机时,应用程序崩溃并抛出错误,
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'
*** First throw call stack:
(0x30584f4b 0x3ac566af 0x32f56d69 0x1141d9 0x32ee4d03 0x32d2c713 0x32d2c6b3 0x32d2c691 0x32d1811f 0x32d2c107 0x32d2bdd9 0x32d26e65 0x32cfc79d 0x32cfafa3 0x30550183 0x3054f653 0x3054de47 0x304b8c27 0x304b8a0b 0x3518c283 0x32d5c049 0xeff7d 0x3b15eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
如何解决这个问题?
【问题讨论】:
【参考方案1】:您应该始终检查源类型是否可用:
来自 Apple UIImagePickerController
doucmentatiom
验证设备是否能够从所需的内容中挑选内容 来源。调用
isSourceTypeAvailable:
类方法执行此操作, 提供来自UIImagePickerControllerSourceType
的常量 枚举。
检查源是否可用,例如:
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
// UIImagePickerControllerSourceTypePhotoLibrary is available.
【讨论】:
以上是关于在 iPad 中启用相机限制会在打开相机时引发 NSException的主要内容,如果未能解决你的问题,请参考以下文章
在 Xcode 中打开 Popover 选项以在 iPad 上选择相机或照片库时出错
Swift 4 相机视图,为啥这会在 iPad 而不是 iPhone 上崩溃?