8

We are trying to make a conference call with multiple users, So by using Kurento server we have achieved this and it's working on safari browser. But when it comes to implementation in WebView / WKWebView. It does not even ask for permissions.

@IBOutlet weak var webViewContainer: UIView!
var webView: WKWebView!

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

    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.ignoresViewportScaleLimits = true
    webConfiguration.suppressesIncrementalRendering = true
    webConfiguration.allowsInlineMediaPlayback = true
    webConfiguration.allowsAirPlayForMediaPlayback = false
    webConfiguration.allowsPictureInPictureMediaPlayback = true
    webConfiguration.mediaTypesRequiringUserActionForPlayback = .all
    webConfiguration.requiresUserActionForMediaPlayback = true
    webView = WKWebView(frame: webViewContainer.frame, configuration: webConfiguration)
    webView.uiDelegate = self
    webView.navigationDelegate = self
    webView.sizeToFit()
    webView.backgroundColor = .black
    webView.isOpaque = false
    self.webViewContainer.addSubview(webView)

}

func webContentController()-> WKUserContentController {
    let contentController = WKUserContentController()
    let script = try! String(contentsOf: Bundle.main.url(forResource: "WebRTC", withExtension: "js")!, encoding: String.Encoding.utf8)
    contentController.addUserScript(WKUserScript(source: script, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true))
    contentController.add(self, name: "callbackHandler")
    return contentController
}

override func viewDidLoad() {
    super.viewDidLoad()
    guard let url = URL (string: urlStr) else { return
    }
    let myRequest = URLRequest(url: url)
    self.webView.load(myRequest)
}

I even have tried this link in safariViewController, but it does not ask for camera permissions.

Nasir Khan
  • 723
  • 4
  • 12
  • 24
  • I don't know why but [] and .all give diferent results: webConfiguration.mediaTypesRequiringUserActionForPlayback = [] webConfiguration.mediaTypesRequiringUserActionForPlayback = .all – Tiago Mendes Feb 11 '21 at 18:18

5 Answers5

4

This

Did you follow the steps from the documentation? The most important part is the NSCameraUsageDescription / NSMicrophoneUsageDescription must be present inside the info.plist file

Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
vk.edward.li
  • 1,899
  • 16
  • 22
  • Yes, it's present in plist – Nasir Khan Jun 18 '19 at 12:45
  • Are you using the latest iOS? The safariViewController camera problem should have been fixed in 11.3? Release notes: > **Safari Resolved Issues** > - WebApps saved to the home screen and webpages > in SafariViewController can now use the camera to capture images. > (35542231) – vk.edward.li Jun 19 '19 at 11:00
  • 1
    no, if you want to support lower version you have to use native code + a javascript bridge – vk.edward.li Jun 20 '19 at 12:36
  • @vk.edwaard.li can you share any example, or refer to the project that has done this? – Nasir Khan Jun 21 '19 at 06:17
1

This is a known limitation of WKWebView for now, see the chrome issue for details

Philipp Hancke
  • 15,855
  • 2
  • 23
  • 31
1

It's a Bug, the WkWebView has limited support to the WebRTC It's now working since IOS 14.3 version

But to get this working, you need to set properties requiresUserActionForMediaPlayback = false allowsInlineMediaPlayback = true

Daniel
  • 2,780
  • 23
  • 21
-1

I had to do this:

import AVFoundation

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        AVCaptureDevice.requestAccess(for: .audio) { haveMicAccess in
            print("Access to Microphone: \(haveMicAccess)")
        }

        return true
    }

    ...

}

It still asks me each time I use the camera with getUserMedia() in the page script too. But without the above code getUserMedia is null. The above code triggers the request for access to the microphone so long as you provide a message in the info.plist for NSMicrophoneUsageDescription

Conrad
  • 397
  • 1
  • 2
  • 12
-1

In my case, I need camera open for account identifier. Setting config for webview below work for me:

webConfiguration.allowsInlineMediaPlayback = true webConfiguration.mediaTypesRequiringUserActionForPlayback = []

Ho Luong
  • 726
  • 8
  • 12