提交 6a772945 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

优化iOS插件示例

上级 3c5f0a7f
...@@ -14,8 +14,6 @@ export const getBatteryInfo : GetBatteryInfo = function (options) { ...@@ -14,8 +14,6 @@ export const getBatteryInfo : GetBatteryInfo = function (options) {
// 返回数据 // 返回数据
const res : GetBatteryInfoSuccess = { const res : GetBatteryInfoSuccess = {
errCode: 0,
errSubject: "uni-getBatteryInfo",
errMsg: "getBatteryInfo:ok", errMsg: "getBatteryInfo:ok",
level: new Int(UIDevice.current.batteryLevel * 100), level: new Int(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
......
...@@ -2,11 +2,13 @@ import { NotificationCenter } from 'Foundation'; ...@@ -2,11 +2,13 @@ import { NotificationCenter } from 'Foundation';
import { UIApplication } from "UIKit" import { UIApplication } from "UIKit"
import { Selector } from "ObjectiveC" import { Selector } from "ObjectiveC"
import { OffMemoryWarning, OnMemoryWarning, OnMemoryWarningCallback, OnMemoryWarningCallbackResult } from "../interface.uts"
class MemoryWarningTool { class MemoryWarningTool {
static listeners: UTSCallback[] = [] static listeners: OnMemoryWarningCallback[] = []
// 监听内存警告 // 监听内存警告
static listenMemoryWarning(callback: UTSCallback) { static listenMemoryWarning(callback: OnMemoryWarningCallback) {
// 只有首次才需要注册监听事件 // 只有首次才需要注册监听事件
if (this.listeners.length == 0) { if (this.listeners.length == 0) {
...@@ -23,12 +25,13 @@ class MemoryWarningTool { ...@@ -23,12 +25,13 @@ class MemoryWarningTool {
@objc static receiveMemoryWarning() { @objc static receiveMemoryWarning() {
// 触发回调 // 触发回调
this.listeners.forEach(listener => { this.listeners.forEach(listener => {
listener({}) const ret: OnMemoryWarningCallbackResult = {}
listener(ret)
}) })
} }
// 移除监听事件 // 移除监听事件
static removeListen(callback: UTSCallback | null) { static removeListen(callback: OnMemoryWarningCallback | null) {
// 移除所有监听 // 移除所有监听
if (callback == null) { if (callback == null) {
this.listeners = [] this.listeners = []
...@@ -46,11 +49,11 @@ class MemoryWarningTool { ...@@ -46,11 +49,11 @@ class MemoryWarningTool {
} }
// 开启监听内存警告 // 开启监听内存警告
export function onMemoryWarning(callback: UTSCallback) { export const onMemoryWarning : OnMemoryWarning = function (callback: OnMemoryWarningCallback) {
MemoryWarningTool.listenMemoryWarning(callback) MemoryWarningTool.listenMemoryWarning(callback)
} }
// 关闭监听内存警告 // 关闭监听内存警告
export function offMemoryWarning(callback: UTSCallback | null) { export const offMemoryWarning : OffMemoryWarning = function (callback: OnMemoryWarningCallback | null) {
MemoryWarningTool.removeListen(callback) MemoryWarningTool.removeListen(callback)
} }
\ No newline at end of file
export type OnMemoryWarningCallbackResult = {
level : number | null
}
export type OnMemoryWarningCallback = (res : OnMemoryWarningCallbackResult) => void
export type OnMemoryWarning = (callback : OnMemoryWarningCallback) => void
export type OffMemoryWarning = (callback : OnMemoryWarningCallback | null) => void
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册