1)指定您的类符合 UIWebViewDelegate 协议(并确保您的 WebView 的委托出口连接到您的视图控制器):
@interface YourWebsiteViewController : UIViewController <UIWebViewDelegate>
2)添加以下委托方法:
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSURL *url = [webView.request mainDocumentURL];
NSLog(@"The Redirected URL is: %@", url);
}
根据您尝试使用此信息执行的操作,您可能希望用 #2 替换此方法(这将使您有机会阻止页面加载):
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request mainDocumentURL];
NSLog(@"The Redirected URL is: %@", url);
// Return YES if you want to load the page, and NO if you don't.
return NO;
}