我正在制作iphone应用程序...我可以在objective-c中获取它的来源吗?
谢谢! 利亚
答案 0 :(得分:1)
您可以使用stringByEvaluatingJavaScriptFromString:从JavaScript视图中获取任何内容。尝试一下
的内容document.lastChild.outerHTML
该文档可能有两个子节点;第一个是DOCTYPE,第二个是< html>元件。
答案 1 :(得分: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