iOS WKWebview: Always allow camera permission
Asked Answered
P

4

6

Starting iOS 13 onwards Apple allows us to always remember the Allow camera permission flag in iOS Safari. (Referring this answer). Also iOS Twitter PWA has this feature too.

But is there anyway to enable it in WKWebview?

Anyone know how to achieve this?

Purtenance answered 25/2, 2021 at 5:48 Comment(0)
A
8

Add permission by default WebView Permission Decision Grant

Objective c :

- (void) webView:(WKWebView *)webView
     requestMediaCapturePermissionForOrigin:(WKSecurityOrigin *)origin
     initiatedByFrame:(WKFrameInfo *)frame type:(WKMediaCaptureType)type
     decisionHandler:(void (^)(WKPermissionDecision decision))decisionHandler
     API_AVAILABLE(ios(15.0))
{
    decisionHandler(WKPermissionDecisionGrant);
 
}

Swift :

func webView(_ webView: WKWebView, 
    requestMediaCapturePermissionFor 
    origin: WKSecurityOrigin,initiatedByFrame
    frame: WKFrameInfo,type: WKMediaCaptureType, 
    decisionHandler: @escaping (WKPermissionDecision) -> Void){
     decisionHandler(.grant)
 }
Anterior answered 23/6, 2022 at 11:28 Comment(5)
This works perfectly as intended, but please note a couple of fixes: decisionhandler should be decisionHandler (H uppercase) and .allow should be .grant in iOS 15 at least :-)Hobart
@Hobart , could you please tell me how you do it? I copied the exact answer with your comments, and didnt work... the app keeps asking to grant access for the camera.Proa
@KhrisVandal, assuming that you have the media permissions (i.e. camera, microphone) set in the Info.plist and that your app works fine once you grant the asked permissions, then simply copy the code with the 2 amendments inside the class ViewConotroller. Without this code, your web app would ask permission twice (Web app + iOS) initially, then once every following time. With this code, the user will not be prompted again for permission, assuming that the user has previously granted both permissions. I'm coding in iOS 15 only, I cannot say about backward compatibility. CheersHobart
In my case, It works adding "decideMediaCapturePermissionsFor". As it is in the Documentation: developer.apple.com/videos/play/wwdc2021/10032Proa
Not mentioned important things .... WKUIDelegate must be added in ViewController class , webView.uiDelegate = self and webViewConfiguration.allowsInlineMediaPlayback = true. Without these it is not working. See my answer: https://mcmap.net/q/1663078/-ios-wkwebview-always-allow-camera-permissionBootie
B
4

There must be met much more conditions than presented in existing answers any many other on stackoverflow. Im attaching simplified ViewController to show what everything must be added.

ViewController.swift

class ViewController: UIViewController, WKUIDelegate {
    var webView: WKWebView!;

    override func loadView() {
        super.loadView();

        let webViewConfiguration = WKWebViewConfiguration();
        webViewConfiguration.allowsInlineMediaPlayback = true;

        webView = WKWebView(frame:.zero , configuration: webViewConfiguration);

        webView.uiDelegate = self;

    }

    override func viewDidLoad() {
        super.viewDidLoad();

        webView.load(...);
    }

    @available(iOS 15.0, *)
    func webView(_ webView: WKWebView,
        decideMediaCapturePermissionsFor origin: WKSecurityOrigin,
        initiatedBy frame: WKFrameInfo,
        type: WKMediaCaptureType) async -> WKPermissionDecision {
            return .grant;
    }
}
Bootie answered 4/8, 2023 at 20:34 Comment(1)
This answer had all the details I needed to solve a similar problem, involving preventing a webpage loaded within an app from requesting microphone permissions every time it loaded.Valentinavalentine
H
0

@mikep's Answer is very detailed and up to date.

Just as a supplement, I would like to point out that web view WKUIDelegate method decideMediaCapturePermissionsFor is an async version equivalent to the requestMediaCapturePermissionFor method.

code from WKUIDelegate.h:

- (void)webView:(WKWebView *)webView requestMediaCapturePermissionForOrigin:(WKSecurityOrigin *)origin initiatedByFrame:(WKFrameInfo *)frame type:(WKMediaCaptureType)type decisionHandler:(void (^)(WKPermissionDecision decision))decisionHandler 
WK_SWIFT_ASYNC_NAME(webView(_:decideMediaCapturePermissionsFor:initiatedBy:type:)) WK_SWIFT_ASYNC(5) API_AVAILABLE(macos(12.0), ios(15.0));
Hautesalpes answered 17/10 at 8:14 Comment(0)
T
-1

There is no API for the PWA's to change it from "Ask" to "Allow". There is an issue on WebKit around this: https://bugs.webkit.org/show_bug.cgi?id=215884.

Temptress answered 24/8, 2021 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.