如何在iOS中同时请求相机和画廊的权限?



我有一个应用程序,我需要请求画廊访问和相机访问在同一时间点击一个按钮。一个接一个。我是swift的新手。有什么合适的方法吗?

这是我目前使用的代码:

func checkPermissions() {

if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
// Request read-write access to the user's photo library.
PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
switch status {
case .notDetermined:
// The user hasn't determined this app's access.
print("Not Determined")
self.showAlertToOpenSettings(type: 1)
case .restricted:
// The system restricted this app's access.
print("Resticted")
self.showAlertToOpenSettings(type: 1)
case .denied:
// The user explicitly denied this app's access.
print("Denied")
self.showAlertToOpenSettings(type: 1)
case .authorized:
// The user authorized this app to access Photos data.
print("authorised")
DispatchQueue.main.async {
self.openPhotoPicker()
}
case .limited:
// The user authorized this app for limited Photos access.
print("Limited")
DispatchQueue.main.async {
self.showLimitedLibraryAlert()
}
//                self.openLimitedLibrary()
@unknown default:
fatalError()
}
}
}
if UIImagePickerController.isSourceTypeAvailable(.camera) {
//Camera
AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
if response {
//access granted
} else {
self.showAlertToOpenSettings(type: 2)
}
}
}
}

这个问题是,如果用户从photoLibrary权限警报中选择选项,相机警报在选择任何选项后立即出现。例如,如果我选择限制选项("选择照片"),那么相机警报会在选择照片窗口后立即出现。

如果我的问题遗漏了什么,请让我知道。我希望现在更清楚了。

首先将这些行添加到info plist

<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access camera!.</string>

还有这个

<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access photos.</string>

然后在你的项目中添加这个类

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()
case .limited:
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()
case .limited:
onAuthorized()
@unknown default:
fatalError("PHPhotoLibrary::execute - "Unknown case"")
}
})
}

如何在button或viewdidload等内部使用

PHPhotoLibrary.execute(controller: self, onAccessHasBeenGranted: {
// access granted... 
})

您可以依次添加相机和照片库权限。你只需要添加这个关键信息。plist——

<key>NSCameraUsageDescription</key> <string>Mention reason to access camera</string>

<key>NSPhotoLibraryUsageDescription</key>
<string>Mention reason to access photo Library</string>

,一旦它被添加,你必须在Appdelegate或视图控制器中添加以下代码,你需要请求权限-

func askForCameraPermission() {
AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
if response {
//access granted
} else {
}
}        
}
func askForPhotoLibraryPermission() {
let photos = PHPhotoLibrary.authorizationStatus()
if photos == .notDetermined {
PHPhotoLibrary.requestAuthorization({status in
if status == .authorized{
//access granted
} else {}
})
}
}

最新更新