-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoginViewController.swift
100 lines (92 loc) · 4.25 KB
/
LoginViewController.swift
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
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
//
// LoginViewController.swift
// BMX API Client
//
// Created by Taras Markevych on 11/13/18.
// Copyright © 2018 Taras Markevych. All rights reserved.
//
import UIKit
import BMXCore
import SVProgressHUD
class LoginViewController: UITableViewController {
@IBOutlet weak var singInButton: UIButton!
@IBOutlet weak var environmentSegmentControll: UISegmentedControl!
private var environmentType: BMXBackendEnvironment = .development
private var authProvider: BMXAuthProvider!
@IBAction func environmentAction(_ sender: Any) {
switch environmentSegmentControll.selectedSegmentIndex {
case 0:
UserDefaults.standard.set("development", forKey: "environment")
environmentType = .development
case 1:
UserDefaults.standard.set("sandbox", forKey: "environment")
environmentType = .sandbox
case 2:
UserDefaults.standard.set("production", forKey: "environment")
environmentType = .production
default:
fatalError("No environment")
}
authProvider = getBMXAuthProvider(for: environmentType)
}
@IBAction func singInAction(_ sender: Any) {
SVProgressHUD.show()
let env = BMXEnvironment(backendEnvironment: environmentType)
BMXCoreKit.shared.configure(withEnvironment: env)
BMXCoreKit.shared.authorize(withAuthProvider: authProvider, callbackURL: URL(string: "demoapp:https://test")!, viewController: self) { result in
switch result {
case .success:
let stb = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = stb.instantiateViewController(withIdentifier: "MainTabController")
mainViewController.modalPresentationStyle = .overFullScreen
SVProgressHUD.dismiss()
self.present(mainViewController, animated: true, completion: {
guard let pushToken = CallsService.shared.pushkitToken else { return }
let token = pushToken.map { String(format: "%02.2hhx", $0) }.joined()
BMXCoreKit.shared.registerDevice(with: .voip(token: token), apnsSandbox: false) { result in
switch result {
case .success:
print("Success")
case .failure(let error):
print(error.localizedDescription)
}
}
})
case .failure(let error):
print(error)
SVProgressHUD.showError(withStatus: error.localizedDescription)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.set("development", forKey: "environment")
BMXCoreKit.shared.delegate = self
authProvider = getBMXAuthProvider(for: environmentType)
SVProgressHUD.setDefaultStyle(.light)
}
private func getBMXAuthProvider(for environment: BMXBackendEnvironment) -> BMXAuthProvider {
var secret = ""
var clientId = ""
switch environment {
case .development:
secret = Bundle.main.object(forInfoDictionaryKey: "butterflymxSecret") as? String ?? "N/a"
clientId = Bundle.main.object(forInfoDictionaryKey: "butterflymxClientId") as? String ?? "N/a"
case .sandbox:
secret = Bundle.main.object(forInfoDictionaryKey: "butterflymxSecretSandbox") as? String ?? "N/a"
clientId = Bundle.main.object(forInfoDictionaryKey: "butterflymxClientIdSandbox") as? String ?? "N/a"
case .production:
secret = Bundle.main.object(forInfoDictionaryKey: "butterflymxSecretProd") as? String ?? "N/a"
clientId = Bundle.main.object(forInfoDictionaryKey: "butterflymxClientIdProd") as? String ?? "N/a"
}
return BMXAuthProvider(secret: secret, clientID: clientId)
}
}
extension LoginViewController: BMXCoreDelegate {
func didUpdate(accessToken: String, refreshToken: String) {
print(accessToken, refreshToken)
}
func logging(_ data: String) {
print("BMXSDK Log: \(data)")
}
}