WebView的私有API请查看《UIWebView体系结构(七)WebView》。
先上代码吧
- (void)modifyUA { id webView = [aUIWebViewInstance valueForKeyPath:@"_internal.browserView._webView"]; [webView performSelector:@selector(setCustomUserAgent:) withObject:@"Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13 "]; }
这是示例修改User Agent。获取WebView使用的是Key-Value Coding的方法,关于Path里的路径,可参考《UIWebView体系结构(一)概貌》。
为了更安全地通过apple的审查,可以这样:
- (void)modifyUA { NSString *path = [@"_internal.browser" stringByAppendingString:@"View._webView"]; id webView = [aUIWebViewInstance valueForKeyPath:path]; NSString *selString = [@"setCustom" stringByAppendingString:@"UserAgent:"]; SEL sel = NSSelectorFromString(selString); [webView performSelector:sel withObject:@"Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13 "]; }把字符串分割再合成就ok了。
转载请注明出处:http://blog.csdn.net/hursing