- (void)modifyUA
{
id webView = [uiWebViewInstance 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的方法
為了更安全地通過apple的審查,可以這樣:
- (void)modifyUA
{
NSString *path = [@"_internal.browser" stringByAppendingString:@"View._webView"];
id webView = [g_webView 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
出處:
更多信息請查看IT技術(shù)專欄