提交 929f308b 编写于 作者: lizhongyi_'s avatar lizhongyi_

feat: iOS showModal 插件

上级 432d69d0
{ // launch.json 配置了启动调试时相关设置,configurations下节点名称可为 app-plus/h5/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp-360/
// launchtype项可配置值为local或remote, local代表前端连本地云函数,remote代表前端连云端云函数
"version": "0.0",
"configurations": [{
"app-plus" :
{
"launchtype" : "local"
},
"default" :
{
"launchtype" : "local"
},
"type" : "uniCloud"
}
{
// launch.json 配置了启动调试时相关设置,configurations下节点名称可为 app-plus/h5/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp-360/
// launchtype项可配置值为local或remote, local代表前端连本地云函数,remote代表前端连云端云函数
"version" : "0.0",
"configurations" : [
{
"app-plus" : {
"launchtype" : "local"
},
"default" : {
"launchtype" : "local"
},
"type" : "uniCloud"
},
{
"bundleId" : "io.dcloud.test123456",
"certificateFile" : "/Users/lizhongyi/Desktop/文件/证书/dev_123.p12",
"certificateProfileFile" : "/Users/lizhongyi/Desktop/文件/证书/io_dcloud_test123456.mobileprovision",
"type" : "uni-app:app-ios"
}
]
}
{
"name" : "uni-api",
"appid" : "__UNI__4F60974",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
"transformPx" : false,
/* 5+App特有相关 */
"app-plus" : {
"usingComponents" : true,
"nvueStyleCompiler" : "uni-app",
"compilerVersion" : 3,
"splashscreen" : {
"alwaysShowBeforeRender" : true,
"waiting" : true,
"autoclose" : true,
"delay" : 0
},
/* 模块配置 */
"modules" : {},
/* 应用发布信息 */
"distribute" : {
/* android打包配置 */
"android" : {
"permissions" : [
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",
"<uses-permission android:name=\"android.permission.VIBRATE\"/>",
"<uses-permission android:name=\"android.permission.READ_LOGS\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",
"<uses-feature android:name=\"android.hardware.camera.autofocus\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>",
"<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",
"<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>",
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
"<uses-feature android:name=\"android.hardware.camera\"/>",
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>"
]
},
/* ios打包配置 */
"ios" : {},
/* SDK配置 */
"sdkConfigs" : {}
}
},
/* 快应用特有相关 */
"quickapp" : {},
/* 小程序特有相关 */
"mp-weixin" : {
"appid" : "",
"setting" : {
"urlCheck" : false
},
"usingComponents" : true
},
"mp-alipay" : {
"usingComponents" : true
},
"mp-baidu" : {
"usingComponents" : true
},
"mp-toutiao" : {
"usingComponents" : true
},
"uniStatistics" : {
"enable" : false
},
"vueVersion" : "2"
{
"name": "uni-api",
"appid": "__UNI__ED9218B",
"description": "",
"versionName": "1.0.0",
"versionCode": "100",
"transformPx": false,
/* 5+App特有相关 */
"app-plus": {
"usingComponents": true,
"nvueStyleCompiler": "uni-app",
"compilerVersion": 3,
"splashscreen": {
"alwaysShowBeforeRender": true,
"waiting": true,
"autoclose": true,
"delay": 0
},
/* 模块配置 */
"modules": {},
/* 应用发布信息 */
"distribute": {
/* android打包配置 */
"android": {
"permissions": [
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",
"<uses-permission android:name=\"android.permission.VIBRATE\"/>",
"<uses-permission android:name=\"android.permission.READ_LOGS\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",
"<uses-feature android:name=\"android.hardware.camera.autofocus\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>",
"<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",
"<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>",
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
"<uses-feature android:name=\"android.hardware.camera\"/>",
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>"
]
},
/* ios打包配置 */
"ios": {},
/* SDK配置 */
"sdkConfigs": {}
}
},
/* 快应用特有相关 */
"quickapp": {},
/* 小程序特有相关 */
"mp-weixin": {
"appid": "",
"setting": {
"urlCheck": false
},
"usingComponents": true
},
"mp-alipay": {
"usingComponents": true
},
"mp-baidu": {
"usingComponents": true
},
"mp-toutiao": {
"usingComponents": true
},
"uniStatistics": {
"enable": false
},
"vueVersion": "2"
}
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<button @tap="testScreenShotListen">开启截屏监听</button>
<button @tap="testScreenShotOff">关闭截屏监听</button>
<button @tap="testGetBatteryInfo">获取电池电量</button>
<button @tap="testonMemoryWarning">开启内存不足告警监听</button>
<button @tap="testoffMemoryWarning">关闭内存不足告警监听</button>
<button @tap="testStartWifi">初始化wifi模块</button>
<button @tap="testGetWifiList">获取当前wifi列表</button>
<button @tap="testGetConnnectWifi">获取当前连接的wifi</button>
<button @tap="testConnnectWifi">链接wifi</button>
<button @tap="testStopWifi">关闭wifi模块</button>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello',
memListener:null,
}
},
onLoad() {
},
methods: {
onMemoryWarning:function(res){
console.log(res);
},
testConnnectWifi(){
uni.connectWifi({
maunal:false,
SSID:"Xiaomi_20D0",
password:"BBBB",
complete:(res)=>{
console.log(res);
}
});
},
testGetConnnectWifi(){
uni.getConnectedWifi({
partialInfo:false,
complete:(res)=>{
console.log(res);
if (res.errCode == 0) {
uni.showToast({
icon:'none',
title:res.wifi.SSID
})
} else{
uni.showToast({
icon:'none',
title:res.errMsg
})
}
}
});
},
testStartWifi(){
uni.startWifi({
success:(res)=> {
console.log("success: " + JSON.stringify(res));
// wifi 开启成功后,注册wifi链接状态监听和wifi列表获取监听
uni.onGetWifiList(function(res){
console.log("onGetWifiList");
console.log(res);
});
uni.onWifiConnected(function(res){
console.log("onWifiConnected");
console.log(res);
});
uni.onWifiConnectedWithPartialInfo(function(res){
console.log("onWifiConnectedWithPartialInfo");
console.log(res);
});
},fail:(res)=>{
console.log("fail: " + JSON.stringify(res));
},complete:(res)=>{
console.log("complete: " + JSON.stringify(res));
}
})
},
testStopWifi() {
uni.stopWifi({
success:(res)=> {
console.log("success: " + JSON.stringify(res));
},fail:(res)=>{
console.log("fail: " + JSON.stringify(res));
},complete:(res)=>{
console.log("complete: " + JSON.stringify(res));
}
})
},
testGetWifiList() {
uni.getWifiList({
success:(res)=> {
console.log("success: " + JSON.stringify(res));
},fail:(res)=>{
console.log("fail: " + JSON.stringify(res));
},complete:(res)=>{
console.log("complete: " + JSON.stringify(res));
}
})
},
testonMemoryWarning() {
uni.onMemoryWarning(this.onMemoryWarning)
uni.showToast({
icon:'none',
title:'已监听,注意控制台输出'
})
},
testoffMemoryWarning(){
uni.offMemoryWarning(this.onMemoryWarning)
uni.showToast({
icon:'none',
title:'监听已移除'
})
},
testScreenShotListen() {
var that = this;
uni.onUserCaptureScreen(function(res) {
console.log(res);
if (uni.getSystemInfoSync().platform == "android") {
// 除android 之外的平台,不需要判断返回状态码
if(res.errCode == -1){
// 启动失败
return ;
}else if(res.errCode == 0){
uni.showToast({
icon:"none",
title:'截屏监听已开启'
})
}else {
uni.showToast({
icon:"none",
title:'捕获截屏事件'
})
that.screenImage = res.image
}
}else{
// 除android 之外的平台,不需要判断返回状态码
uni.showToast({
icon:"none",
title:'捕获截屏事件'
})
}
});
if (uni.getSystemInfoSync().platform != "android") {
// 除android 之外的平台,直接提示监听已开启
uni.showToast({
icon:"none",
title:'截屏监听已开启'
})
}
},
testScreenShotOff() {
uni.offUserCaptureScreen(function(res) {
console.log(res);
});
// 提示已经开始监听,注意观察
uni.showToast({
icon:"none",
title:'截屏监听已关闭'
})
},
testGetBatteryInfo() {
uni.getBatteryInfo({
success(res) {
console.log(res);
uni.showToast({
title: "当前电量:" + res.level + '%',
icon: 'none'
});
}
})
},
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<button @tap="testScreenShotListen">开启截屏监听</button>
<button @tap="testScreenShotOff">关闭截屏监听</button>
<button @tap="testGetBatteryInfo">获取电池电量</button>
<button @tap="testonMemoryWarning">开启内存不足告警监听</button>
<button @tap="testoffMemoryWarning">关闭内存不足告警监听</button>
<button @tap="testShowModal">模态弹窗</button>
<button @tap="testShowModalWithTF">模态弹窗带输入框</button>
<button @tap="testStartWifi">初始化wifi模块</button>
<button @tap="testGetWifiList">获取当前wifi列表</button>
<button @tap="testGetConnnectWifi">获取当前连接的wifi</button>
<button @tap="testConnnectWifi">链接wifi</button>
<button @tap="testStopWifi">关闭wifi模块</button>
</view>
</template>
<script>
import showModal from '@/uni_modules/uni-showmodal';
export default {
data() {
return {
title: 'Hello',
memListener: null,
}
},
onLoad() {
},
methods: {
onMemoryWarning: function(res) {
console.log(res);
},
testShowModal() {
showModal({
title: "温馨提示",
content: "这是一个模态弹窗",
cancelColor: "#ff0000",
confirmColor: "#00ff00",
showCancel: true,
success: function(res) {
console.log(res)
}
})
},
testShowModalWithTF() {
showModal({
title: "提示",
content: "带输入框的弹窗",
showCancel: false,
editable: true,
placeholderText: "请输入内容",
success: function(res) {
console.log(res)
}
})
},
testConnnectWifi() {
uni.connectWifi({
maunal: false,
SSID: "Xiaomi_20D0",
password: "BBBB",
complete: (res) => {
console.log(res);
}
});
},
testGetConnnectWifi() {
uni.getConnectedWifi({
partialInfo: false,
complete: (res) => {
console.log(res);
if (res.errCode == 0) {
uni.showToast({
icon: 'none',
title: res.wifi.SSID
})
} else {
uni.showToast({
icon: 'none',
title: res.errMsg
})
}
}
});
},
testStartWifi() {
uni.startWifi({
success: (res) => {
console.log("success: " + JSON.stringify(res));
// wifi 开启成功后,注册wifi链接状态监听和wifi列表获取监听
uni.onGetWifiList(function(res) {
console.log("onGetWifiList");
console.log(res);
});
uni.onWifiConnected(function(res) {
console.log("onWifiConnected");
console.log(res);
});
uni.onWifiConnectedWithPartialInfo(function(res) {
console.log("onWifiConnectedWithPartialInfo");
console.log(res);
});
},
fail: (res) => {
console.log("fail: " + JSON.stringify(res));
},
complete: (res) => {
console.log("complete: " + JSON.stringify(res));
}
})
},
testStopWifi() {
uni.stopWifi({
success: (res) => {
console.log("success: " + JSON.stringify(res));
},
fail: (res) => {
console.log("fail: " + JSON.stringify(res));
},
complete: (res) => {
console.log("complete: " + JSON.stringify(res));
}
})
},
testGetWifiList() {
uni.getWifiList({
success: (res) => {
console.log("success: " + JSON.stringify(res));
},
fail: (res) => {
console.log("fail: " + JSON.stringify(res));
},
complete: (res) => {
console.log("complete: " + JSON.stringify(res));
}
})
},
testonMemoryWarning() {
uni.onMemoryWarning(this.onMemoryWarning)
uni.showToast({
icon: 'none',
title: '已监听,注意控制台输出'
})
},
testoffMemoryWarning() {
uni.offMemoryWarning(this.onMemoryWarning)
uni.showToast({
icon: 'none',
title: '监听已移除'
})
},
testScreenShotListen() {
var that = this;
uni.onUserCaptureScreen(function(res) {
console.log(res);
if (uni.getSystemInfoSync().platform == "android") {
// 除android 之外的平台,不需要判断返回状态码
if (res.errCode == -1) {
// 启动失败
return;
} else if (res.errCode == 0) {
uni.showToast({
icon: "none",
title: '截屏监听已开启'
})
} else {
uni.showToast({
icon: "none",
title: '捕获截屏事件'
})
that.screenImage = res.image
}
} else {
// 除android 之外的平台,不需要判断返回状态码
uni.showToast({
icon: "none",
title: '捕获截屏事件'
})
}
});
if (uni.getSystemInfoSync().platform != "android") {
// 除android 之外的平台,直接提示监听已开启
uni.showToast({
icon: "none",
title: '截屏监听已开启'
})
}
},
testScreenShotOff() {
uni.offUserCaptureScreen(function(res) {
console.log(res);
});
// 提示已经开始监听,注意观察
uni.showToast({
icon: "none",
title: '截屏监听已关闭'
})
},
testGetBatteryInfo() {
uni.getBatteryInfo({
success(res) {
console.log(res);
uni.showToast({
title: "当前电量:" + res.level + '%',
icon: 'none'
});
}
})
},
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
{
"id": "uni-showmodal",
"displayName": "uni-showmodal",
"version": "1.0.0",
"description": "uni-showmodal",
"keywords": [
"uni-showmodal"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.8"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-android": "u",
"app-ios": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uni-showmodal
### 开发文档
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
[UTS 原生插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
[Hello UTS](https://gitcode.net/dcloud/hello-uts/-/tree/dev)
\ No newline at end of file
import { UIAlertController, UIAlertAction, UITextField } from "UIKit"
import { UTSiOS } from "DCloudUTSFoundation"
import { DispatchQueue } from 'Dispatch';
type ShowModalResult = {
errCode: number,
errSubject: string,
errMsg: string,
confirm: boolean,
cancel: boolean,
content: string
}
type ShowModalOptions = {
title?: string,
content?: string,
showCancel: boolean,
cancelText?: string,
cancelColor?: string,
confirmText?: string,
confirmColor?: string,
editable: boolean,
placeholderText?: string,
success?: (res: ShowModalResult) => void,
fail?: (res: ShowModalResult) => void,
complete?: (res: ShowModalResult) => void
}
export default function showModal(options: ShowModalOptions) {
const title = options.title == null ? "" : options.title!
const message = options.content == null ? "" : options.content!
const showCancel = options.showCancel == null ? true : (options.showCancel != false)
const cancelText = options.cancelText == null ? "取消" : options.cancelText!
const confirmText = options.confirmText == null ? "确定" : options.confirmText!
const canEidt = options.editable == true
const placeholderText = options.placeholderText == null ? "" : options.placeholderText!
// uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
DispatchQueue.main.async(execute = (): void => {
// 初始化 UIAlertController 实例对象 alert
let alert = new UIAlertController(title = title, message = message, preferredStyle = UIAlertController.Style.alert)
if (canEidt == true) {
// 在 alert 上添加输入框
console.log("需要加上输入框")
alert.addTextField(configurationHandler = (tf: UITextField): void => {
// 添加成功的回调
// 设置输入框的 placeholder
tf.placeholder = placeholderText
})
}
// 创建 UIAlertAction 按钮
let okAction = new UIAlertAction(title = confirmText, style = UIAlertAction.Style.default, handler = (action: UIAlertAction): void => {
// 获取输入框中的内容
let inputText = ""
if (canEidt == true) {
let tf = alert.textFields?.[0]
if (tf != null) {
inputText = tf!.text != null ? tf!.text! : ""
}
}
// 点击按钮的回调方法
const res = new ShowModalResult()
res.errCode = 0
res.errSubject = ""
res.errMsg = ""
res.confirm = false
res.cancel = true
res.content = inputText
options.success?.(res)
options.complete?.(res)
})
if (options.confirmColor != null) {
const color = UTSiOS.colorWithString(options.confirmColor!)
okAction.setValue(color, forKey = "titleTextColor")
}
// 将 UIAlertAction 添加到 alert 上
alert.addAction(okAction)
if (showCancel == true) {
// 创建 UIAlertAction 按钮
let cancelAction = new UIAlertAction(title = cancelText, style = UIAlertAction.Style.cancel, handler = (action: UIAlertAction): void => {
// 点击按钮的回调方法
const res = new ShowModalResult()
res.errCode = 0
res.errSubject = ""
res.errMsg = ""
res.confirm = false
res.cancel = true
res.content = ""
options.success?.(res)
options.complete?.(res)
})
if (options.cancelColor != null) {
const color = UTSiOS.colorWithString(options.cancelColor!)
cancelAction.setValue(color, forKey = "titleTextColor")
}
alert.addAction(cancelAction)
}
// 打开 alert 弹窗
UTSiOS.getCurrentViewController().present(alert, animated = true)
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册