iosapi.md 4.2 KB
Newer Older
1
> * 快速集成打包,可参考HBuilder工程
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
2
> * SDK基于Swift开发,因此原生Objective-C语言开发的应用需要新建一个Swift文件用于添加Swift运行环境以及桥接SDK的API,详情可参考UniAppXDemo工程中的`UniAppBridge`
3 4 5

## 初始化SDK
在 AppDelegate 的 didFinishLaunchingWithOptions 中初始化
6
``` swift
7 8 9 10 11
import DCloudUniappRuntime

UniSDKEngine.shared.create()
```
## 监听应用生命周期函数回调
12 13
在 AppDelegate 的 didFinishLaunchingWithOptions 调用
``` swift
14
UniSDKEngine.applicationDidFinishLaunchingWithOptions(application, launchOptions)
15
``` 
16
在 AppDelegate 的 applicationDidBecomeActive 调用
17
``` swift
18 19 20
UniSDKEngine.applicationDidBecomeActive(application)
```
在 AppDelegate 的 applicationWillResignActive 调用
21
``` swift
22 23 24
UniSDKEngine.applicationWillResignActive(application)
```
在 AppDelegate 的 applicationDidEnterBackground 调用
25
``` swift
26 27 28
UniSDKEngine.applicationDidEnterBackground(application)
```
在 AppDelegate 的 applicationWillEnterForeground 调用
29
``` swift
30 31 32
UniSDKEngine.applicationWillEnterForeground(application)
```
在 AppDelegate 的 openURL 调用
33
``` swift
34
UniSDKEngine.applicationOpenURLOptions(application, url, options)
35
``` 
36
在 AppDelegate 的 continueUserActivity 调用
37
``` swift
38 39 40 41 42
UniSDKEngine.applicationContinueUserActivityRestorationHandler(application, userActivity, restorationHandler)
```

## 监听推送相关函数回调(可选)
在 AppDelegate 的 didRegisterForRemoteNotificationsWithDeviceToken 调用
43
``` swift
44 45 46
UniSDKEngine.didRegisterForRemoteNotifications(deviceToken)
```
在 AppDelegate 的 didFailToRegisterForRemoteNotificationsWithError 调用
47
``` swift
48
UniSDKEngine.didFailToRegisterForRemoteNotifications(error)
49
``` 
50
在 AppDelegate 的 didReceiveRemoteNotification 调用
51
``` swift
52 53 54 55
UniSDKEngine.applicationDidReceiveRemoteNotificationCompletionHandler(application, userInfo, completionHandler)
```

## 开始渲染
56
``` swift
57 58 59 60 61
UniSDKEngine.shared.render(toView: toView)
```


# 通信
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
iOS平台目前不支持直接在uvue页面调用原生API,开发者可通过UTS插件`发送/接收 通知消息`实现与原生App通信,具体实现代码如下:

## 原生APP向SDK发消息
UTS插件添加监听:
``` ts
const name = "com.ios.notification.name1"; //通知消息标识
const notificationName = new Notification.Name(name);
const method = Selector("handleReceiveMessage:")//接收通知消息的方法名
NotificationCenter.default.addObserver(this, selector = method, name = notificationName, object = null)
```

``` ts
@objc static handleReceiveMessage(notification : Notification) {
	let userInfo = notification.userInfo
	if(userInfo != null){
		const message = userInfo!["msg"];
	}
}
```

原生发送通知消息:
``` swift
let name = "com.ios.notification.name1"; //通知消息标识
let message = "消息内容";
let userInfo: [AnyHashable: Any] = [
    "msg": message // 你可以在这里放置任何需要传递的信息
]
NotificationCenter.default.post(name: Notification.Name(name), object: nil, userInfo: userInfo)
```    


## SDK向原生APP发消息
原生添加通知监听:
``` swift
const name = "com.ios.notification.name2"; //通知消息标识
NotificationCenter.default.addObserver(self, selector: #selector(handleNotification(_:)), name: Notification.Name(name), object: nil)
```

``` swift
@objc func handleNotification(_ notification: Notification) {
    if let message = (notification.userInfo?["msg"] as? String) {
        // 使用message
        print(message)
    }
}
```
   
UTS插件发送消息:
``` ts
const name = "com.ios.notification.name2"; //通知消息标识
const message = "消息内容";
const notificationName = new Notification.Name(name);
const userInfo = new Map<string,any>()
userInfo.set("msg", message); // 你可以在这里放置任何需要传递的信息
NotificationCenter.default.post(name = notificationName, object = null, userInfo = userInfo);
```

> 注意:消息接收方必须在发送通知前添加监听事件,否则收不到消息 

## 运行示例
`UniAppXDemo`工程中,`__UNI__00DC103`为通信示例资源文件,将`Info.plist``uniapp-x`节点下的`appid`改为`__UNI__00DC103`,并添加`unimoduleTestIosNotification.xcframework`依赖,即可体验通信示例