cordova-plugin-inappbrowser

Android Testsuite Chrome Testsuite iOS Testsuite Lint Test

アプリ内で役立つ記事、動画、Webリソースを表示できます。ユーザーはアプリを離れることなくWebページを閲覧できます。

いくつかのアイデアを得るには、このページの下部にあるサンプルを確認するか、リファレンスコンテンツに直接進んでください。

このプラグインは、cordova.InAppBrowser.open()を呼び出すと表示されるWebブラウザービューを提供します。

var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank', 'location=yes');

window.open

cordova.InAppBrowser.open()関数は、window.open()関数のドロップイン代替として定義されています。既存のwindow.open()呼び出しは、window.openを置き換えることで、InAppBrowserウィンドウを使用できます。

window.open = cordova.InAppBrowser.open;

ブラウザのwindow.open関数をこのように変更すると、(特にこのプラグインが別のプラグインの依存関係としてのみ含まれている場合)意図しない副作用が発生する可能性があります。

InAppBrowserウィンドウは標準のWebブラウザーのように動作し、Cordova APIにアクセスできません。このため、InAppBrowserは、メインのCordova Webビューにロードするのではなく、サードパーティ(信頼できない)コンテンツをロードする必要がある場合に推奨されます。InAppBrowserはホワイトリストの対象ではなく、システムブラウザーでリンクを開くこともありません。

InAppBrowserは、ユーザー向けの独自のGUIコントロール(戻る、進む、完了)をデフォルトで提供します。

インストール

cordova plugin add cordova-plugin-inappbrowser

アプリ内のすべてのページロードをInAppBrowser経由で実行する場合は、初期化中にwindow.openをフックするだけで済みます。例:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    window.open = cordova.InAppBrowser.open;
}

設定

