48
45

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に独自のUserAgentを設定する

Posted at

UIWebViewのインスタンスを生成した後にUserAgentの設定をしようとしても反映されません。

実際に使うWebViewを生成する前に設定処理を書きます。

AppDelegate
- (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    //デフォルトのUserAgentを取得するために、サイズゼロのUIWebViewのインスタンスを生成
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];

	//デフォルトのUserAgentを取得
    NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    
    //デフォルトのUserAgentに独自の文字列を追加
    NSString *customUserAgent = [userAgent stringByAppendingString:@"付加したい文字列"];
    
    //UserAgent再設定
    NSDictionary *dictionary = @{@"UserAgent":customUserAgent};
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

	return YES;
}
48
45
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
48
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?