我们如何在嵌入式 UIWebView 中设置 HTTP 引用?
我经历了这个,但仍然没有成功。
1.在viewDidLoad
,我写了这段代码,
[objWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"MY_URL"]]];
这是我的代码:
- (BOOL) webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType) navigationType
{
NSDictionary *headers = [request allHTTPHeaderFields];
BOOL hasReferer = [headers objectForKey:@"Referer"]!=nil;
if (hasReferer) {
// .. is this my referer?
return YES;
} else {
// relaunch with a modified request
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [request URL];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"Referer link" forHTTPHeaderField:@"Referer"];
[objWebView loadRequest:request];
});
});
return NO;
}
}
2.我也试过这个viewDidLoad
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"MY_URL"]];
[request setValue:@"Referel URL" forHTTPHeaderField:@"Referer"];
[objWebView loadRequest:request];
但是还没有成功。
请帮我解决这个问题或告诉我这段代码有什么问题吗?
希望我清楚地提出问题。