简体   繁体   English

Cocoa:获取WebView的标题

[英]Cocoa: Get the Title of WebView

I make a simple webview to user share with twitter and would like to know: 我创建了一个简单的webview,用户可以通过Twitter分享,并想知道:

How can a get the title from a webview and put in on a string... 如何从webview获取标题并放入字符串...

Set your web view's frameLoadDelegate and implement the webView:didReceiveTitle:forFrame: method: 设置web视图的frameLoadDelegate并实现webView:didReceiveTitle:forFrame:方法:

- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
{
    if (frame == [myWebView mainFrame]) {
        NSLog(@"Title: %@", title);
        //...
    }
}
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

Use -(NSString *)mainFrameTitle method. 使用-(NSString *)mainFrameTitle方法。 this method Return The HTML title of the loaded page. 此方法返回已加载页面的HTML标题。 Returns @"" if the loaded document is not HTML. 如果加载的文档不是HTML,则返回@“”。

Set your WebView's resourceLoadDelegate. 设置WebView的resourceLoadDelegate。 It will be called every time there is a request: 每次有请求时都会调用它:

webView:resource:didReceiveResponse:fromDataSource:

the datasource has a pageTitle property. 数据源具有pageTitle属性。 Use this. 用这个。

This is preferable to polling for mainFrameTitle. 这对于mainFrameTitle的轮询更为可取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM