1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIWebView+JSP/サーブレットのエラー処理

1
Last updated at Posted at 2013-03-06

iOSでハイブリッドアプリで、UIWebView全面に出すようなアプリだと、Webアプリ側のエラーが出て、スタックトレースがでるとまずいことがある。

iOSのネイティブアプリ側で、Webアプリからのレスポンスのステータスコードを見て、400/500番台だったら処理を変えたいが、ネイティブアプリ側でステータスコードをとるのはちょっと大変。

JSP/サーブレットとかであれば、Web.xmlの編集でも対応できる。

web.xml
  <error-page>
    <error-code>500</error-code>
    <location>/out.jsp</location>
  </error-page>

<error-code>に指定するステータスコードはその他、401/403/404などでも。
<location>はhtmlでもJSPでも良い。

out.jsp
<head>
<meta http-equiv="Refresh" content="0; URL=native://closeWebView">
</head>

表示させたページに<meta>で、リダイレクトされるように書いておく。

それで、ネイティブアプリ側では、

-UIWebViewを配置しているビューコントローラのクラスにUIWebViewDelegateプロトコルをインプリメント
-クラスをUIWebViewのdelegateに設定
-webView:shouldStartLoadWithRequest:navigationType: を実装

を行なって、shouldStartLoadWithRequest:navigationTypeには、上記のlogout.jspみたいなリダイレクトされたとき、プロトコル部分がnative でホスト部分が closeWebViewだったら画面を終了するように書いておく。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?