ios - 从 WKWebView 打开照片库或相机?

标签 ios swift camera wkwebview photo

我有一个包含 WKWebView 的应用程序,在 webview 中,我们具有从照片库附加图像的功能。当从 Safari 或 Chrome 打开页面时,这个东西工作正常,但在我的应用程序中照片选项没有打开。

我已经搜索了几个答案,但他们告诉我要注入(inject)自定义 javascript 来附加一个监听器。但没有任何效果。

最佳答案

您需要在代码中手动授予该应用程序权限。以便您的 WKWebView 可以打开照片库。

在您的 Info.plist 文件中添加以下两个键:

<key>NSPhotoLibraryUsageDescription</key>
<string>Needs permission to select photos</string>
<key>NSCameraUsageDescription</key>
<string>Needs permission to take photos</string>

并在 WKWebView 中加载图像附件功能之前添加以下代码以询问照片权限(不要忘记 import Photos 框架):

    let status = PHPhotoLibrary.authorizationStatus()
    if status == .authorized {
        print("already authorized")
    } else {
        PHPhotoLibrary.requestAuthorization({(_ status: PHAuthorizationStatus) -> Void in
            switch status {
            case .authorized:
                print("Authorized")
            case .denied:
                print("Denied")
            case .notDetermined:
                print("Not determined")
            case .restricted:
                print("Restricted")
            @unknown default:
                print("Unknown")
            }
        })
    }

关于ios - 从 WKWebView 打开照片库或相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58968434/

相关文章:

ios - 不了解 NavigationController 和 UIViewControllers 在 iOS 中的工作方式

swift - 为什么 dateformatter.date() 默认返回 UTC?

ios - 如何在 SwiftUI 中添加背景图像以完成 View

c# - 用于扫描仪颜色意图的 WIA Automation 不工作

ios - 调试为什么 SKScene init(fileNamed :) returns nil

ios - UIPageControl 具有未指定的灰色背景色

ios - 检查给定数字的范围

swift - 指定多态函数的类型以将其作为参数传递

ios - 相机图像选择器和摇动手势?

image-processing - card.io 图像处理是如何工作的?