从本地加载一个.html到UIView.加载Js

- (void)loadView
{
UIWebView *webView = [[UIWebView alloc]init];
self.view = webView;
}

  • (void)viewDidLoad {
    [super viewDidLoad];

    // 设置标题
    self.title = self.html.title;

    UIWebView webView = (UIWebView )self.view;

    // 创建URL
    NSURL *url = [[NSBundle mainBundle] URLForResource:self.html.html withExtension:nil];

    // 创建请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 发送请求加载网页
    [webView loadRequest:request];

}
大致分为以下四步

  1. 替换View为UIWebView
  2. 创建URL.
  3. 创建请求
  4. View从请求加载.
    加载Js:
    // 代理方法
  • (void)webViewDidFinishLoad:(UIWebView )webView
    {
    // 1.拼接Javacript代码
    NSString
    js = [NSString stringWithFormat:@”window.location.href = ‘#%@’;”, self.html.ID];
    // 2.执行JavaScript代码
    [webView stringByEvaluatingJavaScriptFromString:js];
    }