Swift 请求用户访问照片库的许可 [英] Swift Ask for user’s permission to access the photo library

查看:39
本文介绍了Swift 请求用户访问照片库的许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问照片库以在 swift 4 中导入个人资料图片.

I'm trying to access the photo library to import a profile image in swift 4.

但是我遇到了这个错误:

but i got this error :

此应用已崩溃,因为它试图访问对隐私敏感的没有使用说明的数据.应用程序的 Info.plist 必须包含一个 NSPhotoLibraryUsageDescription 键,带有解释的字符串值向用户说明应用如何使用这些数据.

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

我以各种方式请求许可.

I asked for permission in every single way.

我试试:

<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>

我什至尝试在代码中做到这一点

I even try to do it in code

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
        }
     }
}

我删除了应用程序并重新安装它,仍然崩溃

I deleted the app and reinstall it , still crash

我是不是忘记了什么?还是我做错了什么?

Did i forget something? or what i'm doing wrong ?

推荐答案

我只是在 Project/info/Custom IOS Target Properties 中添加了键 NSPhotoLibraryUsageDescription .而不是 info.plist,现在它工作正常

i just add the key NSPhotoLibraryUsageDescription in Project/info/Custom IOS Target Properties . instead of info.plist and now it's working fine

这篇关于Swift 请求用户访问照片库的许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