提交 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代表前端连云端云函数 // launch.json 配置了启动调试时相关设置,configurations下节点名称可为 app-plus/h5/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp-360/
"version": "0.0", // launchtype项可配置值为local或remote, local代表前端连本地云函数,remote代表前端连云端云函数
"configurations": [{ "version" : "0.0",
"app-plus" : "configurations" : [
{ {
"launchtype" : "local" "app-plus" : {
}, "launchtype" : "local"
"default" : },
{ "default" : {
"launchtype" : "local" "launchtype" : "local"
}, },
"type" : "uniCloud" "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", "name": "uni-api",
"appid" : "__UNI__4F60974", "appid": "__UNI__ED9218B",
"description" : "", "description": "",
"versionName" : "1.0.0", "versionName": "1.0.0",
"versionCode" : "100", "versionCode": "100",
"transformPx" : false, "transformPx": false,
/* 5+App特有相关 */ /* 5+App特有相关 */
"app-plus" : { "app-plus": {
"usingComponents" : true, "usingComponents": true,
"nvueStyleCompiler" : "uni-app", "nvueStyleCompiler": "uni-app",
"compilerVersion" : 3, "compilerVersion": 3,
"splashscreen" : { "splashscreen": {
"alwaysShowBeforeRender" : true, "alwaysShowBeforeRender": true,
"waiting" : true, "waiting": true,
"autoclose" : true, "autoclose": true,
"delay" : 0 "delay": 0
}, },
/* 模块配置 */ /* 模块配置 */
"modules" : {}, "modules": {},
/* 应用发布信息 */ /* 应用发布信息 */
"distribute" : { "distribute": {
/* android打包配置 */ /* android打包配置 */
"android" : { "android": {
"permissions" : [ "permissions": [
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>", "<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>", "<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",
"<uses-permission android:name=\"android.permission.VIBRATE\"/>", "<uses-permission android:name=\"android.permission.VIBRATE\"/>",
"<uses-permission android:name=\"android.permission.READ_LOGS\"/>", "<uses-permission android:name=\"android.permission.READ_LOGS\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>", "<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",
"<uses-feature android:name=\"android.hardware.camera.autofocus\"/>", "<uses-feature android:name=\"android.hardware.camera.autofocus\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>", "<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.CAMERA\"/>", "<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>", "<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>",
"<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>", "<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",
"<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>", "<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>",
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>", "<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>", "<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
"<uses-feature android:name=\"android.hardware.camera\"/>", "<uses-feature android:name=\"android.hardware.camera\"/>",
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>" "<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>"
] ]
}, },
/* ios打包配置 */ /* ios打包配置 */
"ios" : {}, "ios": {},
/* SDK配置 */ /* SDK配置 */
"sdkConfigs" : {} "sdkConfigs": {}
} }
}, },
/* 快应用特有相关 */ /* 快应用特有相关 */
"quickapp" : {}, "quickapp": {},
/* 小程序特有相关 */ /* 小程序特有相关 */
"mp-weixin" : { "mp-weixin": {
"appid" : "", "appid": "",
"setting" : { "setting": {
"urlCheck" : false "urlCheck": false
}, },
"usingComponents" : true "usingComponents": true
}, },
"mp-alipay" : { "mp-alipay": {
"usingComponents" : true "usingComponents": true
}, },
"mp-baidu" : { "mp-baidu": {
"usingComponents" : true "usingComponents": true
}, },
"mp-toutiao" : { "mp-toutiao": {
"usingComponents" : true "usingComponents": true
}, },
"uniStatistics" : { "uniStatistics": {
"enable" : false "enable": false
}, },
"vueVersion" : "2" "vueVersion": "2"
} }
<template> <template>
<view class="content"> <view class="content">
<image class="logo" src="/static/logo.png"></image> <image class="logo" src="/static/logo.png"></image>
<view class="text-area"> <view class="text-area">
<text class="title">{{title}}</text> <text class="title">{{title}}</text>
</view> </view>
<button @tap="testScreenShotListen">开启截屏监听</button> <button @tap="testScreenShotListen">开启截屏监听</button>
<button @tap="testScreenShotOff">关闭截屏监听</button> <button @tap="testScreenShotOff">关闭截屏监听</button>
<button @tap="testGetBatteryInfo">获取电池电量</button> <button @tap="testGetBatteryInfo">获取电池电量</button>
<button @tap="testonMemoryWarning">开启内存不足告警监听</button> <button @tap="testonMemoryWarning">开启内存不足告警监听</button>
<button @tap="testoffMemoryWarning">关闭内存不足告警监听</button> <button @tap="testoffMemoryWarning">关闭内存不足告警监听</button>
<button @tap="testShowModal">模态弹窗</button>
<button @tap="testStartWifi">初始化wifi模块</button> <button @tap="testShowModalWithTF">模态弹窗带输入框</button>
<button @tap="testGetWifiList">获取当前wifi列表</button>
<button @tap="testGetConnnectWifi">获取当前连接的wifi</button> <button @tap="testStartWifi">初始化wifi模块</button>
<button @tap="testConnnectWifi">链接wifi</button> <button @tap="testGetWifiList">获取当前wifi列表</button>
<button @tap="testStopWifi">关闭wifi模块</button> <button @tap="testGetConnnectWifi">获取当前连接的wifi</button>
</view> <button @tap="testConnnectWifi">链接wifi</button>
</template> <button @tap="testStopWifi">关闭wifi模块</button>
</view>
<script> </template>
export default { <script>
data() { import showModal from '@/uni_modules/uni-showmodal';
return { export default {
title: 'Hello', data() {
memListener:null, return {
} title: 'Hello',
}, memListener: null,
onLoad() { }
},
}, onLoad() {
methods: {
onMemoryWarning:function(res){ },
console.log(res); methods: {
}, onMemoryWarning: function(res) {
testConnnectWifi(){ console.log(res);
},
uni.connectWifi({ testShowModal() {
maunal:false, showModal({
SSID:"Xiaomi_20D0", title: "温馨提示",
password:"BBBB", content: "这是一个模态弹窗",
complete:(res)=>{ cancelColor: "#ff0000",
console.log(res); confirmColor: "#00ff00",
} showCancel: true,
}); success: function(res) {
console.log(res)
}, }
testGetConnnectWifi(){ })
uni.getConnectedWifi({ },
partialInfo:false, testShowModalWithTF() {
complete:(res)=>{ showModal({
console.log(res); title: "提示",
if (res.errCode == 0) { content: "带输入框的弹窗",
uni.showToast({ showCancel: false,
icon:'none', editable: true,
title:res.wifi.SSID placeholderText: "请输入内容",
}) success: function(res) {
} else{ console.log(res)
uni.showToast({ }
icon:'none', })
title:res.errMsg },
}) testConnnectWifi() {
}
uni.connectWifi({
} maunal: false,
}); SSID: "Xiaomi_20D0",
}, password: "BBBB",
testStartWifi(){ complete: (res) => {
uni.startWifi({ console.log(res);
success:(res)=> { }
console.log("success: " + JSON.stringify(res)); });
// wifi 开启成功后,注册wifi链接状态监听和wifi列表获取监听
uni.onGetWifiList(function(res){ },
console.log("onGetWifiList"); testGetConnnectWifi() {
console.log(res); uni.getConnectedWifi({
}); partialInfo: false,
uni.onWifiConnected(function(res){ complete: (res) => {
console.log("onWifiConnected"); console.log(res);
console.log(res); if (res.errCode == 0) {
}); uni.showToast({
uni.onWifiConnectedWithPartialInfo(function(res){ icon: 'none',
console.log("onWifiConnectedWithPartialInfo"); title: res.wifi.SSID
console.log(res); })
}); } else {
uni.showToast({
},fail:(res)=>{ icon: 'none',
console.log("fail: " + JSON.stringify(res)); title: res.errMsg
},complete:(res)=>{ })
console.log("complete: " + JSON.stringify(res)); }
}
}) }
}, });
testStopWifi() { },
uni.stopWifi({ testStartWifi() {
success:(res)=> { uni.startWifi({
console.log("success: " + JSON.stringify(res)); success: (res) => {
},fail:(res)=>{ console.log("success: " + JSON.stringify(res));
console.log("fail: " + JSON.stringify(res)); // wifi 开启成功后,注册wifi链接状态监听和wifi列表获取监听
},complete:(res)=>{ uni.onGetWifiList(function(res) {
console.log("complete: " + JSON.stringify(res)); console.log("onGetWifiList");
} console.log(res);
}) });
uni.onWifiConnected(function(res) {
}, console.log("onWifiConnected");
testGetWifiList() { console.log(res);
uni.getWifiList({ });
success:(res)=> { uni.onWifiConnectedWithPartialInfo(function(res) {
console.log("success: " + JSON.stringify(res)); console.log("onWifiConnectedWithPartialInfo");
},fail:(res)=>{ console.log(res);
console.log("fail: " + JSON.stringify(res)); });
},complete:(res)=>{
console.log("complete: " + JSON.stringify(res)); },
} fail: (res) => {
}) console.log("fail: " + JSON.stringify(res));
},
}, complete: (res) => {
testonMemoryWarning() { console.log("complete: " + JSON.stringify(res));
uni.onMemoryWarning(this.onMemoryWarning) }
uni.showToast({ })
icon:'none', },
title:'已监听,注意控制台输出' testStopWifi() {
}) uni.stopWifi({
}, success: (res) => {
testoffMemoryWarning(){ console.log("success: " + JSON.stringify(res));
uni.offMemoryWarning(this.onMemoryWarning) },
uni.showToast({ fail: (res) => {
icon:'none', console.log("fail: " + JSON.stringify(res));
title:'监听已移除' },
}) complete: (res) => {
}, console.log("complete: " + JSON.stringify(res));
testScreenShotListen() { }
var that = this; })
uni.onUserCaptureScreen(function(res) {
console.log(res); },
testGetWifiList() {
if (uni.getSystemInfoSync().platform == "android") { uni.getWifiList({
// 除android 之外的平台,不需要判断返回状态码 success: (res) => {
if(res.errCode == -1){ console.log("success: " + JSON.stringify(res));
// 启动失败 },
return ; fail: (res) => {
}else if(res.errCode == 0){ console.log("fail: " + JSON.stringify(res));
uni.showToast({ },
icon:"none", complete: (res) => {
title:'截屏监听已开启' console.log("complete: " + JSON.stringify(res));
}) }
}else { })
uni.showToast({
icon:"none", },
title:'捕获截屏事件' testonMemoryWarning() {
}) uni.onMemoryWarning(this.onMemoryWarning)
that.screenImage = res.image uni.showToast({
} icon: 'none',
}else{ title: '已监听,注意控制台输出'
// 除android 之外的平台,不需要判断返回状态码 })
uni.showToast({ },
icon:"none", testoffMemoryWarning() {
title:'捕获截屏事件' uni.offMemoryWarning(this.onMemoryWarning)
}) uni.showToast({
} icon: 'none',
title: '监听已移除'
}); })
},
if (uni.getSystemInfoSync().platform != "android") { testScreenShotListen() {
// 除android 之外的平台,直接提示监听已开启 var that = this;
uni.showToast({ uni.onUserCaptureScreen(function(res) {
icon:"none", console.log(res);
title:'截屏监听已开启'
}) if (uni.getSystemInfoSync().platform == "android") {
} // 除android 之外的平台,不需要判断返回状态码
}, if (res.errCode == -1) {
testScreenShotOff() { // 启动失败
uni.offUserCaptureScreen(function(res) { return;
console.log(res); } else if (res.errCode == 0) {
}); uni.showToast({
// 提示已经开始监听,注意观察 icon: "none",
uni.showToast({ title: '截屏监听已开启'
icon:"none", })
title:'截屏监听已关闭' } else {
}) uni.showToast({
}, icon: "none",
testGetBatteryInfo() { title: '捕获截屏事件'
uni.getBatteryInfo({ })
success(res) { that.screenImage = res.image
console.log(res); }
uni.showToast({ } else {
title: "当前电量:" + res.level + '%', // 除android 之外的平台,不需要判断返回状态码
icon: 'none' uni.showToast({
}); icon: "none",
} title: '捕获截屏事件'
}) })
}, }
}
} });
</script>
if (uni.getSystemInfoSync().platform != "android") {
<style> // 除android 之外的平台,直接提示监听已开启
.content { uni.showToast({
display: flex; icon: "none",
flex-direction: column; title: '截屏监听已开启'
align-items: center; })
justify-content: center; }
} },
testScreenShotOff() {
.logo { uni.offUserCaptureScreen(function(res) {
height: 200rpx; console.log(res);
width: 200rpx; });
margin-top: 200rpx; // 提示已经开始监听,注意观察
margin-left: auto; uni.showToast({
margin-right: auto; icon: "none",
margin-bottom: 50rpx; title: '截屏监听已关闭'
} })
},
.text-area { testGetBatteryInfo() {
display: flex; uni.getBatteryInfo({
justify-content: center; success(res) {
} console.log(res);
uni.showToast({
.title { title: "当前电量:" + res.level + '%',
font-size: 36rpx; icon: 'none'
color: #8f8f94; });
} }
})
},
}
}
</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> </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.
先完成此消息的编辑!
想要评论请 注册