React-Native开发总结-webview
来源:互联网 发布:开机还原软件大全 编辑:程序博客网 时间:2024/05/20 02:52
最近更新时间:2017年12月14日19:36:11
上学的时候希望尽早工作,融入社会,积累工作经验。工作之后才发现,工作永远干不完,而且每个项目都不一样,积累工作经验不是一时半会的事情,而是日积月累细心领悟的过程,因此,需要不断总结和积累所见所闻,保持学习,才能轻松驾驭工作。
在react native项目中,一方面使用RN技术进行APP开发,同时还能在APP中嵌套使用网页,是常见的业务需求。常见方案有三种:使用html文件、URI地址、html代码片段。
0、概述
import { WebView } from 'react-native';
WebView组件,用于创建一个原生的WebView,可以用于访问一个网页;
1、HTML代码片段在WebView中的使用
html代码片段如下,html代码片段的内容可以是图文混编,来源于ajax接口返回的数据(string类型):
let HTML = '';
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
<style>
html {
font-size: 20px;
}
@media only screen and (min-width: 400px) {
html {
font-size: 21.33333333px !important;
}
}
@media only screen and (min-width: 414px) {
html {
font-size: 22.08px !important;
}
}
@media only screen and (min-width: 480px) {
html {
font-size: 25.6px !important;
}
}
body {
margin: 0;
padding: 1.5rem .8rem 1.5rem .8rem;
color: #424242;
line-height: 26px;
font-size: .8rem;
}
.content{
font-size: .8rem;
line-height: 1.3rem;
}
img {
width: 100%;
display: block;
margin: .5rem 0;
}
p{
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div class="content">
<p>asdfasdf</p>
</div>
</body>
</html>
对HTML代码片段style样式适配(如:禁止长按触发)重置如下:
let arr = HTML.split("<style>");
let disableSelect =`* {-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;}`
if(arr.length > 1){
arr[1] = disableSelect + arr[1];
HTML = arr.join("<style>");
}
APP页面使用方法:
<WebView
ref={webview => {this.webview = webview;}}
onError = {()=>{
ToastAndroid.show(`加载失败,请重试!`,ToastAndroid.SHORT,ToastAndroid.CENTER);
this.props.navigation.goBack()
}}
javaScriptEnabled={true}
domStorageEnabled={true}
onMessage={this.onMessage}
source={{html: HTML}}
/>
获取WebView组件实例的方法:
<WebView ref={webview => {this.webview01 = webview;}} />
console.log(this.webview01);//对象内容如下
<WebView ref={‘WEBVIEW01_REF’} />
console.log(this.refs[WEBVIEW01_REF]);//对象内容如下
2、uri地址在WebView中的使用
uri地址如下,uri的内容可以是PDF文件、doc文件,来源于ajax接口返回的数据(string类型):
let uri = 'http://abc.com/aa/bb/cc=='
<WebView
source={{uri: uri}}
/>
3、WebView组件实例详解
WebView组件实例是一个WebView对象,包括属性和方法,如下:
属性:context、props(domStorageEnabled、javaScriptEnabled、onError()、onMessage()、saveFormDataDisabled、scalesPageToFit、source:{}、thirdPartyCookiesEnabled、ref)、refs、state
方法:goBack()、goForward()、onLoadingError()、reloadd()、postMessage()......
使用:
this.refs[WEBVIEW01_REF].onMessage();//在webview内部的网页中调用window.postMessage方法时可以触发此属性对应的函数,从而实现网页和RN之间的数据交换。 设置此属性的同时会在webview中注入一个postMessage的全局函数并覆盖可能已经存在的同名实现。
未完,待续...
- React-Native开发总结-webview
- React Native开发总结
- React Native Webview使用
- 【React Native开发】React Native控件之WebView组件详解以及实例使用(22)
- React-Native开发之BUG 总结
- React-Native开发总结-react-navigation应用与实践
- React-Native开发总结-react层面上的问题
- 【React native】Webview的使用
- React native Webview的使用
- React Native控件之WebView
- React Native学习八- WebView
- React-Native WebView控件使用
- react-native的WebView使用
- React-Native开发总结+构建-调试-基础归纳总结
- React Native 学习总结
- react-native 问题总结
- React Native问题总结
- React native 问题总结
- Linux上部署Redis集群(to be continue)
- 基于HALCON的模板匹配方法总结
- 普及练习场 排序Ex 谁拿了最多奖学金
- Redis工具类
- 小米note2 手机,2017-12-14 最新的稳定版系统
- React-Native开发总结-webview
- 把更改后的uboot烧写到开发板及遇到的问题
- Unity3D Shader之路 VS2015下Graphics Debugger调试Shader
- 欢迎使用Markdown编辑器写博客
- C#数据结构中的二叉树
- 函数、函数的递归、重载、委托的声明
- hive中的内部表、外部表、分区表
- Spring Boot系列十 Spring MVC全局异常处理总结
- 深入Spring Boot:排查@Transactional引起的NullPointerException