基于UIWebView的混合编程

 

UIWebView的加载方法

1.UIWebView的loadRequest:方法


__weak typeof (self)weakSelf=self;

    NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{


        NSString *strUrl=_webViewImageURL;


        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];

        

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{


            [weakSelf.webView loadRequest:request];

            

            [MBProgressHUD showHUDAddedTo:_webView animated:YES];

            

        }];

    }];

    queue=[[NSOperationQueue alloc]init];

    [queue addOperation:operation];


2.UIWebView的loadHTMLString::方法

将本地html文件内容嵌入webView


NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];


     NSString *filePath  = [resourcePath stringByAppendingPathComponent:@"test.html"];


     NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];


    [self.webView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundlebundlePath]]];



对于第二种加载方式,可以使用模板引擎渲染HTML界面

UIWebView通过 

- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;

这个接口接收一个html的内容.
由于html是不停变化的,所以我们不能用固定的HTML格式,所以我们需要一个 模板引擎
模板引擎有MGTemplateEngine 和 GRMustache

关于GRMustache引擎 

1.它将模板内容放在另一个单独的文件中,方便日后更改

2.将原来一stringFormat格式固定的%@ 替换为{{ name }}形式

模板调整后变成了如下内容(文件名为template.html)

<HTML>

   <HEAD>

   </HEAD>

   <BODY>

<H1>{{ name }}</H1>

<P>{{ content }}</P>

</BODY>

</HTML>


然后我们在代码中将该文件读取到内存中(把fileName和bundlePath 用stringByAppendingPathComponent拼接起来,用stringWithContentsOfFile:读取 文件),

再使用GRMustache的renderObject方法生成渲染后的HTML内容.


然后让UIWebView通过loadHTMLString:方法加载




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值