我已经在应用程序中实现了WKWebView。在显示的网页中有一个文件输入,它应该从照片中导入图像。每当我按该输入并选择“拍摄照片”或“照片图库”时,应用程序都会突然崩溃,我认为这是因为该应用程序缺少拍摄照片或从图库中导入的权限。
当用户选择上述方法之一(拍摄照片或照片库)时,如何发送许可请求?
我将Swift 3.0与WKWebView一起使用。
我已经在应用程序中实现了WKWebView。在显示的网页中有一个文件输入,它应该从照片中导入图像。每当我按该输入并选择“拍摄照片”或“照片图库”时,应用程序都会突然崩溃,我认为这是因为该应用程序缺少拍摄照片或从图库中导入的权限。
当用户选择上述方法之一(拍摄照片或照片库)时,如何发送许可请求?
我将Swift 3.0与WKWebView一起使用。
Answers:
摄影机
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
照片:
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo use
您还可以通过编程方式请求访问权限,我更喜欢这样做,因为在大多数情况下,您需要知道是否获得访问权限。
Swift 4更新:
//Camera
AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
if response {
//access granted
} else {
}
}
//Photos
let photos = PHPhotoLibrary.authorizationStatus()
if photos == .notDetermined {
PHPhotoLibrary.requestAuthorization({status in
if status == .authorized{
...
} else {}
})
}
您不共享代码,所以我不确定这是否对您有用,但是一般而言,将其用作最佳实践。
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { response in
。别忘了import AVFoundation
。
文件:Info.plist
相机
<key>NSCameraUsageDescription</key>
<string>camera description.</string>
相片
<key>NSPhotoLibraryUsageDescription</key>
<string> photos description.</string>
保存照片
<key>NSPhotoLibraryAddUsageDescription</key>
<string> photos add description.</string>
位置
<key> NSLocationWhenInUseUsageDescription</key>
<string> location description.</string>
苹果音乐:
<key>NSAppleMusicUsageDescription</key>
<string>My description about why I need this capability</string>
日历
<key>NSCalendarsUsageDescription</key>
<string>My description about why I need this capability</string>
西里
<key>NSSiriUsageDescription</key>
<string>My description about why I need this capability</string>
使用上面提到的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)
}
}
}
要获得照片应用的许可,您需要添加以下代码(快速3):
PHPhotoLibrary.requestAuthorization({
(newStatus) in
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
}
})
<key>NSPhotoLibraryUsageDescription</key> <string>You can select photos to attach to reports.</string>
info.plist
我编写了一个扩展程序,其中考虑了所有可能的情况:
onAccessHasBeenGranted
将运行。requestAuthorization(_:)
则将被调用。用法示例:
PHPhotoLibrary.execute(controller: self, onAccessHasBeenGranted: {
// access granted...
})
扩展代码:
import Photos
import UIKit
public extension PHPhotoLibrary {
static func execute(controller: UIViewController,
onAccessHasBeenGranted: @escaping () -> Void,
onAccessHasBeenDenied: (() -> Void)? = nil) {
let onDeniedOrRestricted = onAccessHasBeenDenied ?? {
let alert = UIAlertController(
title: "We were unable to load your album groups. Sorry!",
message: "You can enable access in Privacy Settings",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { _ in
if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(settingsURL)
}
}))
controller.present(alert, animated: true)
}
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .notDetermined:
onNotDetermined(onDeniedOrRestricted, onAccessHasBeenGranted)
case .denied, .restricted:
onDeniedOrRestricted()
case .authorized:
onAccessHasBeenGranted()
@unknown default:
fatalError("PHPhotoLibrary::execute - \"Unknown case\"")
}
}
}
private func onNotDetermined(_ onDeniedOrRestricted: @escaping (()->Void), _ onAuthorized: @escaping (()->Void)) {
PHPhotoLibrary.requestAuthorization({ status in
switch status {
case .notDetermined:
onNotDetermined(onDeniedOrRestricted, onAuthorized)
case .denied, .restricted:
onDeniedOrRestricted()
case .authorized:
onAuthorized()
@unknown default:
fatalError("PHPhotoLibrary::execute - \"Unknown case\"")
}
})
}
在执行相机会话的好方法斯威夫特5,iOS版13
https://github.com/egzonpllana/CameraSession
Camera Session是一个iOS应用程序,试图使实现AVCaptureSession的最简单方法成为可能。
通过该应用程序,您可以找到实现的这些摄像头会话:
自定义相机功能包括割炬和旋转相机选项。