Examples of uni-app making calls on different platforms

Scenario
Making calls in the App is a relatively common application scenario, but by searching for articles, we found that most of the blog posts are from the uni-app official website copy, copy
The call provided by uni-app only helps you to call out the dialing interface, and cannot make direct calls. The Android native API can be used, but IOS cannot because of permission issues
So , we can make a judgment. If it is Android, click to dial the phone directly. For other platforms, use the default call dialing interface of uni-app
Implementation mechanism
- The interface provided by HTML5 plus.device.dial The use of this SDK requires the introduction of the package
- The interface provided externally by uni-app uni.makePhoneCall
- IOS and Andriod provide native interfaces - no Familiar with native development, there will be difficulties
- H5 page in mobile browser
<a href="tel: 10086">10086</a>复制代码
No more nonsense, just go to the code description The following is the implementation of the code interface of each platform through conditional compilation
testDevice.vue
<view>
<!-- #ifdef APP-PLUS -->
<button @tap="telphone">拨打电话</button>
<!-- #endif -->
<!-- #ifdef H5 -->
<a href="tel:10086">10086-h5平台下</a>
<!-- #endif -->
</view>
<script>
// 对不同的平台有一点区分
import telphone from './telphone.js'
export default {
methods: {
telphone() {
// 通过传递电话参数,调用不同平台拨打电话的功能
telphone("10086")
}
}
}
</script>复制代码We do not pay attention to interface issues here to avoid distracting the attention of the readers, and focus on the implementation in js
Please note that conditional compilation must be used to support different scenarios. The above is the App side (IOS and Andriod), and the following is ordinary h5
telphone.js
//#ifdef H5
import VConsole from 'vconsole'
new VConsole()
//#endif
export default (phone) => {
// 获取设备平台
let platform = uni.getSystemInfoSync().platform
//#ifdef H5
// h5环境--浏览器
let ua = navigator.userAgent.toLowerCase()
// 就要判断 是微信内置浏览器还是用户的普通浏览器
if (ua.match(/MicroMessenger/i) == "micromessenger") {
// 微信浏览器
console.log('微信浏览器')
} else {
// 普通浏览器
}
//#endif
//#ifdef APP-PLUS
// app环境
switch (platform) {
case 'android':
// 导入Activity、Intent类
var Intent = plus.android.importClass("android.content.Intent");
var Uri = plus.android.importClass("android.net.Uri");
// 获取主Activity对象的实例
var main = plus.android.runtimeMainActivity();
// 创建Intent
var uri = Uri.parse("tel:" + phone); // 这里可修改电话号码
var call = new Intent("android.intent.action.CALL", uri);
// 调用startActivity方法拨打电话
main.startActivity(call);
break;
case 'ios':
// 使用uni-app提供的借口
uni.makePhoneCall({
phoneNumber: phone
})
break;
default:
// 调试器工具
}
//#endif
}复制代码Notes
- Conditional compilation, when we use VConsole, if we do not use conditional compilation, an error will be reported on the App side
- Be sure not to write the import statement in the if judgment Or during trinocular operation, an error will be reported. To understand the mechanism of ES6 module loading
- Use the interface provided by uni-app to determine whether it is the App platform (IOS or Andriod). How to distinguish between ordinary browsers and WeChat browsers? Dependent on conditional compilation
- Whether it is the API implementation provided by uni-app or the Android SDK, you will jump out of the App to make a call. After hanging up, you will still be called back to the App interface
plus.device.dial needs to introduce the corresponding SDK. This actually requires conditional compilation to determine the current environment. The above is enough. In fact, it is the same as introducing vconsole
For other articles, please visit the uni-app column!
The above is the detailed content of Examples of uni-app making calls on different platforms. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
How to develop uni-app in VSCode? (Tutorial sharing)
May 13, 2022 pm 08:11 PM
How to develop uni-app in VSCode? The following article will share with you a tutorial on developing uni-app in VSCode. This may be the best and most detailed tutorial. Come and take a look!
Use uniapp to develop a simple map navigation
Jun 09, 2022 pm 07:46 PM
How to use uniapp to develop a simple map navigation? This article will provide you with an idea for making a simple map. I hope it will be helpful to you!
How to encapsulate uni-app vue3 interface request
May 11, 2023 pm 07:28 PM
uni-app interface, global method encapsulation 1. Create an api file in the root directory, create api.js, baseUrl.js and http.js files in the api folder 2.baseUrl.js file code exportdefault"https://XXXX .test03.qcw800.com/api/"3.http.js file code exportfunctionhttps(opts,data){lethttpDefaultOpts={url:opts.url,data:data,method:opts.method
Take you step by step to develop a uni-app calendar plug-in (and publish it)
Jun 30, 2022 pm 08:13 PM
This article will guide you step by step in developing a uni-app calendar plug-in, and introduce how the next calendar plug-in is developed from development to release. I hope it will be helpful to you!
Let's talk about how to use uniapp to develop a snake game!
May 20, 2022 pm 07:56 PM
How to use uniapp to develop a snake game? The following article will take you step by step to implement the Snake game in uniapp. I hope it will be helpful to you!
Detailed example of how uniapp implements phone recording function (with code)
Jan 05, 2023 pm 04:41 PM
This article brings you relevant knowledge about uniapp. It mainly introduces how to use uniapp to make calls and synchronize recording. Friends who are interested should take a look at it. I hope it will be helpful to everyone.
Examples to explain how uniapp implements the all-select function of multi-select boxes
Jun 22, 2022 am 11:57 AM
This article brings you relevant knowledge about uniapp, which mainly organizes the related issues of implementing the select-all function of the multi-select box. The reason why the select-all function cannot be implemented is that when the checked field of the checkbox is dynamically modified, the status on the interface can Real-time changes, but the change event of checkbox-group cannot be triggered. Let's take a look at it. I hope it will be helpful to everyone.
Let's talk about uniapp's scroll-view drop-down loading
Jul 14, 2022 pm 09:07 PM
How does uniapp implement scroll-view drop-down loading? The following article talks about the drop-down loading of the uniapp WeChat applet scroll-view. I hope it will be helpful to everyone!