config.xml

  • InAppBrowserStatusBarStyle [iOSのみ]:(文字列、オプション 'lightcontent'、'darkcontent'、または 'default'。デフォルトは 'default')iOSのテキストカラーのスタイルを設定します。 'lightcontent'は暗い背景での使用を目的としています。 'darkcontent'はiOS 13以降でのみ利用可能で、明るい背景での使用を目的としています。 ```xml

## cordova.InAppBrowser.open

Opens a URL in a new `InAppBrowser` instance, the current browser
instance, or the system browser.

    var ref = cordova.InAppBrowser.open(url, target, options);

- __ref__: Reference to the `InAppBrowser` window when the target is set to `'_blank'`. _(InAppBrowser)_

- __url__: The URL to load _(String)_. Call `encodeURI()` on this if the URL contains Unicode characters.

- __target__: The target in which to load the URL, an optional parameter that defaults to `_self`. _(String)_

    - `_self`: Opens in the Cordova WebView if the URL is in the white list, otherwise it opens in the `InAppBrowser`.
    - `_blank`: Opens in the `InAppBrowser`.
    - `_system`: Opens in the system's web browser.

- __options__: Options for the `InAppBrowser`. Optional, defaulting to: `location=yes`. _(String)_

    The `options` string must not contain any blank space, and each feature's name/value pairs must be separated by a comma. Feature names are case insensitive.

    All platforms support:

    - __location__: Set to `yes` or `no` to turn the `InAppBrowser`'s location bar on or off.

    Android supports these additional options:

    - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally.
    - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests or `yes` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload=post` it will raise an error).
    - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
    - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened
    - __closebuttoncaption__: set to a string to use as the close button's caption instead of a X. Note that you need to localize this value yourself.
    - __closebuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the
    close button color from default, regardless of being a text or default X. Only has effect if user has location set to `yes`.
    - __footer__: set to `yes` to show a close button in the footer similar to the iOS __Done__ button. 
    The close button will appear the same as for the header hence use __closebuttoncaption__ and __closebuttoncolor__ to set its properties.
    - __footercolor__: set to a valid hex color string, for example `#00ff00` or `#CC00ff00` (`#aarrggbb`) , and it will change the footer color from default.
    Only has effect if user has __footer__ set to `yes`.
    - __hardwareback__: set to `yes` to use the hardware back button to navigate backwards through the `InAppBrowser`'s history. If there is no previous page, the `InAppBrowser` will close.  The default value is `yes`, so you must set it to `no` if you want the back button to simply close the InAppBrowser.
    - __hidenavigationbuttons__: set to `yes` to hide the navigation buttons on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`.
    - __hideurlbar__: set to `yes` to hide the url bar on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`.
    - __navigationbuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color of both navigation buttons from default. Only has effect if user has location set to `yes` and not hidenavigationbuttons set to `yes`.
    - __toolbarcolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color the toolbar from default. Only has effect if user has location set to `yes`.
    - __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, navigation buttons go to the right and close button to the left. Default value is `no`.
    - __zoom__: set to `yes` to show Android browser's zoom controls, set to `no` to hide them.  Default value is `yes`.
    - __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`).
    - __shouldPauseOnSuspend__: Set to `yes` to make InAppBrowser WebView to pause/resume with the app to stop background audio (this may be required to avoid Google Play issues like described in [CB-11013](https://issues.apache.org/jira/browse/CB-11013)).
    - __useWideViewPort__: Sets whether the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is `no`, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is `yes` and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. (defaults to `yes`).
    - __fullscreen__: Sets whether the InappBrowser WebView is displayed fullscreen or not. In fullscreen mode, the status bar is hidden. Default value is `yes`.

    iOS supports these additional options:

    - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally.
    - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests or `yes` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload=post` it will raise an error).
    - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
    - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened. For WKWebView, requires iOS 11+ on target device.
    - __cleardata__: set to `yes` to have the browser's entire local storage cleared (cookies, HTML5 local storage, IndexedDB, etc.) before the new window is opened
    - __closebuttoncolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default __Done__ button's color. Only applicable if toolbar is not disabled.
    - __closebuttoncaption__: set to a string to use as the __Done__ button's caption. Note that you need to localize this value yourself.
    - __disallowoverscroll__: Set to `yes` or `no` (default is `no`). Turns on/off the the bounce of the WKWebView's UIScrollView.
    - __hidenavigationbuttons__:  set to `yes` or `no` to turn the toolbar navigation buttons on or off (defaults to `no`). Only applicable if toolbar is not disabled.
    - __navigationbuttoncolor__:  set as a valid hex color string, for example: `#00ff00`, to change from the default color. Only applicable if navigation buttons are visible.
    - __toolbar__:  set to `yes` or `no` to turn the toolbar on or off for the InAppBrowser (defaults to `yes`)
    - __toolbarcolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default color of the toolbar. Only applicable if toolbar is not disabled.
    - __toolbartranslucent__:  set to `yes` or `no` to make the toolbar translucent(semi-transparent)  (defaults to `yes`). Only applicable if toolbar is not disabled.
    - __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, close button goes to the right and navigation buttons to the left.
    - __enableViewportScale__:  Set to `yes` or `no` to prevent viewport scaling through a meta tag (defaults to `no`).
    - __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`).
    - __allowInlineMediaPlayback__: Set to `yes` or `no` to allow in-line HTML5 media playback, displaying within the browser window rather than a device-specific playback interface. The HTML's `video` element must also include the `webkit-playsinline` attribute (defaults to `no`).
    - __presentationstyle__:  Set to `pagesheet`, `formsheet` or `fullscreen` to set the [presentation style](https://developer.apple.com/documentation/uikit/uimodalpresentationstyle) (defaults to `fullscreen`).
    - __transitionstyle__: Set to `fliphorizontal`, `crossdissolve` or `coververtical` to set the [transition style](https://developer.apple.com/documentation/uikit/uimodaltransitionstyle) (defaults to `coververtical`).
    - __toolbarposition__: Set to `top` or `bottom` (default is `bottom`). Causes the toolbar to be at the top or bottom of the window.
    - __hidespinner__: Set to `yes` or `no` to change the visibility of the loading indicator (defaults to `no`).

    Windows supports these additional options:

    - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally.
    - __hardwareback__: works the same way as on Android platform.
    - __fullscreen__: set to `yes` to create the browser control without a border around it. Please note that if __location=no__ is also specified, there will be no control presented to user to close IAB window.


### Supported Platforms

- Android
- Browser
- iOS
- OSX
- Windows

### Example

    var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank', 'location=yes');
    var ref2 = cordova.InAppBrowser.open(encodeURI('http://ja.m.wikipedia.org/wiki/ハングル'), '_blank', 'location=yes');

### OSX Quirks

At the moment the only supported target in OSX is `_system`.

`_blank` and `_self` targets are not yet implemented and are ignored silently. Pull requests and patches to get these to work are greatly appreciated.

### iOS Quirks

Since the introduction of iPadOS 13, iPads try to adapt their content mode / user agent for the optimal browsing experience. This may result in iPads having their user agent set to Macintosh, making it hard to detect them as mobile devices using user agent string sniffing. You can change this with the `PreferredContentMode` preference in `config.xml`.

```xml
<preference name="PreferredContentMode" value="mobile" />

