Skip to main content

Custom Events System

The custom events system is a powerful feature that enables bidirectional data communication between a DPApp and the host app within the Dragonpass Hybrid SDK ecosystem. The DPApp can send structured data that the host app can recognize and process; additionally, the host app can implement native-level operations based on received events and return response data back to the DPApp for enhanced functional interaction and business logic execution.

iOS

Set the delegate after SDK initialization, typically in AppDelegate:

Swift
import DPSDKKit

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
DPSDK.start(clientId: "your-client-id") { success in
guard success else { return }
// Configure the custom event delegate
DPSDK.shared.defaultCustomDelegate = self
}
return true
}

// MARK: - DPAppCustomDelegate Implementation
extension AppDelegate: DPAppCustomDelegate {
func dpAppHandleCustomEvent(
_ event: DPAppCustomEvent,
completion: @escaping (DPAppResponse) -> Void
) {
switch event.type {
case "getUserInfo":
handleGetUserInfo(event, completion: completion)
default:
completion(DPAppResponse(statusCode: 0, data: [:]))
}
}
}

private func handleGetUserInfo(
_ event: DPAppCustomEvent,
completion: @escaping (DPAppResponse) -> Void
) {
if !isLoggedIn() || currentUser == nil {
completion(DPAppResponse(statusCode: 0, data: [
"errCode": "xxxxx",
"msg": "xxxxx"
]))
return
}

let userInfo: [String: Any] = [
"id": "xxxxx",
"firstName": "xxxxx",
"lastName": "xxxxx"
]
completion(DPAppResponse(statusCode: 1, data: userInfo))
}

Android

Kotlin
val appId = "<app_id>"
val dpApp = DPSDK.getDPApp(appId)
dpApp?.setCustomEventListener { activity, data, jsCustomEventMsgEntity, callBackFunction ->
// Event Listening
val eventType = jsCustomEventMsgEntity.eventType
when (eventType) {
"getUserInfo" -> {
getUserInfo(activity, data, jsCustomEventMsgEntity, callBackFunction)
}
}
}

private fun getUserInfo(
activity: FragmentActivity,
data: String?,
jsCustomEventMsgEntity: JsCustomEventMsgEntity,
function: CallBackFunction?
): Boolean {
if (!LoginUtils.isLogin() || LoginUtils.getUserInfo() == null) {
function?.onFailure("errorCode", "errorMsg")
} else {
val hashMapOf = hashMapOf<String, Any?>()
hashMapOf.put("id", "xxxxx")
hashMapOf.put("firstName", "xxxxx")
hashMapOf.put("lastName", "xxxxx")
function?.onSuccess(hashMapOf)
}
return true
}

Custom Event Response Format

Successful Response Format

JSON
{
"statusCode": 1,
"errCode": null,
"msg": "success",
"data": {...}
}

Failure Response Format

JSON
{
"statusCode": 0,
"errCode": "APP_1001",
"msg": "xxxxx",
"data": null
}