我在应用中实现了WKWebView。 在显示的网页中有一个文件输入,它应该从照片中导入图像。 每当我按下那个输入并选择"拍照"或者"照片库"应用程序突然崩溃,我认为这是因为该应用程序缺少从照片库中导入照片或导入的权限。
当用户选择上述某种方法(拍照或照片库)时,如何推送权限请求?
我使用Swift 3.0和WKWebView。
答案 0 :(得分:94)
你也可以以编程方式请求访问,我更喜欢这种访问,因为在大多数情况下你需要知道你是否接受了访问。
Swift 4更新:
SELECT batchno, MAX(id)
FROM batchtracing
WHERE contentid = '7434'
GROUP BY batchno
您不共享代码,因此我无法确定这对您是否有用,但一般来说将其用作最佳做法。
答案 1 :(得分:89)
您必须在Info.plist中添加以下权限。
相机:
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
照片:
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo use
答案 2 :(得分:22)
文件:Info.plist
相机
C
照片强>
<key>NSCameraUsageDescription</key>
<string>camera description.</string>
保存照片
<key>NSPhotoLibraryUsageDescription</key>
<string> photos description.</string>
位置强>
<key>NSPhotoLibraryAddUsageDescription</key>
<string> photos add description.</string>
Apple Music:
<key> NSLocationWhenInUseUsageDescription</key>
<string> location description.</string>
<强>日历强>
<key>NSAppleMusicUsageDescription</key>
<string>My description about why I need this capability</string>
<强> Siri的强>
<key>NSCalendarsUsageDescription</key>
<string>My description about why I need this capability</string>
答案 3 :(得分:19)
文件:Info.plist
相机:
<key>NSCameraUsageDescription</key>
<string>You can take photos to document your job.</string>
对于照片库,您会希望这个允许应用用户浏览照片库。
<key>NSPhotoLibraryUsageDescription</key>
<string>You can select photos to attach to reports.</string>
答案 4 :(得分:18)
使用上面提到的plist设置和相应的访问器(AVCaptureDevice或PHPhotoLibrary),但如果您确实需要,也要提醒他们并将其发送到设置,如下所示:
Swift 4.0和4.1
func proceedWithCameraAccess(identifier: String){
// handler in .requestAccess is needed to process user's answer to our request
AVCaptureDevice.requestAccess(for: .video) { success in
if success { // if request is granted (success is true)
DispatchQueue.main.async {
self.performSegue(withIdentifier: identifier, sender: nil)
}
} else { // if request is denied (success is false)
// Create Alert
let alert = UIAlertController(title: "Camera", message: "Camera access is absolutely necessary to use this app", preferredStyle: .alert)
// Add "OK" Button to alert, pressing it will bring you to the settings app
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
}))
// Show the alert with animation
self.present(alert, animated: true)
}
}
}
答案 5 :(得分:10)
Swift 4
无需以编程方式添加权限的最简单方法是打开info.plist文件并选择“信息属性”列表旁边的+,然后在下拉列表中滚动到“隐私”选项,并选择“隐私相机使用说明”以访问相机或隐私照片库用法说明访问照片库。在您选择此选项后,请在右侧填写字符串值,以便在警报弹出请求权限时包含您希望向用户显示的文本。
答案 6 :(得分:9)
要获得照片应用的许可,您需要添加此代码(Swift 3):
PHPhotoLibrary.requestAuthorization({
(newStatus) in
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
}
})
答案 7 :(得分:3)
我编写了一个扩展程序,其中考虑了所有可能的情况:
RETURN
CASE WHEN LENGTH(csv) - LENGTH(REPLACE(csv, ';', '')) + 1 >= pos
THEN substring_index(substring_index(csv, ';', pos), ';', -1)
ELSE ''
END
。onAccessHasBeenGranted
。用法示例:
requestAuthorization(_:)
扩展代码:
PHPhotoLibrary.execute(controller: self, onAccessHasBeenGranted: {
// access granted...
})
答案 8 :(得分:0)
在 Swift 5 , iOS 13
中实现Camera会话的好方法https://github.com/egzonpllana/CameraSession
Camera Session是一个iOS应用,它试图以最简单的方式来实现AVCaptureSession。
通过该应用,您可以找到已实施的这些摄像头会话:
自定义相机功能,例如手电筒和旋转相机选项。