英文:
How to prompt user for photo library change authorization in UIKit?
问题
I am new to programming and iOS development and I am trying to build an onboarding flow for my app.
我是新手程序员,正在尝试为我的应用程序构建引导流程。
I would like to ask the user for permission to add images in their photo library when they use the app for the first time.
我想在用户首次使用应用程序时向他们请求在其照片库中添加图片的权限。
I have a working code, but it is only fired when the user wants to save their first image and I would like to move the authorization request to the onboarding flow without saving any images.
我有一个可工作的代码,但它只在用户想要保存第一张图片时触发,我想将授权请求移到引导流程中,而不保存任何图片。
I don't need access to see the other images in their photo library, just permission to add new images.
我不需要访问他们照片库中的其他图片,只需要添加新图片的权限。
I would like to avoid directing the user out of my app to the settings because this is the first time they will be prompted for authorization.
我想避免将用户引导到应用程序设置之外,因为这是他们首次被要求授权。
This is the current working code, but for this code I need an image to be saved:
以下是当前的工作代码,但对于这个代码,我需要保存一张图片:
import UIKit
import PhotosUI
if image != nil {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image!)
}, completionHandler: { success, error in
})
}
I tried to modify it for the onboarding flow to avoid saving an image at the time of the permission request:
我尝试修改它以用于引导流程,以避免在请求权限时保存图像:
In UIView setup:
在UIView设置中:
button.addTarget(self, action: #selector(askAuthorization), for: .touchUpInside)
Outside of UIView setup:
在UIView设置之外:
@objc func askAuthorization() async {
let status = PHPhotoLibrary.authorizationStatus(for: .addOnly)
// If the system hasn't determined the user's authorization status, explicitly prompt them for approval.
if status == .notDetermined {
await PHPhotoLibrary.requestAuthorization(for: .addOnly)
}
}
The authorization request pops up correctly, but the app crashes due to memory issues immediately after selecting OK.
授权请求弹出正确,但在选择“好”后,应用程序立即崩溃,因为内存问题。
英文:
I am new to programming and iOS development and I am trying to build an onboarding flow for my app.
I would like to ask the user for permission to add images in their photo library when they use the app for the first time.
I have a working code, but it is only fired when the user wants to save their first image and I would like to move the authorization request to the onboarding flow without saving any images.
I don't need access to see the other images in their photo library, just permission to add new images.
I would like to avoid directing the user out of my app to the settings, because this is the first time they will be prompted for authorization.
This is the current working code, but for this code I need an image to be saved:
import UIKit
import PhotosUI
if image != nil {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image!)}, completionHandler: {success, error in
})
}
I tried to modify it for the onboarding flow to avoid saving an image at the time of the permission request:
In UIView setup:
button.addTarget(self, action: #selector(askAuthorization), for: .touchUpInside)
Outside of UIView setup:
@objc func askAuthorization() async {
let status = PHPhotoLibrary.authorizationStatus(for: .addOnly)
// If the system hasn't determined the user's authorization status, explicitly prompt them for approval.
if status == .notDetermined {
await PHPhotoLibrary.requestAuthorization(for: .addOnly)
}
}
The authorization request pops up correctly, but the app crashes due to memory issues immediately after selecting OK.
答案1
得分: 0
您的问题是您正在将异步操作传递给 UIButton.addTarget(_:action:for)
。请将 askAuthorization()
方法改为同步,并调用 requestAuthorization(for:handler:)
方法的非异步版本。
@objc func askAuthorization() {
let status = PHPhotoLibrary.requestAuthorization(for: .addOnly)
print("status", status.rawValue)
}
或者,您可以保留 requestAuthorization(for:handler:)
的异步版本,如下所示:
@objc func askAuthorization() {
Task {
await askAuthorizationAsync()
}
}
func askAuthorizationAsync() async {
let status = await PHPhotoLibrary.requestAuthorization(for: .addOnly)
print("status", status.rawValue)
}
英文:
Your problem is that you are passing an async action to UIButton.addTarget(_:action:for)
. Make askAuthorization()
synchronous and call the non-async version of the requestAuthorization(for:handler:)
method.
@objc func askAuthorization() {
PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
print("status", status.rawValue)
}
}
Alternatively, you can keep the async version of requestAuthorization(for:handler:)
like so:
@objc func askAuthorization() {
Task {
await askAuthorizationAsync()
}
}
func askAuthorizationAsync() async {
let status = await PHPhotoLibrary.requestAuthorization(for: .addOnly)
print("status", status.rawValue)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论