I know you can always use Application.OpenURL, but if you use that, you will pop out of your app and open Safari, and then the user would have to press the Home button, and then navigate back to your app…
As a game developer, my goal is to keep my players within the game as much as possible.
Although there are times that you might want to show a website to your players (your company’s website or Facebook page, perhaps?)
So I imitated Apple’s SKProductViewController, create a UIWebViewController and present it as a modal view.
It will look like this:
(to prevent the Status Bar showing in the Appstore view, add View Controller based Status Bar to Info.plist and set it to false)
Some Code Bits: NativeWebView.h and NativeWebView.mm
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
// Root view controller of Unity screen
extern UIViewController *UnityGetGLViewController();
@interface NativeWebView : UINavigationController
{ UIWebView *webView;
}
-(void) openURL: (const char*) urlString;
-(void) dismissModal;
@end
#import “NativeWebView.h”
@implementation NativeWebView
– (id)init
{
webView = [[UIWebView alloc] init];
UIViewController *webViewController = [[UIViewController alloc] init];
webViewController.view = webView;
self = [super initWithRootViewController: webViewController];
self.navigationBar.barStyle = UIBarStyleDefault;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@”Cancel” style: UIBarButtonItemStylePlain target:self action: @selector(dismissModal)];
[self.navigationBar.topItem setLeftBarButtonItem: cancelButton];
return self;
}
-(void) openURL: (const char*) urlString
{
NSURL *url = [[NSURL alloc] initWithString: [NSString stringWithUTF8String: urlString]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
[UnityGetGLViewController() presentModalViewController: self animated: YES];
}
-(void) dismissModal
{ [UnityGetGLViewController() dismissModalViewControllerAnimated: YES];
}
-(BOOL) shouldAutorotate
{ return false;
}
@end
static NativeWebView *nativeWebViewPlugin = nil;
extern “C”
{
void _OpenURL(const char* url)
{
if (nativeWebViewPlugin == nil)
nativeWebViewPlugin = [[NativeWebView alloc] init];
[nativeWebViewPlugin openURL: url];
}
}
And then on Unity side, I have a WebViewHandler.cs:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class WebViewHandler : Singleton<WebViewHandler>
{
#if UNITY_IPHONE
[DllImport (“__Internal“)] private static extern void _OpenURL(string url);
#endif
public void openURL(string url)
{
if(!Application.isEditor)
{ #if UNITY_IPHONE
_OpenURL(url);
#endif
} else
{ Debug.Log(“WebViewHandler:: Cannot open WebView in Editor.“);
}
}
}
So… to open a website from Unity just call WebViewHandler (which is a Singleton) and pass the URL:
WebViewHandler.Instance.openURL(m_url);
That’s it! 🙂
All codes and UnityPackage are available in GitHub: https://github.com/purplelilgirl/Unity-NativeWebView
this is great, thanks for sharing!
Thanks a lot purplelilgirl
This is what I’ve been looking for! Thank you so much, purplelilgirl.
Also, I have to give you credit for this script, right?
PS: Any news on the Android version of this?
That’s alright, you don’t need to credit me. For the Android it’s a bit trickier, because it doesn’t have a default “browser”, so it still pops up so something.
Ohhhhh… That’s okay, I get it. I just thought it had already been posted or something since this publication is from 2014 😅
Thank you so much. You rock, purplelilgirl ♥