I can’t get the WebViewTest demo to work. Followed the tutorial to see if I was missing anything but nothing is jumping out at me. I see the GUI buttons, but the web view doesn’t display. Just a default blue background. No errors are thrown. I’m using Unity 4.3.1, Xcode 5.0.2 and U3DXT Pro 1.6.2.1. I have tested on the iPhone 5 and the Simulator.
edit: also tested on iPad 2 with same results. Doesn’t matter if fullscreen is true or false.
EDIT: iOS SDK Pro v1.6.3.0+ resolves this issue. I kept the comment below for historical reasons.
It looks like 4.3.1 changed when/how the Start() function is called. We will develop another solution to adding the WebView automatically when embedding the prefab. In the meantime, try the attached WebViewGameObject.cs. Instead of automatically creating the webview at launch, you have to click on the “Create WebView” button.
using UnityEngine;
using System.Collections;
using U3DXT.iOS.Native.UIKit;
using U3DXT.iOS.Native.Foundation;
public class WebViewGameObject : MonoBehaviour {
public string homeURL = "http://u3d.as/content/vitapoly-inc/i-os-sdk-native-api-access-from-c-javascript-and-boo/50g";
// use pixels, automatically converts to DPI
public Rect locationAndSize = new Rect(10,50,460,260);
// you probabably don't want this unless you want to0 create your own UI with UIKit. Unity will be completely covered.
private bool fullScreen = false;
public bool showNavigationButtons = true;
// Use this for initialization
UIWebView _webview = null;
void Start () {
}
void CreateWebView()
{
// figure out parent
UIView parentView = null;
if ((UIApplication.deviceRootViewController != null)
&& (UIApplication.deviceRootViewController.view != null))
parentView = UIApplication.deviceRootViewController.view;
else
parentView = UIApplication.SharedApplication().keyWindow;
// create view
if (_webview == null) {
if ( fullScreen )
_webview = new UIWebView(parentView.bounds);
else
_webview = new UIWebView(locationAndSize);
this.setURL(homeURL);
}
// add it to parent
if (_webview.superview == null) {
parentView.AddSubview(_webview);
}
}
UIWebView getWebView()
{
return _webview;
}
void OnGUI() {
if ( !showNavigationButtons )
return;
GUILayout.BeginArea(new Rect(50, 25, Screen.width - 100, 50));
GUILayout.BeginHorizontal();
OnGUICreateWebView();
OnGUIBack();
OnGUIForward();
OnGUIHome();
OnGUISearch();
OnHideShow();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
void OnGUICreateWebView()
{
if (GUILayout.Button("Create WebView", GUILayout.ExpandHeight(true))) {
CreateWebView();
}
}
void OnHideShow()
{
if (GUILayout.Button("Hide/Show", GUILayout.ExpandHeight(true))) {
lock(_webview){
_webview.hidden = !_webview.hidden;
}
}
}
void OnGUIBack()
{
if (GUILayout.Button("Back", GUILayout.ExpandHeight(true))) {
lock(_webview){
_webview.GoBack();
}
}
}
void OnGUIHome()
{
if (GUILayout.Button("Home", GUILayout.ExpandHeight(true))) {
this.setURL("http://u3d.as/content/vitapoly-inc/i-os-sdk-native-api-access-from-c-javascript-and-boo/50g");
}
}
void OnGUISearch()
{
if (GUILayout.Button("Search", GUILayout.ExpandHeight(true))) {
this.setURL("http://www.google.com");
}
}
void OnGUIForward()
{
if (GUILayout.Button("Forward", GUILayout.ExpandHeight(true))) {
lock(_webview){
_webview.Go();
}
}
}
// NSURLRequest and NSURL must be a member variable otherwise you will get a crash
// when mono decides to delete it before webview is done with it.
NSURLRequest _request = null;
NSURL _url = null;
public void setURL(string url)
{
lock(_webview){
_url = new NSURL(url);
_request = new NSURLRequest( _url );
_webview.LoadRequest(_request);
}
}
void OnDestroy()
{
_webview.RemoveFromSuperview();
_webview = null;
_request = null;
_url = null;
}
// Update is called once per frame
// void Update () {
//
// }
}