iOS调用WebView私有API的方法
2014-11-04来源:易贤网

- (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技术专栏

推荐信息