MyIdController.swift 1.65 KB
Newer Older
Javohir Savriy's avatar
Javohir Savriy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
//  MyIdController.swift
//  Runner
//
//  Created by Azamat on 07/08/22.
//

import UIKit
import MyIdSDK

class MyIdController: UIViewController {
    
    var onResult: (String) -> Void
    var onCancel: () -> Void
    
    init(onResult: @escaping (String) -> Void, onCancel: @escaping () -> Void) {
        self.onResult = onResult
        self.onCancel = onCancel
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        MyIdSdk.start(withConfigureOptions: { options in
            options?.clientId = "YOUR_CLIENT_ID"
            options?.locale = .UZ
        }, withDelegate: self)
    }
    
    func toJson(json: [String: Any]) -> String {
        do {
            let data = try JSONSerialization.data(withJSONObject: json)
            return String(data: data, encoding: String.Encoding.utf8) ?? ""
        } catch let JSONError {
            print(JSONError)
        }
        
        return ""
    }
}

extension MyIdController: MyIdSdkDelegate {
    
    func myidOnSuccess(result: MyIdResult) {
        let jsonObject: [String: Any] = [
            "code": result.code ?? "",
        ]
        
        onResult(toJson(json: jsonObject))
    }
    
    func myidOnError(exception: MyIdException) {
        let jsonObject: [String: Any] = [
            "error": [
                "code": exception.code,
                "message": exception.message,
            ],
        ]
        
        onResult(toJson(json: jsonObject))
    }
    
    func myidOnUserExited() {
        onCancel()
    }
}