提交 3ef95c0d 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

增加 iOS平台内存警告示例

上级 5a6e3423
/unpackage
.DS_Store
\ No newline at end of file
{
"deploymentTarget": "9"
"deploymentTarget": "9.0"
}
\ No newline at end of file
// 引用 iOS 原生平台 api
import { UIDevice } from "UIKit";
import { Int } from 'Swift';
/**
* 定义 接口参数
*/
type GetBatteryInfoOptions = {
success?: (res: UTSJSONObject) => void;
fail?: (res: UTSJSONObject) => void;
complete?: (res: UTSJSONObject) => void;
success?: (res: object) => void;
fail?: (res: object) => void;
complete?: (res: object) => void;
};
/**
* 导出 获取电量方法
*/
export default function getBatteryInfo(options: GetBatteryInfoOptions) {
// 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true
// 返回数据
const res = {
errMsg: "getBatteryInfo:ok",
level: Number(UIDevice.current.batteryLevel * 100),
level: new Int(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
};
options.success?.(res);
options.complete?.(res);
}
}
\ No newline at end of file
......@@ -2,14 +2,16 @@
### uni.onMemoryWarning(function listener)
监听内存不足告警事件(目前仅Android)
监听内存不足告警事件
Android 向小程序进程发出内存警告时,触发该事件。
系统应用进程发出内存警告时,触发该事件。
触发该事件不意味应用被杀,大部分情况下仅仅是告警,开发者可在收到通知后回收一些不必要资源。
返回值说明:
返回值说明:
**注意平台差异:仅Android平台有返回值,iOS平台无返回值**
```kotlin
......@@ -65,7 +67,7 @@ int TRIM_MEMORY_COMPLETE = 80;
### uni.offMemoryWarning(function listener)
移除内存不足告警事件的监听函数(目前仅Android)
移除内存不足告警事件的监听函数
onMemoryWarning 传入的监听函数。不传此参数则移除所有监听函数。
import { NotificationCenter } from 'Foundation';
import { UIApplication } from "UIKit"
class MemoryWarningTool {
static listener: UTSCallback | null;
// 监听截屏
static listenMemoryWarning(callback: UTSCallback | null) {
this.listener = callback
// 注册监听截屏事件及回调方法
// target-action 回调方法需要通过 Selector("方法名") 构建
const method = Selector("receiveMemoryWarning")
NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.didReceiveMemoryWarningNotification, object = null)
}
// 内存警告回调的方法
// target-action 的方法前需要添加 @objc 前缀
@objc static receiveMemoryWarning() {
// 回调
this.listener?.({})
}
// 移除监听事件
static removeListen(callback: UTSCallback | null) {
this.listener = null
NotificationCenter.default.removeObserver(this)
callback?.({})
}
}
// 开启监听内存警告
export function onMemoryWarning(callback: UTSCallback | null) {
MemoryWarningTool.listenMemoryWarning(callback)
}
// 关闭监听内存警告
export function offMemoryWarning(callback: UTSCallback | null) {
MemoryWarningTool.removeListen(callback)
}
\ No newline at end of file
import { NotificationCenter} from 'Foundation';
import { NotificationCenter } from 'Foundation';
import { UIApplication } from "UIKit"
/**
* 定义监听截屏事件工具类
*/
class CaptureScreenTool {
static listener?: UTSCallback;
static listener: UTSCallback | null;
// 监听截屏
static listenCaptureScreen(callback?: UTSCallback) {
static listenCaptureScreen(callback: UTSCallback | null) {
this.listener = callback
// 注册监听截屏事件及回调方法
......@@ -20,30 +20,28 @@ class CaptureScreenTool {
// 捕获截屏回调的方法
// target-action 的方法前需要添加 @objc 前缀
@objc static userDidTakeScreenshot() {
const obj = new UTSJSONObject()
// 回调
this.listener?.(obj)
this.listener?.({})
}
// 移除监听事件
static removeListen(callback?: UTSCallback) {
static removeListen(callback: UTSCallback | null) {
this.listener = null
NotificationCenter.default.removeObserver(this)
const obj = new UTSJSONObject()
callback?.(obj)
callback?.({})
}
}
/**
* 开启截图监听
*/
export function onUserCaptureScreen(callback?: UTSCallback) {
export function onUserCaptureScreen(callback: UTSCallback | null) {
CaptureScreenTool.listenCaptureScreen(callback)
}
/**
* 关闭截屏监听
*/
export function offUserCaptureScreen(callback?: UTSCallback) {
export function offUserCaptureScreen(callback: UTSCallback | null) {
CaptureScreenTool.removeListen(callback)
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册