简体   繁体   English

Cocoa:获取WebView mainFrame html字符串

[英]Cocoa: get WebView mainFrame html string

I'm creating an Mac app that loads some content from a URL in a WebView and then needs to save the content to a file. 我正在创建一个Mac应用程序,它从WebView中的URL加载一些内容,然后需要将内容保存到文件中。 The content may and may not be HTML. 内容可能也可能不是HTML。 Loading works fine, the problem is to get the content from the WebView. 加载工作正常,问题是从WebView获取内容。 How do I do that? 我怎么做?

Thanks! 谢谢!

I do not know if you use WebView (MacOS) or UIWebVoew (iOS...). 我不知道您是否使用WebView(MacOS)或UIWebVoew(iOS ...)。 I use the following code (in MacOS) which works well for me: 我使用以下代码(在MacOS中)对我很有用:

WebFrame *frame = [myWebView mainFrame];
WebDataSource *source = [frame dataSource];
NSData *data = [source data];
NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
[someTextView setString:str];  // shows the content of myWebView as string

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

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