4
4

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でHeaderを設定する方法

Posted at

UIWebViewでHeaderに値を足してたんだけど
shouldStartLoadWithRequestで

sample
[request setValue:@"1" forHTTPHeaderField: @"aaa"];
[_webview loadRequest:request];

ってゆう感じでやってたんですが、このやり方だとiFrameでyoutubeが埋め込まれてる場合にyoutubeが表示されないので調べ直した。
まずなんで表示されないかはわからなかった別にiframeってGET通信でいいだろうし、Chromeの「ModHeader」で同じヘッダー付けてサイト表示した場合は問題なかった。
didFailLoadWithErrorに入ってきたので通信キャンセルされてそうだった。

で他にいい方法あるかなーって調べた結果
iOS UIWebViewでrequest http header をカスタムする方法
ってのが見つかり

sample
-(NSURLRequest*) uiWebView:(id)webView
                  resource:(id)identifier
           willSendRequest:(NSURLRequest *)request
          redirectResponse:(NSURLResponse *)redirectResponse
            fromDataSource:(id)dataSource

という隠されたdeledateがあり確かに処理として通ってて、多分ついてる。※通信結果までは確認してません。
これならメソッドも書き換わらないし、便利ですね。
youtubeのiframeも無事表示されました。

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?