WebViewControlleris a custom
UIViewControllerfor handling displaying the WebView browser. It uses
subdomainat the top of the file to your own.
WebViewController, find the variable
source. It holds the code to
webkit.messageHandlers. This callback passes the
event.data(which contains the Ready Player Me avatar's .glb file URL).
WKWebViewConfigurationobject and a
source:parameter is set to the
selfand the name to use for the callback function.
WKScriptMessageHandler, you can pass
selfas the first parameter. The name passed for the callback function is
WKWebView, passing the frame bounds and the
configobject, and assign this view as the
WebViewto ensure it is then displayed.
message.body. In this example, you pass this to an
avatarurlDelegate.avatarUrlCallbackfunction that passes the data to the main ViewController which is then displayed in a native popup.
ViewController.swiftfile implements the main view controller.
viewDidLoad()function calls the
createWebView()function, sets the visibility of the buttons to
true, and hides the
hasCookies()function determines whether to hide the
editAvatarButton. It is only possible to edit an existing avatar if one has been created, and the avatar data is stored in the browser's cookies.
createWebView()function creates and configures the
webViewController.avatarUrlDelegate = self, it assigns itself as an
tagused as an identifier inside the
avatarUrlCallback(url: string)function is called by the
webViewControllerwhen the avatar creation process has completed. It displays an alert with the URL and and hides the
WebViewController,and force a page reload.
onCreateNewAvatarActionfunction must remove all previous avatar data. It destroys and recreates the web view, reloads the page, and clears the history with
destroyWebView()function uses the
view.tagidentifier set in the
CreateWebView()function to determine which view to remove.
webCacheCleaner.swiftwith a single function
clean()for clearing the web view browser's cookies and cache.