【问题标题】:UIWebView - how to extract the HTML code from webpages like Facebook?UIWebView - 如何从 Facebook 等网页中提取 HTML 代码?
【发布时间】:2012-02-06 07:43:51
【问题描述】:

我有一个源代码示例,它以编程方式设置 HTML 中字段的值。

部分源码如下:

- (void)viewDidLoad
{
[super viewDidLoad];

myWebView.delegate = self;

//---formulate the HTML string---
NSString *str = [[[NSString alloc] initWithString:
                @"<html>"
                 "    <body>"
                 "        <h1>The fields below are filled in programmatically</h1>"
                 "        <input id='username' value='UserName' />"
                 "        <input id='password' value='Password' type='password' />"
                 "    </body>"
                 "</html>"
                 ] autorelease];

//---load the UIWebView with the html string---
[myWebView loadHTMLString:str baseURL:nil];
}

//---wait for the UIWebView to finish loading---
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//---access the 2 fields in the HTML---
[myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('username').value='Wei-Meng Lee';"];
[myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('password').value='TopSecret';"];
}

我想在像 Facebook 这样的实际网页中以编程方式设置字段的值,而不是像上面写的那样自定义 HTML。

我应该如何将“str”替换为实际网页的 html 源代码,以便我可以挑选出“ElementbyId”?

【问题讨论】:

  • 您正在寻找从 URL 而不是 HTML 打开网页?
  • 我想从一个 URL 打开一个网页并能够使用 stringByEvaulatingJavaScriptFromString 函数。

标签: javascript html ios xcode uiwebview


【解决方案1】:

通过替换

//---formulate the HTML string---
NSString *str = [[[NSString alloc] initWithString:
            @"<html>"
             "    <body>"
             "        <h1>The fields below are filled in programmatically</h1>"
             "        <input id='username' value='UserName' />"
             "        <input id='password' value='Password' type='password' />"
             "    </body>"
             "</html>"
             ] autorelease];

//---load the UIWebView with the html string---
[myWebView loadHTMLString:str baseURL:nil];

使用[UIWebView loadRequest],它将使用 URL 加载 Web 视图,并且 UIWebView 将包含 HTML 以使 stringByEvaluatingJavaScriptFromString 工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2011-10-14
    • 2016-08-28
    • 2023-03-27
    • 2021-10-12
    相关资源
    最近更新 更多