上記の例では、ユーザーエージェントにiPadを含めるように強制しています。もう1つのオプションは、値desktopを使用してユーザーエージェントをMacintoshに切り替えることです。

ブラウザの癖

  • プラグインはiframe経由で実装されます。

  • ナビゲーション履歴(LocationBarの戻るボタンと進むボタン)は実装されていません。

InAppBrowser

ターゲットが'_blank'に設定されている場合に、cordova.InAppBrowser.openの呼び出しから返されるオブジェクト。

メソッド

  • addEventListener
  • removeEventListener
  • close
  • show
  • hide
  • executeScript
  • insertCSS

InAppBrowser.addEventListener

InAppBrowserからのイベントのリスナーを追加します。(ターゲットが'_blank'に設定されている場合にのみ使用可能)

ref.addEventListener(eventname, callback);
  • refInAppBrowserウィンドウへの参照(InAppBrowser)

  • eventname:リッスンするイベント(文字列)

    • loadstartInAppBrowserがURLのロードを開始したときに発生するイベント。
    • loadstopInAppBrowserがURLのロードを完了したときに発生するイベント。
    • loaderrorInAppBrowserがURLのロード中にエラーが発生したときに発生するイベント。
    • exitInAppBrowserウィンドウが閉じられたときに発生するイベント。
    • beforeloadInAppBrowserがURLをロードするかどうかを決定したときに発生するイベント(beforeloadオプションが設定されている場合のみ)。
    • messageInAppBrowser Webビュー内にロードされたページから投稿されたメッセージをInAppBrowserが受信したときに発生するイベント。
  • callback:イベントが発生したときに実行される関数。この関数には、パラメータとしてInAppBrowserEventオブジェクトが渡されます。


var inAppBrowserRef;

function showHelp(url) {

    var target = "_blank";

    var options = "location=yes,hidden=yes,beforeload=yes";

    inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);

    inAppBrowserRef.addEventListener('loadstart', loadStartCallBack);

    inAppBrowserRef.addEventListener('loadstop', loadStopCallBack);

    inAppBrowserRef.addEventListener('loaderror', loadErrorCallBack);

    inAppBrowserRef.addEventListener('beforeload', beforeloadCallBack);

    inAppBrowserRef.addEventListener('message', messageCallBack);
}

function loadStartCallBack() {

    $('#status-message').text("loading please wait ...");

}

function loadStopCallBack() {

    if (inAppBrowserRef != undefined) {

        inAppBrowserRef.insertCSS({ code: "body{font-size: 25px;}" });

        inAppBrowserRef.executeScript({ code: "\
            var message = 'this is the message';\
            var messageObj = {my_message: message};\
            var stringifiedMessageObj = JSON.stringify(messageObj);\
            webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);"
        });

        $('#status-message').text("");

        inAppBrowserRef.show();
    }

}

function loadErrorCallBack(params) {

    $('#status-message').text("");

    var scriptErrorMesssage =
       "alert('Sorry we cannot open that page. Message from the server is : "
       + params.message + "');"

    inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);

    inAppBrowserRef.close();

    inAppBrowserRef = undefined;

}

