vue如何判斷安卓還是IOS
更新時間:2022年04月12日 17:05:07 作者:周小盜
這篇文章主要介紹了vue如何判斷安卓還是IOS,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
vue判斷安卓還是IOS
最近工作上遇到這樣一個需求
vue寫的頁面,需要同時跟安卓和ios進行交互;
- 若是安卓,執(zhí)行代碼:android.finishActivity();
- 若是IOS,執(zhí)行代碼:
try {? ?window.webkit.messageHandlers.finishActivity.postMessage("");? ?}catch(error) {? ?console.log('WKWebView post message'); }
所以我們需要進行一個判斷
是安卓還是IOS:因為是做的單獨的APP所以沒有考慮微信的問題
finishActivity() { ? ? ? ? let ua = navigator.userAgent.toLowerCase(); ? ? ? ? //android終端 ? ? ? ? let isAndroid = ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1;? ? ? ? ? //ios終端 ? ? ? ? let isiOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); ? ? ? ?? ? ? ? ? ? if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { ? ? ? ? ? ? //ios ? ? ? ? ? ? console.log(" 我是ios") ? ? ? ? ? ? //這里是和IOS商量好的寫法,調(diào)用IOS的finishActivity方法 ? ? ? ? ? ? try {? ? ? ? ? ? ? ? window.webkit.messageHandlers.finishActivity.postMessage("");? ? ? ? ? ? ? }catch(error) {? ? ? ? ? ? ? ? ? console.log('WKWebView post message'); ? ? ? ? ? ? ? } ? ? ? ? ? } else(/(Android)/i.test(navigator.userAgent)) { ? ? ? ? ? ? //android ? ? ? ? ? ? console.log("我是android") ? ? ? ? ? ? //這里是和安卓商量好的寫法,調(diào)用安卓的finishActivity方法 ? ? ? ? ? ? android.finishActivity(); ? ? ? ? ? ? ? ? ? ? ? } ? ? ?? ? }
然后就可以一個頁面同時給安卓和IOS進行交互啦!
H5端判斷安卓跟ios顯示不同的背景圖
html:
<div :class="`${isApple==true ? 'index-cont-phone' : 'index-cont'}`" ></div>
css:
? ? .index-cont{ ? ? ? ? width: 100%; ? ? ? ? height: auto; ? ? ? ? min-height: 100vh; ? ? ? ? overflow-x:hidden; ? ? ? ? background: url("https://tuoluohuodong.oss-cn-shenzhen.aliyuncs.com/interaction_h5/main_bg3.png") no-repeat; ? ? ? ? background-size: contain; ? ? ? ? margin: 0; ? ? ? ? padding-bottom: 199%; ? ? ? ? // position: fixed; ? ? } ? ? .index-cont-phone{ ? ? ? ? width: 100%; ? ? ? ? height: auto; ? ? ? ? min-height: 100vh; ? ? ? ? overflow-x:hidden; ? ? ? ? background: url("https://tuoluohuodong.oss-cn-shenzhen.aliyuncs.com/interaction_h5/main_bg4.png") no-repeat; ? ? ? ? background-size: contain; ? ? ? ? margin: 0; ? ? ? ? padding-bottom: 199%; ? ? ? ? // position: fixed; ? ? }
js:
<script> export default { ? ? name: "index", ? ? data() { ? ? ? ? return { ? ? ? ? ? ? isApple:true, ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }, ? ? ?}, ? ? ?methods: { ? ? ? ?// 判斷是安卓還是ios ? ? ? ? appDown() { ? ? ? ? ? ? var u = navigator.userAgent; ? ? ? ? ? ? var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); ? ? ? ? ? ? var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android終端 ? ? ? ? ? ? if(isiOS){ ? ? ? ? ? ? ? ? this.isApple = true ? ? ? ? ? ? }else if(isAndroid){ ? ? ? ? ? ? ? ? this.isApple = false ? ? ? ? ? ? } ? ? ? ?}, ? ?mounted() { ? ? ? ? ? // 調(diào)用判斷ios與安卓方法 ? ? ? ? this.appDown(); ? ? }, ?} </script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3 ts組合式API異常onMounted is called when&
這篇文章主要為大家介紹了vue3 ts組合式API異常onMounted is called when there is no active component問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05element 結(jié)合vue 在表單驗證時有值卻提示錯誤的解決辦法
這篇文章主要介紹了element 結(jié)合vue 在表單驗證下,有值卻提示錯誤的解決辦法,需要的朋友可以參考下2018-01-01詳解vue-template-admin三級路由無法緩存的解決方案
這篇文章主要介紹了vue-template-admin三級路由無法緩存的解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03webpack項目調(diào)試以及獨立打包配置文件的方法
下面小編就為大家分享一篇webpack項目調(diào)試以及獨立打包配置文件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02簡單的vue-resourse獲取json并應(yīng)用到模板示例
本篇文章主要介紹了簡單的vue-resourse獲取json并應(yīng)用到模板示例,非常具有實用價值,需要的朋友可以參考下。2017-02-02