ios Swift请求用户允许访问照片库

pod7payv  于 2024-01-09  发布在  iOS
关注(0)|答案(4)|浏览(232)

我正在尝试访问照片库,以便在swift 4中导入个人资料图片。
但我得到了这个错误:
此应用程序已崩溃,因为它试图访问没有使用说明的隐私敏感数据。应用程序的Info.plist必须包含NSPhotoLibraryUsageDescription键,该键具有向用户解释应用程序如何使用此数据的字符串值。
我以各种方式请求许可。
我试着:

<key>NSPhotoLibraryUsageDescription</key>
    <string>this permission for profile photo only</string>

字符串
代码:

let imagePicker = UIImagePickerController()
 override func viewDidLoad() {
     super.viewDidLoad()
     imagePicker.delegate = self
}
@objc func importUserImage() {
        print("import driver")

        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true)
    }


我试

<key>NSPhotoLibraryAddUsageDescription</key>
    <string>this permission for profile photo only</string>


我甚至试着用代码来做

import UIKit
import Photos
import UserNotifications

class LoginViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        PHPhotoLibrary.requestAuthorization { (status) in
            // cruch here
        }
     }
}


我删除了应用程序,并重新安装它,仍然崩溃
我是不是忘了什么?还是我做错了什么?

kupeojn6

kupeojn61#

试试这个:
进入你的plist。在最上面,它应该说信息属性列表选择了这个。然后右键单击并添加行类型隐私-照片库添加使用说明然后在右边有一个值列,需要你输入文本,这是用户将看到的。我希望这有帮助!

ih99xse1

ih99xse12#

NSPhotoLibraryUsageDescription应该是足够的,如果只是想访问用户的照片库和以下代码将工作正常

PHPhotoLibrary.requestAuthorization { (status) in
   // No crash
}

字符串
如果您需要读写权限,请将以下两个键添加到info.plist

<key>NSPhotoLibraryUsageDescription</key>
<string>this permission for profile photo only</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>this permission for profile photo only</string>

vcudknz3

vcudknz33#

我只是在项目/信息/自定义IOS目标属性中添加关键字NSPhotoLibraryUsageDescription。而不是info.plist,现在它工作正常

ntjbwcob

ntjbwcob4#

第一次添加隐私-照片库添加使用说明到您的信息.plist x1c 0d1x
然后你可以使用下面的代码

import SwiftUI
import Photos
struct ContentView: View {
    @State private var showingAlert = false
    @State private var authorizationStatus = PHAuthorizationStatus.notDetermined

    var body: some View {
        VStack {
            Text("\(authorizationStatus.rawValue)")
            
            Button(action: {
                PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
                    authorizationStatus = status
                    if status == .denied {
                        showingAlert = true
                    }
                }
            }) {
                Text("Hello, world!")
            }
            .alert(isPresented: $showingAlert) {
                Alert(
                    title: Text("Permission Denied"),
                    message: Text("You have denied access to the photo library. Please enable access in your settings if you want to use this feature."),
                    dismissButton: .default(Text("OK")) {
                        // Action to open the app settings
                        if let settingsURL = URL(string: UIApplication.openSettingsURLString),
                           UIApplication.shared.canOpenURL(settingsURL) {
                            UIApplication.shared.open(settingsURL)
                        }
                    }
                )
            }
        }
        .padding()
    }
}

  #Preview {
    ContentView()
   }

字符串

相关问题