Set the referer http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36 using - setValue:forHTTPHeaderField: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableURLRequest/setValue%3aforHTTPHeaderField%3a
NSMutableURLRequest* request = ...;
[request setValue:@"https://myapp.com" forHTTPHeaderField: @"Referer"];
但请注意,根据 HTTP RFC,您不应该这样做,因为您的应用程序无法使用 URI 进行寻址:
如果获取了 Request-URI,则不得发送 Referer 字段
来自没有自己的 URI 的源,例如来自
用户键盘。
...除非您使用绑定到您的应用程序的自定义协议 (myapp://blah.com/blah)。
You can 创建一个 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html并打电话加载请求: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006950-CH3-SW8手动或拦截用户发出的正常请求。
- (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:@"https://whatever.com" forHTTPHeaderField: @"Referer"];
[self.webView loadRequest:request];
});
});
return NO;
}
}