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:


(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

#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;


#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;


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
    [DllImport (__Internal)] private static extern void _OpenURL(string url);
    public void openURL(string url)
        {    #if UNITY_IPHONE
        }    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:


That’s it! 🙂

All codes and UnityPackage are available in GitHub:




  1. This is what I’ve been looking for! Thank you so much, purplelilgirl.
    Also, I have to give you credit for this script, right?

    1. 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.

      1. 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 ♥

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s