UserAgent 설정을 매우 쉽게 사용자 지정하는 방법
                                            
                                                
                                                
                                                
                                                
                                                
                                                 5986 단어  XcodeiOSObjective-C
                    
2016/0901 この記事は環境:Xcode7.3.1です。목적
WebView에서 표시할 때 서버에 Request하는 User-Agent 헤더에 앱 버전을 부여시키고 싶다.
AppDelegate 편집(UIWebView)
WebView를 사용하여 통신하기 전에 설정하지 않으면 반영되지 않으므로 AppDelegate
didFinishLaunchingWithOptions에 설명합니다.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// デフォルトのUserAgentを取得するために、サイズゾロのUIWebViewインスタンスを生成
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    // デフォルトのUserAgentを取得
    NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    // デフォルトのUserAgent文字列後尾にアプリバージョンを追加した文字列を作成
    NSString *customUserAgent = [userAgent stringByAppendingString:[NSString stringWithFormat:@" AppVersion/%@",appVersion]];
    // UserAgentをkyeとし,customUserAgentをvalueとした辞書型変数を作成
    NSDictionary *userAgentDict = [[NSDictionary alloc] initWithObjectsAndKeys:customUserAgent, @"UserAgent", nil];
    // ユーザーデフォルトにカスタムUserAgentを設定
    [[NSUserDefaults standardUserDefaults] registerDefaults:userAgentDict];
 }
기본 UserAgent를 사용하지 않는 경우의 설명은 매우 적어, 상기
didFinishLaunchingWithOptions에 있어서 2행으로 설정할 수 있습니다 
   // UserAgentをkyeとし,customUserAgentをvalueとした辞書型変数を作成
    NSDictionary *userAgentDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"InsertCustomUserAgent!!", @"UserAgent", nil];
    // ユーザーデフォルトにカスタムUserAgentを設定
    [[NSUserDefaults standardUserDefaults] registerDefaults:userAgentDict];
AppDelegate 편집(WKWebView)
마찬가지로 WebView를 사용하여 통신하기 전에 설정하지 않으면 반영되지 않으므로 AppDelegate
didFinishLaunchingWithOptions에 설명합니다. WKWebView는 UserAgent 프로퍼티가 준비되어 있으므로 대입하는 것만으로 설정할 수 있습니다.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    WKWebView *webView = [[WKWebView alloc] init];
    webView.customUserAgent = @"InsertCustomUserAgent!!";
}
Reference
이 문제에 관하여(UserAgent 설정을 매우 쉽게 사용자 지정하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kazTera/items/f51e1c1e000b3f2ea32d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)