Created
September 2, 2019 13:26
-
-
Save anzfactory/972f5a8bd641f9e858e1dd204df08fe3 to your computer and use it in GitHub Desktop.
WKWebViewでUserAgentをいじるあれこれ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. UserDefaultsを使う方法 | |
// didFinishLaunchingWithOptions 内でやること | |
let userDefaults = UserDefaults.standart | |
userDefaults.register(defaults; [ | |
"UserAgent": "custom user agent..." | |
]) | |
// 2. WKWebView.customUserAgentを使う方法 | |
// ロード開始する前に指定しておくこと | |
let webview = WKWebView(frame: .zero) | |
webview.customUserAgent = "custom usre agent..." | |
// 3. WKWebViewConfiguration.applicationNameForUserAgent を使う方法 | |
let config = WKWebViewConfiguration() | |
config.applicationNameForUserAgent = "add user agent" | |
let webview = WKWebView(frame: .zero, configuration: config) | |
/* | |
* 1と2は、設定した値がまるっとUserAgentになる | |
* 3は指定された値でデフォルトのUserAgentに追記したものになる | |
* 1もしくは2を設定している場合に3を設定しても3の方は無視される | |
* 優先度は 2 > 1 > 3 という感じ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment