UIWebView - 如何从Facebook等网页中提取HTML代码?

时间:2012-02-06 07:43:51

标签: javascript html ios xcode uiwebview

我有一个源代码示例,它以编程方式设置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”?

1 个答案:

答案 0 :(得分:0)

替换

//---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将包含使stringByEvaluatingJavaScriptFromString工作的HTML。