我已经在应用程序中实现了 WKWebView。显示的网页中有一个文件输入,它应该从照片中导入图像。每当我按下该输入并选择“拍照”或“照片库”时,应用程序突然崩溃,我认为这是因为该应用程序缺少拍照或从库导入的权限。
当用户选择上述方法之一(拍照或照片库)时,如何推送权限请求?
我将 Swift 3.0 与 WKWebView 一起使用。
您还可以以编程方式请求访问权限,我更喜欢这种方式,因为在大多数情况下,您需要知道您是否获得了访问权限。
斯威夫特 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 {}
})
}
您不共享代码,因此我不确定这是否对您有用,但一般来说,将其用作最佳实践。
You have to add the below permission in Info.plist. More Referance
相机 :
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
照片 :
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo use
信息列表
有限的照片
<key>PHPhotoLibraryPreventAutomaticLimitedAccessAlert</key>
<true/>
相机
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) camera description.</string>
相片
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME)photos description.</string>
保存照片
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME) photos add description.</string>
地点
<key> NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) location description.</string>
苹果音乐
<key>NSAppleMusicUsageDescription</key>
<string>$(PRODUCT_NAME) My description about why I need this capability</string>
日历
<key>NSCalendarsUsageDescription</key>
<string>$(PRODUCT_NAME) My description about why I need this capability</string>
西里
<key>NSSiriUsageDescription</key>
<string>$(PRODUCT_NAME) My description about why I need this capability</string>
使用上面提到的 plist 设置和适当的访问器(AVCaptureDevice 或 PHPhotoLibrary),但如果您确实需要,也可以提醒它们并将它们发送到设置,如下所示:
斯威夫特 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)
}
}
}
文件: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>
https://i.stack.imgur.com/ER39g.png
要请求照片应用程序的权限,您需要添加以下代码(Swift 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>
我写了一个扩展,考虑了所有可能的情况:
如果允许访问,则将运行代码 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)
}
}))
DispatchQueue.main.async {
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\"")
}
})
}
在您的项目的 info.plist
中添加以下 keys
以及 Request Permissions
所需的 string
消息:
相片 :
Privacy - Photo Library Additions Usage Description
Privacy - Photo Library Usage Description
相机:
Privacy - Camera Usage Description
最好在 Apple 的技术文档中详细检查所有受保护资源(Bluetooth, Calendar, Camera & Microphone, Contacts Face ID , Location , Photos etc)
的学习内容。
https://developer.apple.com/documentation/technologies
转到上面的链接和搜索:
Property List Keys -> Bundle Resources -> Information Property List -> Protected resources
Quick Guide Video for implementation in your Project
谢谢!:)
在 Swift 5、iOS 13 中实现相机会话的好方法
https://github.com/egzonpllana/CameraSession
Camera Session 是一个 iOS 应用程序,它试图以最简单的方式实现 AVCaptureSession。
通过该应用程序,您可以找到已实现的这些相机会话:
用于拍照或录制视频的本机相机。
导入照片和视频的本地方式。
选择照片和视频等资产的自定义方式,可以从库中选择一个或多个资产。
自定义相机拍摄照片或视频,可选择按住按钮和录制。
分离的相机权限请求。
自定义相机功能,如手电筒和旋转相机选项。
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { response in
。不要忘记import AVFoundation
。