Embedding Whereby in iOS

Below are the recommended approaches to embed a Whereby room depending on the iOS version.

We offer native SDKs that allow you to tap into powerful features such as listening to room events and use custom buttons to send commands to the room from your application. Read more

iOS 14.5 and onwards

WKWebView supports embedding pages that use WebRTC from iOS 14.5 onwards. To access the microphone and camera, it is necessary to add both NSMicrophoneUsageDescription and NSCameraUsageDescription keys to the app’s Info.plist file.

import WebKit

class WKWebViewController: UIViewController, WKNavigationDelegate {

    public var roomUrlString = "" // Replace by your own
    private var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let config = WKWebViewConfiguration()
        config.allowsInlineMediaPlayback = true
        webView = WKWebView(frame: view.frame, configuration: config)
        webView.navigationDelegate = self
        view = webView
        guard let roomUrl = URL(string: roomUrlString) else {
            return
        }
        webView.load(URLRequest(url: roomUrl))
    }
}

iOS 14.3 and 14.4

For iOS 14.3 and 14.4 use SFSafariViewController to open a website containing an iframe with its src specified as a Whereby room, alongside a custom user interface:

import SafariServices

class ViewController: UIViewController, SFSafariViewControllerDelegate {

    public var roomUrlString = "" // Replace by your own
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        guard let roomUrl = URL(string: roomUrlString) else {
            return
        }
        let safariVC = SFSafariViewController(url: roomUrl)
        safariVC.delegate = self
        present(safariVC, animated: true)
    }
}

iOS 14.2 and earlier

Redirect to a browser (Safari by default) for iOS versions lower than 14.3:

import UIKit

class ViewController: UIViewController {

    public var roomUrlString = "" // Replace by your own
    
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let roomUrl = URL(string: roomUrlString),
            UIApplication.shared.canOpenURL(roomUrl) else {
            return
        }
        UIApplication.shared.open(roomUrl)
    }
}sw

Handling multiple iOS versions

Here is an example on how to handle different solutions, depending on the iOS version:

if #available(iOS 14.5, *) {
    // Use WKWebView
} else if #available(iOS 14.3, *) {
    // Use SFSafariViewController
} else {
    // Redirect to browser app
}

When the app is sent to background, the camera is disabled. If you need the microphone to continue working while the app is in the background, we recommend redirecting to Safari app.

To use Whereby with Cordova (Phonegap) please use the plugin for SafariViewController

Last updated