在开发uniapp的过程中就需要判断用户在使用的终端,根据客户端调用不同的链接、参数等。
//手机 /*#ifdef APP-PLUS*/ //对应逻辑 if (plus.os.name == 'Android') { plus.runtime.openURL(tburl, res => { uni.showModal({ content: "本机未检测到淘宝客户端,是否打开浏览器访问?", success: res => { if (res.confirm) { plus.runtime.openURL(tburl) } } }) }, 'com.taobao.taobao'); }else{ // } /*#endif*/ //H5 /*#ifdef H5*/ //对应逻辑 /*#endif*/ //MP:小程序 /*#ifdef MP*/ //对应逻辑 /*#endif*/
也可这样判断:
switch (uni.getSystemInfoSync().platform) { case 'android': console.log('运行Android上'); break; case 'ios': console.log('运行iOS上'); break; default: console.log('运行在开发者工具上'); break; }
因为uni.getSystemInfoSync可以获取很多信息:
{ "SDKVersion": "", "appId": "__UNI__geg", "appLanguage": "zh-Hans", "appName": "哥哥不寂寞", "appVersion": "1.7.109", "appVersionCode": "17109", "browserName": "safari", "browserVersion": "13.0.3", "deviceId": "16976172513196857", "deviceModel": "iPhone", "deviceOrientation": "portrait", "devicePixelRatio": 2, "deviceType": "phone", "hostLanguage": "zh-CN", "hostName": "safari", "hostTheme": "light", "hostVersion": "13.0.3", "language": "zh-CN", "model": "iPhone", "osName": "ios", "osVersion": "13.2.3", "pixelRatio": 2, "platform": "ios", "safeArea": { "left": 0, "right": 375, "top": 0, "bottom": 667, "width": 375, "height": 667 }, "safeAreaInsets": { "top": 0, "right": 0, "bottom": 0, "left": 0 }, "screenHeight": 667, "screenWidth": 375, "statusBarHeight": 0, "system": "iOS 13.2.3", "ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1", "uniCompileVersion": "3.8.7", "uniPlatform": "web", "uniRuntimeVersion": "3.8.7", "version": "", "windowBottom": 0, "windowHeight": 623, "windowTop": 44, "windowWidth": 375 }