我有一個應用程式,我需要在點擊一個按鈕時同時請求畫廊訪問和相機訪問。即一個接一個。我是 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 權限警報中選擇選項,則在選擇任何選項后立即出現相機警報。例如,如果我選擇有限選項(“選擇照片”),則在選擇照片視窗后立即出現相機警報。
如果我的問題中遺漏了任何內容,請告訴我。我希望現在更清楚了。
uj5u.com熱心網友回復:
首先將這些行添加到 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\"")
}
})
}
如何在按鈕或 viewdidload 等中使用它
PHPhotoLibrary.execute(controller: self, onAccessHasBeenGranted: {
// access granted...
})
uj5u.com熱心網友回復:
您可以一個接一個地添加相機和照片庫權限。你只需要在 info.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 {}
})
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/344262.html
上一篇:服務器SSH指紋驗證失敗