function executeScriptCallBack(params) {

    if (params[0] == null) {

        $('#status-message').text(
           "Sorry we couldn't open that page. Message from the server is : '"
           + params.message + "'");
    }

}

function beforeloadCallBack(params, callback) {

    if (params.url.startsWith("http://www.example.com/")) {

        // Load this URL in the inAppBrowser.
        callback(params.url);
    } else {

        // The callback is not invoked, so the page will not be loaded.
        $('#status-message').text("This browser only opens pages on http://www.example.com/");
    }

}

function messageCallBack(params){
    $('#status-message').text("message received: "+params.data.my_message);
}

InAppBrowserEventプロパティ

  • type:イベント名。loadstartloadstoploaderrormessage、またはexitのいずれか。(文字列)

  • url:ロードされたURL。(文字列)

  • code:エラーコード。loaderrorの場合のみ。(数値)

  • message:エラーメッセージ。loaderrorの場合のみ。(文字列)

  • data:メッセージの内容。messageの場合のみ。文字列化されたJSONオブジェクト。(文字列)

サポートされているプラットフォーム

  • Android
  • ブラウザ
  • iOS
  • Windows
  • OSX

ブラウザの癖

loadstartloaderrormessageイベントは発生しません。

Windowsの癖

messageイベントは発生しません。

簡単な例

var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank', 'location=yes');
ref.addEventListener('loadstart', function(event) { alert(event.url); });

InAppBrowser.removeEventListener

InAppBrowserからのイベントのリスナーを削除します。(ターゲットが'_blank'に設定されている場合にのみ使用可能)

ref.removeEventListener(eventname, callback);
  • refInAppBrowserウィンドウへの参照。(InAppBrowser)

  • eventname:リスンを停止するイベント。(文字列)

    • loadstartInAppBrowserがURLのロードを開始したときに発生するイベント。
    • loadstopInAppBrowserがURLのロードを完了したときに発生するイベント。
    • loaderrorInAppBrowserがURLのロード中にエラーが発生したときに発生するイベント。
    • exitInAppBrowserウィンドウが閉じられたときに発生するイベント。
    • messageInAppBrowser Webビュー内にロードされたページから投稿されたメッセージをInAppBrowserが受信したときに発生するイベント。
  • callback:イベントが発生したときに実行される関数。この関数には、InAppBrowserEventオブジェクトが渡されます。

サポートされているプラットフォーム

  • Android
  • ブラウザ
  • iOS
  • Windows

簡単な例

var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank', 'location=yes');
var myCallback = function(event) { alert(event.url); }
ref.addEventListener('loadstart', myCallback);
ref.removeEventListener('loadstart', myCallback);

InAppBrowser.close

InAppBrowserウィンドウを閉じます。

ref.close();
  • refInAppBrowserウィンドウへの参照(InAppBrowser)

サポートされているプラットフォーム

  • Android
  • ブラウザ
  • iOS
  • Windows

簡単な例

var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank', 'location=yes');
ref.close();

InAppBrowser.show

非表示で開いたInAppBrowserウィンドウを表示します。InAppBrowserがすでに表示されている場合、これを呼び出しても効果はありません。

