From 51d1454d45e98f9ce1bff115aab4f808bdeb6fe1 Mon Sep 17 00:00:00 2001 From: Pham Cong Anh Date: Wed, 6 Apr 2022 17:39:11 +0700 Subject: [PATCH] add feature decideMediaCapturePermissions on ios15+ --- ios/Classes/InAppWebView/InAppWebView.swift | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/ios/Classes/InAppWebView/InAppWebView.swift b/ios/Classes/InAppWebView/InAppWebView.swift index f49fcef7..a5744e8d 100755 --- a/ios/Classes/InAppWebView/InAppWebView.swift +++ b/ios/Classes/InAppWebView/InAppWebView.swift @@ -1442,6 +1442,43 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi return result; } + + @available(iOS 15.0, *) + @available(macOS 12.0, *) + @available(macCatalyst 15.0, *) + public func webView(_ webView: WKWebView, + requestMediaCapturePermissionFor origin: WKSecurityOrigin, + initiatedByFrame frame: WKFrameInfo, + type: WKMediaCaptureType, + decisionHandler: @escaping (WKPermissionDecision) -> Void) { + onPermissionRequest(origin: origin, type: type, result: {(result) -> Void in + if result is FlutterError { + print((result as! FlutterError).message ?? "") + decisionHandler(.deny) + } + else if (result as? NSObject) == FlutterMethodNotImplemented { + decisionHandler(.deny) + } + else { + var response: [String: Any] + if let r = result { + response = r as! [String: Any] + var action = response["action"] as? Int + action = action != nil ? action : 0; +// var resources = response["resources"] as? [String] + switch action { + case 1: + decisionHandler(.grant) + break + default: + decisionHandler(.deny) + } + return; + } + decisionHandler(.deny) + } + }) + } @available(iOS 13.0, *) public func webView(_ webView: WKWebView, @@ -2374,6 +2411,25 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi channel?.invokeMethod("onNavigationResponse", arguments: navigationResponse.toMap(), result: result) } + @available(iOS 15.0, *) + @available(macOS 12.0, *) + @available(macCatalyst 15.0, *) + public func onPermissionRequest(origin: WKSecurityOrigin, type: WKMediaCaptureType, result: FlutterResult?) { + let resources: [String]; + switch type { + case .camera: + resources = ["camera"] + break; + case .microphone: + resources = ["microphone"] + break; + default: + resources = ["cameraAndMicrophone"] + } + let arguments: [String: Any] = ["origin": origin.protocol + "://" + origin.host, "resources": resources] + channel?.invokeMethod("onPermissionRequest", arguments: arguments, result: result) + } + public func onReceivedHttpAuthRequest(challenge: URLAuthenticationChallenge, result: FlutterResult?) { channel?.invokeMethod("onReceivedHttpAuthRequest", arguments: HttpAuthenticationChallenge(fromChallenge: challenge).toMap(), result: result)