注册一个全局通知(nodeView.registerSystemNotifiation)
函数
注册是否成功 = nodeView.registerSystemNotifiation(通知id,回调函数(通知id, 节点对象, 信息))
##参数
- 通知id
整数型 - 回调函数
函数型- 通知id
整数型 - 节点对象
对象型 - 信息
数组型
- 通知id
返回值
- 注册是否成功
布尔型
说明
用于注册一个全局通知. 当系统出现相应通知时, 会调用回调函数.
回调函数中的 通知id: 当前注册监听的通知id, 节点对象: 触发该通知的节点对象 该值可能为nil, 信息: 触发该通知的额外信息 该值可能为nil
此方法运行在一个单独的线程中, 只用使用sys.msleep 函数让出线程调度, 该方法的回调函数才能生效.
此方法只能在 3.0.3以上版本中使用
通知id 说明
- 1000 表示 视图控制器发生改变
- 1005 表示 属性发生改变 该通知在每个系统版本中 可能存在区别
- 1008 不知道怎么解释 可捕获通知 和 app切换器 等 每个系统版本效果 可能存在区别
- 1006 表示 系统弹窗 或APP弹窗 (UIAlertController 类或其子类 的视图)
- 1011 屏幕旋转方向发生改变 (需要ios16以上版本)
- 1004 屏幕锁定
- 1015 返回到了主界面 (需要ios18 或更高系统, ios17 可能也支持(作者没有17系统测试))
- 1031 键盘显示或影藏
- 1007 网页加载完成 (WebKit 框架的 app 的内嵌网页)
- 1072 网页状态发生改变
- 1069 滚动视图完成滚动
- 1020 视图移动
- 1039 屏幕亮起
示例
nodeView.registerSystemNotifiation( --注册一个 视图控制器发生改变通知
1000,
function(id, node, info)
--当接收到通知时 打印出当前通知ID ,如果节点信息存在 就打印出 触发该通知的应用bundldeId 否则打印nil
print(id, node and node:bundleId() or nil)
end
)
nodeView.registerSystemNotifiation( -- 注册一个 监听系统通知
1008,
function(id, node, info)
-- 当系统出现通知时, 会调用回调函数.
-- 回调函数中的 通知id: 当前注册监听的通知id, 节点对象: 触发该通知的节点对象 该值可能为nil, 信息: 触发该通知的额外信息 该值可能为nil
print(id, node and node:bundleId() or nil, info)
-- 如果是通知,info 信息中会携带 通知的消息内容
print(type(info) == "table" and info.data and info.data or "") --如果存在 data字段我们就打印出来
end
)
--以下是一个 处理系统弹窗的示例
nodeView.registerSystemNotifiation( -- 注册一个监控 系统或app弹窗
1006,
function(id, node, info)
-- 当系统或APP 弹出窗口时, 调用回调函数.
local nodes = node:match(function (info) --查找出弹窗的标题
if info.traits:find("alert") then
return true
end
end, false)
--获取弹窗的标题 如果存在的话.
local title = #nodes > 0 and nodes[1].text and nodes[1].text or nil
-- 把无用的字符删除掉
if title then
title = title:gsub("“.-”" ,""):gsub("?" ,"")
end
if (title == "允许使用无线数据") then --如果是网络授权弹窗
node:matchText("无线局域网与蜂窝网络",true)[1]:tap() --查找 "无线局域网与蜂窝网络" 按钮并点击它
end
end
)
-- 注册监听 app 启动事件
nodeView.registerSystemNotifiation(
5000,
function (id, node, info)
print(info.displayName, "被启动" );
end
)