Nhảy tới nội dung

Tích hợp iOS

Yêu cầu iOS >= 9.0

Bước 1: Cấu hình file Info.plist

  • CFBundleURLSchemes: Giá trị scheme là clientId được gửi kèm trong email khi đơn vị tích hợp đăng ký với VNPT.
  • LSApplicationQueriesSchemes: Thêm scheme vnptsmartca(dùng cho môi trường Production) và vnptsmartcademo(dùng cho môi trường Demo).
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>partnerSchemeId1</string>
<string>partnerSchemeId2</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>vnptsmartca</string>
<string>vnptsmartcademo</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Bước 2: Import SDK trong file AppDelegate.swift

import VNPTSmartCAiOSSDK

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
VNPTSmartCATransaction.receiveBackLink(url: url, sourceApp: sourceApplication!)
return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
VNPTSmartCATransaction.receiveBackLink(url: url, sourceApp: "")
return true
}
Lưu ý

Đối với phiên bản iOS >= 13.0, cần import SDK trong file SceneDelegate.swift.

import VNPTSmartCAiOSSDK

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
VNPTSmartCATransaction.receiveBackLink(url: URLContexts.first!.url, sourceApp: "")
}

Bước 3: Cập nhật giao diện và truyền tham số để mở ứng dụng VNPTSmartCA

  • Trước khi mở ứng dụng VNPTSmartCA, cần chỉ định môi trường với hàm: VNPTSmartCATransaction.setEnvironment là DEMO hoặc PRODUCTION.
import VNPTSmartCAiOSSDK // You need import before using
override func viewDidLoad() {
//STEP 1: addObserver Notification
NotificationCenter.default.removeObserver(self,name: NSNotification.Name(rawValue: "<VNPTSmartCA>NotificationCenterReceived"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.NotificationCenterTokenReceived), name:NSNotification.Name(rawValue: "<VNPTSmartCA>NotificationCenterReceived"), object: nil)
//STEP 2: INIT TRANSACTION BASIC INFO AS CLIENT ID AND TRAN ID
let tranInfo: NSMutableDictionary = NSMutableDictionary()
tranInfo["clientId"] = "partnerSchemeId" // You need pass your client ID replace for 'partnerSchemeId'.
tranInfo["tranId"] = "transactionId" // You need pass your transaction ID replace for 'transactionId'.
VNPTSmartCATransaction.setEnvironment(_environment: VNPTSmartCATransaction.ENVIRONMENT.DEMO)
VNPTSmartCATransaction.createTransactionInformation(info: tranInfo)
//STEP 3: INIT LAYOUT - ADD BUTTON OPEN VNPTSMARTCA
let buttonOpen = UIButton()
buttonOpen.frame = CGRect(x: 20, y: 200, width: 260, height: 40)
buttonOpen.setTitle("Open VNPT SmartCA", for: .normal)
buttonOpen.setTitleColor(UIColor.white, for: .normal)
buttonOpen.titleLabel!.font = UIFont.systemFont(ofSize: 15)
buttonOpen.backgroundColor = UIColor.blue
buttonOpen.addTarget(self, action: #selector(self.openApp), for: .touchUpInside)
self.view.addSubview(buttonOpen)
}

@objc func openApp() {
VNPTSmartCATransaction.handleOpen()
}
// You can listen return event from VNPTSmartCA app in here
@objc func NotificationCenterTokenReceived(notify: NSNotification) {
let response: NSMutableDictionary = notify.object! as! NSMutableDictionary

let _statusStr = "\(response["status"] as! String)"
let _message = response["message"]

if (_statusStr == "0") {
print("Status code: \(_statusStr)")
print("Message:", _message as! String)
} else {

}
}