iOSWebView is the IWebView implementation used by 3D WebView for iOS. It also includes additional APIs for iOS-specific functionality.
IntPtr GetNativeWebView()
Returns an Objective-C pointer to the instance's underlying native WKWebView. The application can use this to utilize native iOS APIs for which 3D WebView doesn't yet have dedicated C# equivalents. To utilize the pointer, the application must pass it to a native function defined in an Objective-C (.m) file like illustrated in the example below.
// 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;
}
// 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);
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.
void SetAllowsBackForwardNavigationGestures(bool allow)
Sets whether horizontal swipe gestures trigger backward and forward page navigation. The default is false
.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = webViewPrefab.Webview as iOSWebView;
iOSWebViewInstance.SetAllowsBackForwardNavigationGestures(true);
#endif
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.
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetAllowsInlineMediaPlayback(false);
#endif
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.
void Awake() {
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetCameraEnabled(true);
#endif
}
void SetFullscreenEnabled(bool enabled)
When Native 2D Mode is enabled, this method sets whether web pages can use the JavaScript Fullscreen API to make an HTML element occupy the device's entire screen. The default is true
, meaning that the JavaScript Fullscreen API is enabled by default. When Native 2D Mode is disabled, this method has no effect because the JavaScript Fullscreen API is only supported in Native 2D Mode.
#if UNITY_IOS && !UNITY_EDITOR
await canvasWebViewPrefab.WaitUntilInitialized();
var iOSWebViewInstance = canvasWebViewPrefab.WebView as iOSWebView;
// Disable the JavaScript Fullscreen API.
iOSWebViewInstance.SetFullscreenEnabled(false);
#endif
See also: Fullscreen support in 3D WebView
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.
await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = canvasWebViewPrefab.WebView as iOSWebView;
iOSWebViewInstance.SetLongPressGesturesEnabled(false);
#endif
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.
void Awake() {
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetMicrophoneEnabled(true);
#endif
}
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 *
).
void Awake() {
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetNativeParentView(nativeParentView);
#endif
}
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.
await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = canvasWebViewPrefab.WebView as iOSWebView;
iOSWebViewInstance.SetScrollViewBounces(false);
#endif
See also: UIScrollView.bounces
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.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = webViewPrefab.WebView as iOSWebView;
iOSWebViewInstance.SetTargetFrameRate(15);
#endif
EventHandler<iOSAuthChallengeReceivedEventArgs> AuthChallengeReceived
The application can use this event to handle authentication challenges (e.g. for HTTP auth, client certificate auth, integration with smart cards). This event maps directly to iOS's native webView:didReceiveAuthenticationChallenge:completionHandler: delegate method. In order to provide access to the full flexibility of the native API, this event provides IntPtr references to the native Objective-C pointers for the challenge
and completionHandler
arguments that the OS passed to webView:didReceiveAuthenticationChallenge:completionHandler:. To utilize these IntPtr arguments, the application must pass them to a native function defined in an Objective-C (.m) file like illustrated in the example below. If the application attaches a listener to this event, it must invoke the native CompletionHandler callback in order to specify how the authentication challenge should be handled.
// Example of defining a native Objective-C function that handles the authentication challenge.
// Place this in a .m file in your project, like Assets/Plugins/WebViewCustom.m
#import <Foundation/Foundation.h>;
typedef void (^ CompletionHandler)(enum NSURLSessionAuthChallengeDisposition, NSURLCredential *);
void WebViewCustom_handleAuthChallengeReceived(NSURLAuthenticationChallenge * authChallenge, CompletionHandler completionHandler) {
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"myUsername" password:@"myPassword" persistence:NSURLCredentialPersistenceForSession];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}
// Example of calling the native Objective-C function from C#.
async void AttachAuthChallengeReceivedEventHandler(WebViewPrefab webViewPrefab) {
await webViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = webViewPrefab.WebView as iOSWebView;
iOSWebViewInstance.AuthChallengeReceived += (sender, eventArgs) => {
WebViewCustom_handleAuthChallengeReceived(eventArgs.AuthChallenge, eventArgs.CompletionHandler);
};
#endif
}
[System.Runtime.InteropServices.DllImport("__Internal")]
static extern void WebViewCustom_handleAuthChallengeReceived(IntPtr authChallenge, IntPtr completionHandler);