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

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

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

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

54
## 跳转页面
55
``` swift
56 57 58 59 60 61
if UniSDKEngine.shared.getAppManager()?.getCurrentApp() == nil {
    // uni.exit() 方法会销毁app,所以在这里需要判断currentApp是否为空
    UniSDKEngine.shared.getAppManager()?.create()
}
let viewController = UniAppRootViewController()
self.navigationController?.pushViewController(viewController, animated: true)
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 123 124 125 126
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`依赖,即可体验通信示例