How to open a website in Unity without popping out of your App? (iOS)

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:

IMG_7697

(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

 

 

Advertisements

6 thoughts on “How to open a website in Unity without popping out of your App? (iOS)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s