iOSWebView

class : IWebView

Namespace: Vuplex.WebView

iOSWebView is the IWebView implementation used by 3D WebView for iOS. It also includes additional APIs for iOS-specific functionality.

Summary

Public methods

GetNativeWebView

IntPtr GetNativeWebView()

Returns a pointer to the instance's native Objective-C WKWebView.

Objective-C Example

// Example of defining a native Objective-C function that sets WKWebView.allowsLinkPreview.
// Place this in a .m file in your project, like Assets/Plugins/WebViewCustom.m
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>

void WebViewCustom_SetAllowsLinkPreview(WKWebView *webView, BOOL allowsLinkPreview) {

    webView.allowsLinkPreview = allowsLinkPreview;
}

C# Example

// Example of calling the native Objective-C function from C#.
async void EnableLinkPreviews(WebViewPrefab webViewPrefab) {

    await webViewPrefab.WaitUntilInitialized();
    #if UNITY_IOS && !UNITY_EDITOR
        var wkWebViewPtr = (webViewPrefab.WebView as iOSWebView).GetNativeWebView();
        WebViewCustom_SetAllowsLinkPreview(wkWebViewPtr, true);
    #endif
}

[System.Runtime.InteropServices.DllImport("__Internal")]
static extern void WebViewCustom_SetAllowsLinkPreview(System.IntPtr webViewPtr, bool allowsLinkPreview);

Important note

Adding code that interacts with the native WKWebView directly may interfere with 3D WebView's functionality and vice versa. So, it's highly recommended to stick to 3D WebView's C# APIs whenever possible and only use GetNativeWebView() if truly necessary. If 3D WebView is missing an API that you need, feel free to contact us.

SetAllowsBackForwardNavigationGestures

void SetAllowsBackForwardNavigationGestures(bool allow)

Sets whether horizontal swipe gestures trigger backward and forward page navigation. The default is false.

Example

await webViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
    var iOSWebViewInstance = webViewPrefab.Webview as iOSWebView;
    iOSWebViewInstance.SetAllowsBackForwardNavigationGestures(true);
#endif

SetAllowsInlineMediaPlayback

static void SetAllowsInlineMediaPlayback(bool allow)

Sets whether HTML5 videos play inline or use the native full-screen controller. The default is true. This method is static because the WKWebView's configuration cannot be modified at runtime after the webview is created.

Example

#if UNITY_IOS && !UNITY_EDITOR
    iOSWebView.SetAllowsInlineMediaPlayback(false);
#endif

SetCameraEnabled

static void SetCameraEnabled(bool enabled)

Like Web.SetCameraAndMicrophoneEnabled(), but enables only the camera without enabling the microphone. In addition to calling this method, you must also complete the additional steps described here in order to successfully enable the camera.

Example

void Awake() {
    #if UNITY_IOS && !UNITY_EDITOR
        iOSWebView.SetCameraEnabled(true);
    #endif
}

SetLongPressGesturesEnabled

void SetLongPressGesturesEnabled(bool enabled)

When Native 2D Mode is enabled, this method sets whether long press gestures are enabled. The default is true. When Native 2D Mode is not enabled, this method has no effect.

Example

await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
    var iOSWebViewInstance = canvasWebViewPrefab.WebView as iOSWebView;
    iOSWebViewInstance.SetLongPressGesturesEnabled(false);
#endif

SetMicrophoneEnabled

static void SetMicrophoneEnabled(bool enabled)

Like Web.SetCameraAndMicrophoneEnabled(), but enables only the microphone without enabling the camera. In addition to calling this method, you must also complete the additional steps described here in order to successfully enable the microphone.

Example

void Awake() {
    #if UNITY_IOS && !UNITY_EDITOR
        iOSWebView.SetMicrophoneEnabled(true);
    #endif
}

SetNativeParentView

static void SetNativeParentView(IntPtr nativeParentView)

3D WebView for iOS is powered by native WKWebView instances, and it must add those instances to the native iOS view hierarchy in order for them to work correctly. By default, 3D WebView adds the native WKWebView instances as children of the Unity view controller's view. However, you can call this method at the start of the app to override the native UIView to which 3D WebView adds the native WKWebView instances. For example, you may need to do this if your app embeds Unity as a library. The nativeParentView parameter is a pointer to an Objective-C UIView (UIView *).

Example

void Awake() {
    #if UNITY_IOS && !UNITY_EDITOR
        iOSWebView.SetNativeParentView(nativeParentView);
    #endif
}

SetScrollViewBounces

void SetScrollViewBounces(bool bounces)

When Native 2D Mode is enabled, this method sets whether the scroll view bounces past the edge of content and back again. The default is true. When Native 2D Mode is not enabled, this method has no effect.

Example

await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
    var iOSWebViewInstance = canvasWebViewPrefab.WebView as iOSWebView;
    iOSWebViewInstance.SetScrollViewBounces(false);
#endif

SetTargetFrameRate

void SetTargetFrameRate(uint targetFrameRate)

Sets the target web frame rate. The default is 30, which is also the maximum value. This method can be used to lower the target web frame rate in order to decrease energy and CPU usage. 3D WebView's rendering speed is limited by the speed of the underlying iOS APIs, so the actual web frame rate achieved is always lower than the default target of 30 FPS. This method is only used for the default render mode and is ignored when Native 2D Mode is enabled.

Example

await webViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
    var iOSWebViewInstance = webViewPrefab.WebView as iOSWebView;
    iOSWebViewInstance.SetTargetFrameRate(15);
#endif