這裏有一個例子ViewController.m:
#import "ViewController.h"
@interface ViewController() <UIWebViewDelegate>
@property (nonatomic) UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *HTML = @"<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=\"content-type\" content=\"text/html; charset=「utf-8\"> <!-- Internal CSS --> <style type=\"text/css\"> </style> <!-- Extrernal CSS --> <link rel=\"stylesheet\" href=\"css/normalize.css\"> <!-- jQuery --> <script src=\"http://code.jquery.com/jquery.min.js\" type=\"text/javascript\"></script> <!-- Internal JavaScripts --> <script type=\"text/javascript\"> </script> </head> <body> <a href=\"https://www.google.com/images/srpr/logo11w.png\"> <img src=\"https://www.google.com/images/srpr/logo11w.png\" alt=\"google\"> <p>This is a Google image.</p> </a> </body> </html>";
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.webView];
self.webView.delegate = self;
[self.webView loadHTMLString:HTML baseURL:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *htmlInner = [self.webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
NSLog(@"inner\n%@",htmlInner);
NSString *htmlOuter = [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
NSLog(@"outer\n%@",htmlOuter);
}
我不明白...... – user1033619
這個代碼轉換成HTML和打開這個字符串中網視圖 –