在 UIWebview 中获取当前 URL 但它没有响应

Get current URL in UIWebview but it not respond

我创建应用程序以使用 UIWebview 并在触摸 UIWebview 中的内容时向 url 显示日志。 这是我的代码

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url1 =[NSURL URLWithString:@"MyWebSite"];
    NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
    [_webView1 loadRequest:request1];
    NSURL *url2 =[NSURL URLWithString:@"MyWebsite2"];
    NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
    [_webView2 loadRequest:request2];//url menu 2
    NSString *currentURL = [_webView1 stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSLog(@"%@",currentURL);
}

但我触摸内容日志不打印,更改 webview 日志打印(空) 抱歉我的英语不好。

在从 URL 接收数据之前,您无法获取网站的标题。

所以

  • 将您的 webview delegate 设置为 self

然后在

  • - webViewDidFinishLoad: 获得称号