Custom UserAgent 설정

개발/iOS 2020. 12. 10. 15:21
반응형
//--------------------------------------------------------
/// WKWebView (type 1 - Objective C)
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero];
[wkWebView loadHTMLString:@"<html></html>" baseURL:nil];
[wkWebView evaluateJavaScript:@"navigator.appName" completionHandler:^(id __nullable appName, NSError * __nullable error) {
    NSLog(@"%@", appName);
}];

[wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id __nullable userAgent, NSError * __nullable error) {
    NSLog(@"%@", userAgent);
    // iOS 8.3
    // Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F70
    // iOS 9.0
    // Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.32 (KHTML, like Gecko) Mobile/13A4254v
    // Custom UserAgent
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@", userAgent, appName];
    if (@available(iOS 9.0, *)) {
        wkWebView.customUserAgent = newUserAgent;
    } else {
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    }
}];

//--------------------------------------------------------
/// WKWebView (type 2 - Objective C)
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.applicationNameForUserAgent = @"My Custom Useragent Name";
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
NSLog(@"%@", wkWebView.configuration.applicationNameForUserAgent); // My Custom Useragent Name
NSLog(@"%@", wkWebView.customUserAgent); // null
[wkWebView loadHTMLString:@"<html></html>" baseURL:nil];
[wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id __nullable userAgent, NSError * __nullable error) {
    NSLog(@"%@", userAgent);
    // iOS 9
    // Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.32 (KHTML, like Gecko) My Custom Useragent Name
}];

//--------------------------------------------------------
/// WKWebView (type 1 - Swift) Available: iOS 9+
wkWebView.evaluateJavaScript("navigator.userAgent") {
    [weak webView] (result, error) in
if let webView = webView, let userAgent = result as? String {
    webView.customUserAgent = userAgent + "/Custom Agent"
} }

//--------------------------------------------------------
/// WKWebView (type 1 - Swift) Available: iOS 9+
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.customUserAgent = "ExampleApp/1.0 (iPhone)"

//--------------------------------------------------------
/// WKWebView (type 2 - Swift) Available: iOS 9+
/// Append to the default User Agent
let webConfiguration = WKWebViewConfiguration()
webConfiguration.applicationNameForUserAgent = "ExampleApp/1.0 (iPhone)"
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
// Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) ExampleApp/1.0 (iPhone)
 
//--------------------------------------------------------
/// UIWebView (Objective C)
UIWebView *webView = [[UIWebView alloc] init];
[webView loadHTMLString:@"<html></html>" baseURL:nil];
NSString *appName = [webView stringByEvaluatingJavaScriptFromString:@"navigator.appName"];
NSLog(@"%@", appName);
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"%@", userAgent);
// iOS 8.3
// Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F70
// iOS 9.0
// Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.32 (KHTML, like Gecko) Mobile/13A4254v}

// Custom UserAgent
NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@", userAgent, appName];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

//--------------------------------------------------------
/// UIWebView (Swift) Available: iOS 9+
webView.customUserAgent = (UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent") ?? "") + "/Custom agent"



//--------------------------------------------------------
//--------------------------------------------------------
/// Example :

// UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@", userAgent, appName];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

// WKWebView
WKWebView *wkWebView = [[WKWebView alloc]initWithFrame:CGRectZero];
[wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id userAgent, NSError *error) {
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@", userAgent, appName];
    if (@available(iOS 9.0, *)) {
        wkWebView.customUserAgent = newUserAgent;
    } else {
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    }
}];

 

2020/12/10 - [iOS/Tips] - Custom UserAgent 설정

2020/12/10 - [iOS/Tips] - CocoaPods 설치 및 제거

2020/12/10 - [iOS/Tips] - Clang diagnostic 경고 무시하기

2020/12/10 - [개발노트] - Bluetooth UUID

2020/12/08 - [개발노트] - 모바일 앱 메모리덤프 이슈 해결방법

2020/12/08 - [프로그래밍/Java Script] - Android, iOS 앱 설치여부 체크 및 스토어 이동

2020/08/21 - [Android/Tips] - aab파일 apk파일로 변환

2020/08/11 - [iOS/Swift] - WKWebView 화면 출력 완료 이벤트

2020/08/06 - [iOS/Tips] - 개발관련 폴더 경로

2020/07/19 - [Android/Tips] - 안드로이드 원격 디버깅 방법

2020/07/18 - [Android/Tips] - Cannot convert string value 'JETPACK_COMPOSE' to an enum value of type 'com.android.builder.model.AndroidGradlePluginProjectFlags$BooleanFlag' (valid case insensitive values: APPLICATION_R_CLASS_CONSTANT_IDS, TEST_R_CLASS_CONSTANT_IDS, TRANSITIVE_R_CLASS)

2020/07/18 - [Android/Tips] - OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

2020/07/17 - [iOS/Tips] - 웹에서 iOS 앱 설치여부 체크

2020/07/14 - [Android/Tips] - 웹에서 안드로이드 앱을 실행 (Custom URL Scheme)

2020/07/11 - [OS/Mac OS X] - SMC(System Management Controller) 재설정

반응형

'개발 > iOS' 카테고리의 다른 글

Fat Static Library 빌드 (2/2)  (0) 2020.12.10
Fat Static Library 빌드 (1/2)  (1) 2020.12.10
CocoaPods 설치 및 제거  (0) 2020.12.10
Clang diagnostic 경고 무시하기  (0) 2020.12.10
WKWebView 화면 출력 완료 이벤트  (0) 2020.08.11
블로그 이미지

SKY STORY

,