ref.show();
  • ref:InAppBrowserウィンドウへの参照(InAppBrowser

サポートされているプラットフォーム

  • Android
  • ブラウザ
  • iOS
  • Windows

簡単な例

var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank', 'hidden=yes');
// some time later...
ref.show();

InAppBrowser.hide

InAppBrowserウィンドウを非表示にします。InAppBrowserがすでに非表示になっている場合、これを呼び出しても効果はありません。

ref.hide();
  • ref:InAppBrowserウィンドウへの参照(InAppBrowser

サポートされているプラットフォーム

  • Android
  • iOS
  • Windows

簡単な例

var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank');
// some time later...
ref.hide();

InAppBrowser.executeScript

JavaScriptコードをInAppBrowserウィンドウに挿入します。(ターゲットが'_blank'に設定されている場合にのみ使用可能)

ref.executeScript(details, callback);
  • refInAppBrowserウィンドウへの参照。(InAppBrowser)

  • injectDetails:実行するスクリプトの詳細。fileまたはcodeキーのいずれかを指定します。(オブジェクト)
    • file:挿入するスクリプトのURL。
    • code:挿入するスクリプトのテキスト。
  • callback:JavaScriptコードが挿入された後に実行される関数。
    • 挿入されたスクリプトのタイプがcodeの場合、コールバックは、スクリプトの戻り値である単一のパラメータをArrayでラップして実行します。複数行のスクリプトの場合、これは最後のステートメントの戻り値、または最後に評価された式です。

サポートされているプラットフォーム

  • Android
  • ブラウザ
  • iOS
  • Windows

簡単な例

var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank', 'location=yes');
ref.addEventListener('loadstop', function() {
    ref.executeScript({file: "myscript.js"});
});

ブラウザの癖

  • codeキーのみがサポートされています。

Windowsの癖

MSDNドキュメントによると、呼び出されたスクリプトは文字列値のみを返すことができます。それ以外の場合、callbackに渡されるパラメータは[null]になります。

InAppBrowser.insertCSS

CSSをInAppBrowserウィンドウに挿入します。(ターゲットが'_blank'に設定されている場合にのみ使用可能)

ref.insertCSS(details, callback);
  • refInAppBrowserウィンドウへの参照(InAppBrowser)

  • injectDetails:実行するスクリプトの詳細。fileまたはcodeキーのいずれかを指定します。(オブジェクト)
    • file:挿入するスタイルシートのURL。
    • code:挿入するスタイルシートのテキスト。
  • callback:CSSが挿入された後に実行される関数。

サポートされているプラットフォーム

  • Android
  • iOS
  • Windows

簡単な例

var ref = cordova.InAppBrowser.open('https://apache.dokyumento.jp', '_blank', 'location=yes');
ref.addEventListener('loadstop', function() {
    ref.insertCSS({file: "mystyles.css"});
}); __

サンプル:InAppBrowserでヘルプページを表示する

このプラグインを使用すると、アプリ内で役立つドキュメントページを表示できます。ユーザーはオンラインヘルプドキュメントを表示してから、アプリを離れることなく閉じることができます。

これを行う方法を示すいくつかのスニペットを次に示します。

ユーザーにヘルプを求める方法を提供する

アプリでこれを行う方法はたくさんあります。ドロップダウンリストは簡単な方法です。


<select id="help-select">
    <option value="default">Need help?</option>
    <option value="article">Show me a helpful article</option>
    <option value="video">Show me a helpful video</option>
    <option value="search">Search for other topics</option>
</select>

ページのonDeviceReady関数でユーザーの選択を収集し、共有ライブラリファイル内のヘルパー関数に適切なURLを送信します。ヘルパー関数はshowHelp()という名前で、次にその関数を作成します。


$('#help-select').on('change', function (e) {

    var url;

    switch (this.value) {

        case "article":
            url = "https://cordova.dokyumento.jp/docs/en/latest/"
                        + "reference/cordova-plugin-inappbrowser/index.html";
            break;

        case "video":
            url = "https://youtu.be/F-GlVrTaeH0";
            break;

        case "search":
            url = "https://www.google.com/#q=inAppBrowser+plugin";
            break;
    }

    showHelp(url);

});

ヘルプページをロードする

open関数を使用してヘルプページをロードします。hiddenプロパティをyesに設定して、ページコンテンツがロードされた後にのみブラウザを表示できるようにします。これにより、ユーザーはコンテンツが表示されるのを待っている間、空白のブラウザを見ることがなくなります。loadstopイベントが発生すると、コンテンツがロードされたことがわかります。そのイベントは間もなく処理します。


function showHelp(url) {

    var target = "_blank";

    var options = "location=yes,hidden=yes";

    inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);

    inAppBrowserRef.addEventListener('loadstart', loadStartCallBack);

    inAppBrowserRef.addEventListener('loadstop', loadStopCallBack);

    inAppBrowserRef.addEventListener('loaderror', loadErrorCallBack);

}

ページを準備していることをユーザーに知らせる

ブラウザがすぐには表示されないため、loadstartイベントを使用して、ステータスメッセージ、プログレスバー、またはその他のインジケーターを表示できます。これにより、コンテンツが来ていることをユーザーに保証します。


function loadStartCallBack() {

    $('#status-message').text("loading please wait ...");

}

ヘルプページを表示する

loadstopcallbackイベントが発生すると、コンテンツがロードされたことがわかり、ブラウザを表示できます。この種のトリックは、パフォーマンスが向上した印象を与えることができます。実際には、コンテンツがロードされる前にブラウザを表示するかどうかにかかわらず、ロード時間はまったく同じです。


function loadStopCallBack() {

    if (inAppBrowserRef != undefined) {

        inAppBrowserRef.insertCSS({ code: "body{font-size: 25px;}" });

        $('#status-message').text("");

        inAppBrowserRef.show();
    }

}

insertCSS関数の呼び出しに気付いたかもしれません。これは、私たちのシナリオでは特定の目的を果たしません。しかし、なぜそれを使用する可能性があるのかを理解するのに役立ちます。この場合、ページのフォントサイズが特定のサイズになっていることを確認しているだけです。この関数を使用して、任意のCSSスタイル要素を挿入できます。プロジェクト内のCSSファイルを指定することもできます。

ページエラーを処理する

ページが存在しなくなった場合、スクリプトエラーが発生した場合、またはユーザーがリソースを表示する権限がない場合があります。その状況をどのように処理するかは、あなたとあなたの設計に完全に委ねられています。ブラウザにそのメッセージを表示させることも、別の方法で表示することもできます。

エラーをメッセージボックスに表示してみます。alert関数を呼び出すスクリプトを挿入することで実現できます。ただし、これはWindowsデバイス上のブラウザでは動作しないため、executeScriptのコールバック関数のパラメータを見て、試みが成功したかどうかを確認する必要があります。もしうまくいかなければ、ページ上の<div>にエラーメッセージを表示します。


function loadErrorCallBack(params) {

    $('#status-message').text("");

    var scriptErrorMesssage =
       "alert('Sorry we cannot open that page. Message from the server is : "
       + params.message + "');"

    inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);

    inAppBrowserRef.close();

    inAppBrowserRef = undefined;

}

