objective-c – 在UIWebView中更改用户代理(IOS 5)
发布时间:2020-12-16 07:02:56 所属栏目:百科 来源:网络整理
导读:到目前为止我所做的:我使用下面的代码在项目的UIWebView中更改User-Agent. NSDictionary *userAgentReplacement = [[NSDictionary alloc] initWithObjectsAndKeys:userAgent,@"UserAgent",nil];[[NSUserDefaults standardUserDefaults] registerDefaults:us
到目前为止我所做的:我使用下面的代码在项目的UIWebView中更改User-Agent.
NSDictionary *userAgentReplacement = [[NSDictionary alloc] initWithObjectsAndKeys:userAgent,@"UserAgent",nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:userAgentReplacement]; 问题:我有这个代码工作,但是当我再次调用该函数时,要将UserAgent更改为其他东西,UIWebView不会更新.为了成功更新UserAgent,我需要删除/释放UIWebView并再次分配/显示它,但它会导致UIWebView丢失所有历史记录.有没有办法更新UserAgent而无需创建新的UIWebView以使其生效? 解决方法
当你使用NSUserDefaults时,我假设你正在设置它们
application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 我之前使用的替代方案是 NSMutableURLRequest*request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:yourURL]]; [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; [webView loadRequest:request]; 您还可以使用UIWebView Delegate使用NSString变量捕获webview的上次加载状态 -(void)webViewDidFinishLoad:(UIWebView *)webView{ currentConfig=[webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML]; } 然后使用重新加载webView [webView loadHTMLString:currentConfig baseURL:yourURL]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |