2010-11-26 93 views

回答

0

如果您需要在Web视图中获取当前URL的内容,您可以在UIViewController中使用类似的内容。我没有编译这段代码,所以可能会有一些语法错误,但这应该是您的问题的一个坚实的起点。

@interface aViewController : UIViewController <UIWebViewDelegate> 
    UIWebView *webView; 
    NSString *htmlSource; 
@end 
@implementation aViewController 

-(void) viewDidLoad { 
    CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; 

    self.webView = [[UIWebView alloc] initWithFrame:webFrame]; 
    self.webView.delegate = self; 

    NSURL *aURL = [NSURL URLWithString:@"http://www.myurl.com"]; 
    NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL]; 
    //load the index.html file into the web view. 
    [self.webView loadRequest:aRequest]; 
}  
//This is a delegate method that is sent after a web view finishes loading content. 
- (void)webViewDidFinishLoad:(UIWebView *)webView{ 
    NSLog(@"finished loading"); 
    self.htmlSource = [[NSString alloc] initWithData:[[webView request] HTTPBody] encoding:NSASCIIStringEncoding]; 
} 

@end 
+0

对同一个URL的两个请求使错误响应的几率增加一倍,增加了响应不一致的可能性,并增加了对广播的使用。缓存可能会缓解这些问题,但为什么要依赖于这些数据? – 2010-11-26 19:50:35

1

您可以从web视图中获取JavaScript可以使用stringByEvaluatingJavaScriptFromString返回的任何内容:.尝试一下沿着

document.lastChild.outerHTML 

该文件可能有两个子节点;第一个是DOCTYPE,第二个是元素。