function executeScriptCallBack(params) {

    if (params[0] == null) {

        $('#status-message').text(
           "Sorry we couldn't open that page. Message from the server is : '"
           + params.message + "'");
    }

}

その他の使用情報

ローカルURL (ソースはアプリパッケージ内)

var iab = cordova.InAppBrowser;

iab.open('local-url.html');                  // loads in the Cordova WebView
iab.open('local-url.html', '_self');         // loads in the Cordova WebView
iab.open('local-url.html', '_system');       // Security error: system browser, but url will not load (iOS)
iab.open('local-url.html', '_blank');        // loads in the InAppBrowser
iab.open('local-url.html', 'random_string'); // loads in the InAppBrowser
iab.open('local-url.html', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar

ホワイトリストに登録されたコンテンツ

var iab = cordova.InAppBrowser;

iab.open('https://whitelisted-url.com');                  // loads in the Cordova WebView
iab.open('https://whitelisted-url.com', '_self');         // loads in the Cordova WebView
iab.open('https://whitelisted-url.com', '_system');       // loads in the system browser
iab.open('https://whitelisted-url.com', '_blank');        // loads in the InAppBrowser
iab.open('https://whitelisted-url.com', 'random_string'); // loads in the InAppBrowser

iab.open('https://whitelisted-url.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar

ホワイトリストに登録されていないURL

var iab = cordova.InAppBrowser;

iab.open('https://url-that-fails-whitelist.com');                  // loads in the InAppBrowser
iab.open('https://url-that-fails-whitelist.com', '_self');         // loads in the InAppBrowser
iab.open('https://url-that-fails-whitelist.com', '_system');       // loads in the system browser
iab.open('https://url-that-fails-whitelist.com', '_blank');        // loads in the InAppBrowser
iab.open('https://url-that-fails-whitelist.com', 'random_string'); // loads in the InAppBrowser
iab.open('https://url-that-fails-whitelist.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar