IT분야/iOS
[iOS]UIWebView에서 user-agent 변경
suroMind
2012. 1. 31. 16:23
출처 : http://wkqqn.tistory.com/archive/20110711
기존에 user-agent 변경하는 방법을 Swizzle을 이용해서 변경했었는데
iOS 5로 업데이트 되면서 변경이 안되었다. 물론 앱스토어에 올라가 있는 앱들이 제대로 되지도 않고..
그래서 찾아보다가 한참을 헤매었는데 위 블로그에서 아래와 같은 방법을 찾았다.
AppDelegate.m 파일에 아래 코드를 넣으면 됩니다.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *deviceModel = [[UIDevice currentDevice].model stringByReplacingOccurrencesOfString:@"" withString:@""];
NSString *userAgent = [NSString stringWithFormat:@"Mozilla/5.0 (%@; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334,webView_iphone",deviceModel];
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:userAgent,@"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[dictionnary release];
.....
.....
}
본인은 스마트폰에서 모바일웹으로 접속한 것인지 앱에서 웹뷰로 접속한 것인지 구분하기 위해
UserAgent 맨 뒤에 ,webView_iphone 문자열을 삽입하였습니다.
확인은 웹뷰를 구현한 곳에서 아래처럼 출력하시면 확인 할 수 있습니다
-(BOOL) webView:(UIWebView*) webView shouldStartLoadWithRequest:(NSURLRequest*) req navigationType:(UIWebViewNavigationType) navigationType{
NSMutableURLRequest *request = (NSMutableURLRequest *)req;
NSLog(@"%@",[request allHTTPHeaderFields]);
....
....
}
기존에 user-agent 변경하는 방법을 Swizzle을 이용해서 변경했었는데
iOS 5로 업데이트 되면서 변경이 안되었다. 물론 앱스토어에 올라가 있는 앱들이 제대로 되지도 않고..
그래서 찾아보다가 한참을 헤매었는데 위 블로그에서 아래와 같은 방법을 찾았다.
AppDelegate.m 파일에 아래 코드를 넣으면 됩니다.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *deviceModel = [[UIDevice currentDevice].model stringByReplacingOccurrencesOfString:@"" withString:@""];
NSString *userAgent = [NSString stringWithFormat:@"Mozilla/5.0 (%@; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334,webView_iphone",deviceModel];
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:userAgent,@"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[dictionnary release];
.....
.....
}
본인은 스마트폰에서 모바일웹으로 접속한 것인지 앱에서 웹뷰로 접속한 것인지 구분하기 위해
UserAgent 맨 뒤에 ,webView_iphone 문자열을 삽입하였습니다.
확인은 웹뷰를 구현한 곳에서 아래처럼 출력하시면 확인 할 수 있습니다
-(BOOL) webView:(UIWebView*) webView shouldStartLoadWithRequest:(NSURLRequest*) req navigationType:(UIWebViewNavigationType) navigationType{
NSMutableURLRequest *request = (NSMutableURLRequest *)req;
NSLog(@"%@",[request allHTTPHeaderFields]);
....
....
}