uiwebview 加载本地js、css、img,html从网站加载

本文介绍了一种使用WebView加载本地HTML的方法,通过从服务端拉取HTML文件,并利用iOS平台的UIWebView组件进行加载展示。文章包含完整的HTML、CSS及JavaScript代码示例。

资源文件都是放在根目录下

1、index.html

<html>
<head>
<title>My test Page</title>
<link rel="stylesheet" href = "test.css" type="text/css"/>
<script type = "text/javascript" src = "22.js"></script>
</head>
<body>
<h1 id="food" onclick ="sayHello()">Click me!</h1>
<img src="114.png" width="128" height="128" />
</body>
</html>

2、22.js

function sayHello() {

    if (document.getElementById('foo').innerHTML == 'Hi there'){

        document.getElementById('foo').innerHTML = 'Click me!';

        

    }else{

        document.getElementById('foo').innerHTML = 'Hi there'

    }

}

 3、test.css

body{

    color:red;

    background-color:#fff;

    font-size:12px;

    font-style:italic;

    font-weight:bold;

    font-family:Arial;

}

4、oc 代码 

    webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [self.view addSubview:iWebView];

    [webView setBackgroundColor:[UIColor whiteColor]];

     NSString *path = [[NSBundle mainBundle] bundlePath];

    NSURL *baseURL = [NSURL fileURLWithPath:path];

    NSString *imageStr=[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.155/index.html"] encoding:NSUTF8StringEncoding error:nil];

      [webView loadHTMLString:imageStr baseURL:baseURL];

5、原理就是先把服务端的html拉下来,然后通过webview加载本地html的方式加载,

 

转载于:https://www.cnblogs.com/sgdkg/p/5576906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值