Skip to content

Commit c9ff02e

Browse files
author
Jaesung
authored
Merge pull request #11 from sendbird/feature/jaesung/additional-signing-in
Add manual signing in feature
2 parents bef6d25 + c38e431 commit c9ff02e

File tree

6 files changed

+357
-38
lines changed

6 files changed

+357
-38
lines changed

QuickStart.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
A951A7AF238D154700410E40 /* VoiceCallViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A951A7AE238D154700410E40 /* VoiceCallViewController.swift */; };
2020
A95A7C9E2420614A00EB0D56 /* SignInWithQRViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A95A7C9D2420614A00EB0D56 /* SignInWithQRViewController.swift */; };
2121
A95A7CA0242063C700EB0D56 /* QRCodeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A95A7C9F242063C700EB0D56 /* QRCodeViewController.swift */; };
22+
A95E91C5242870AF0035274E /* SignInManuallyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A95E91C4242870AF0035274E /* SignInManuallyViewController.swift */; };
23+
A95E91C7242887730035274E /* SignInDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A95E91C6242887730035274E /* SignInDelegate.swift */; };
2224
A96839F02418ED0100317004 /* VideoCallViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A96839EF2418ED0100317004 /* VideoCallViewController.swift */; };
2325
A968F6A42415CFF800E6EFD5 /* UIView+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A968F6A32415CFF800E6EFD5 /* UIView+Extension.swift */; };
2426
A99449592387E92400A4FFBB /* SettingsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A99449582387E92400A4FFBB /* SettingsTableViewController.swift */; };
@@ -56,6 +58,8 @@
5658
A951A7B7238D23C000410E40 /* QuickStart.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = QuickStart.entitlements; sourceTree = "<group>"; };
5759
A95A7C9D2420614A00EB0D56 /* SignInWithQRViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInWithQRViewController.swift; sourceTree = "<group>"; };
5860
A95A7C9F242063C700EB0D56 /* QRCodeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeViewController.swift; sourceTree = "<group>"; };
61+
A95E91C4242870AF0035274E /* SignInManuallyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInManuallyViewController.swift; sourceTree = "<group>"; };
62+
A95E91C6242887730035274E /* SignInDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInDelegate.swift; sourceTree = "<group>"; };
5963
A96839EF2418ED0100317004 /* VideoCallViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoCallViewController.swift; sourceTree = "<group>"; };
6064
A968F6A32415CFF800E6EFD5 /* UIView+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Extension.swift"; sourceTree = "<group>"; };
6165
A99449582387E92400A4FFBB /* SettingsTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsTableViewController.swift; sourceTree = "<group>"; };
@@ -210,6 +214,8 @@
210214
A9A72CB82387BAE6007C0878 /* SignInViewController.swift */,
211215
A95A7C9F242063C700EB0D56 /* QRCodeViewController.swift */,
212216
A95A7C9D2420614A00EB0D56 /* SignInWithQRViewController.swift */,
217+
A95E91C4242870AF0035274E /* SignInManuallyViewController.swift */,
218+
A95E91C6242887730035274E /* SignInDelegate.swift */,
213219
);
214220
path = SignIn;
215221
sourceTree = "<group>";
@@ -351,6 +357,7 @@
351357
files = (
352358
A968F6A42415CFF800E6EFD5 /* UIView+Extension.swift in Sources */,
353359
A99CC94A23BA142F00F9116A /* UIImageView+QuickStart.swift in Sources */,
360+
A95E91C5242870AF0035274E /* SignInManuallyViewController.swift in Sources */,
354361
A95A7CA0242063C700EB0D56 /* QRCodeViewController.swift in Sources */,
355362
A9A72CB92387BAE6007C0878 /* SignInViewController.swift in Sources */,
356363
A9409C462390FA8100410458 /* UserDefaults+QuickStart.swift in Sources */,
@@ -366,6 +373,7 @@
366373
A951A7A7238CC4DF00410E40 /* DialViewController.swift in Sources */,
367374
A96839F02418ED0100317004 /* VideoCallViewController.swift in Sources */,
368375
A9AC8755241A202C004AB93B /* AppInfoTableViewController.swift in Sources */,
376+
A95E91C7242887730035274E /* SignInDelegate.swift in Sources */,
369377
A943CAF723C5AD06009F6E49 /* AppDelegate+CXProviderDelegate.swift in Sources */,
370378
A91199BC2421257300208E21 /* DirectCallDataSource.swift in Sources */,
371379
A943CAF323C5AAB8009F6E49 /* Bundle+QuickStart.swift in Sources */,

QuickStart/SignIn/QRCodeViewController.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import UIKit
1010
import AVFoundation
1111
import SendBirdCalls
1212

13-
protocol QRCodeScanDelegate: class {
14-
func didScanQRCode(appId: String, userId: String, accessToken: String?)
15-
}
16-
1713
class QRCodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
1814
typealias SendBirdQRInfo = [String: String?]
1915

@@ -34,7 +30,7 @@ class QRCodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDele
3430
}
3531
}
3632

37-
weak var delegate: QRCodeScanDelegate?
33+
weak var delegate: SignInDelegate?
3834

3935
override func viewDidLoad() {
4036
super.viewDidLoad()
@@ -122,6 +118,6 @@ class QRCodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDele
122118
guard let appId = qrInfo["app_id"] as? String else { return }
123119
guard let userId = qrInfo["user_id"] as? String else { return }
124120
let accessToken = qrInfo["access_token"] as? String
125-
self.delegate?.didScanQRCode(appId: appId, userId: userId, accessToken: accessToken)
121+
self.delegate?.didSignIn(appId: appId, userId: userId, accessToken: accessToken)
126122
}
127123
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// SignInDelegate.swift
3+
// QuickStart
4+
//
5+
// Created by Jaesung Lee on 2020/03/23.
6+
// Copyright © 2020 SendBird Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
protocol SignInDelegate: class {
12+
func didSignIn(appId: String, userId: String, accessToken: String?)
13+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//
2+
// SignInManuallyViewController.swift
3+
// QuickStart
4+
//
5+
// Created by Jaesung Lee on 2020/03/23.
6+
// Copyright © 2020 SendBird Inc. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import SendBirdCalls
11+
12+
class SignInManuallyViewController: UIViewController {
13+
@IBOutlet weak var appIdTextField: UITextField! {
14+
didSet {
15+
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: self.appIdTextField.frame.height))
16+
self.appIdTextField.leftView = paddingView
17+
self.appIdTextField.leftViewMode = UITextField.ViewMode.always
18+
self.appIdTextField.delegate = self
19+
}
20+
}
21+
22+
@IBOutlet weak var userIdTextField: UITextField! {
23+
didSet {
24+
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: self.userIdTextField.frame.height))
25+
self.userIdTextField.leftView = paddingView
26+
self.userIdTextField.leftViewMode = UITextField.ViewMode.always
27+
self.userIdTextField.delegate = self
28+
}
29+
}
30+
31+
@IBOutlet weak var accessTokenTextField: UITextField! {
32+
didSet {
33+
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: self.accessTokenTextField.frame.height))
34+
self.accessTokenTextField.leftView = paddingView
35+
self.accessTokenTextField.leftViewMode = UITextField.ViewMode.always
36+
self.accessTokenTextField.delegate = self
37+
}
38+
}
39+
40+
@IBOutlet weak var versionLabel: UILabel! {
41+
didSet {
42+
let sampleVersion = Bundle.main.version
43+
self.versionLabel.text = "QuickStart \(sampleVersion) Calls SDK \(SendBirdCall.sdkVersion)"
44+
}
45+
}
46+
47+
weak var delegate: SignInDelegate?
48+
49+
@IBAction func didTapCancel() {
50+
self.dismiss(animated: true, completion: nil)
51+
}
52+
53+
// MARK: - ManualSignInDelegate
54+
@IBAction func didTapSignIn() {
55+
guard let appId = self.appIdTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), !appId.isEmpty else {
56+
self.presentErrorAlert(message: "Please enter valid app ID")
57+
return
58+
}
59+
guard let userId = self.userIdTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), !userId.isEmpty else {
60+
self.presentErrorAlert(message: "Please enter valid user ID")
61+
return
62+
}
63+
let accessToken = self.accessTokenTextField.text
64+
65+
self.delegate?.didSignIn(appId: appId, userId: userId, accessToken: accessToken)
66+
self.dismiss(animated: true, completion: nil)
67+
}
68+
}
69+
70+
// MARK: - UITextFieldDelegate
71+
extension SignInManuallyViewController: UITextFieldDelegate {
72+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
73+
self.view.endEditing(true)
74+
}
75+
76+
func textFieldDidEndEditing(_ textField: UITextField) {
77+
let animator = UIViewPropertyAnimator(duration: 0.3, curve: .easeOut) {
78+
textField.layer.borderWidth = 0.0
79+
self.view.layoutIfNeeded()
80+
}
81+
animator.startAnimation()
82+
textField.resignFirstResponder()
83+
}
84+
85+
func textFieldDidBeginEditing(_ textField: UITextField) {
86+
let animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeOut) {
87+
textField.layer.borderWidth = 1.0
88+
self.view.layoutIfNeeded()
89+
}
90+
animator.startAnimation()
91+
}
92+
}

QuickStart/SignIn/SignInWithQRViewController.swift

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ class SignInWithQRViewController: UIViewController {
1313
// Scan QR Code
1414
@IBOutlet weak var scanButton: UIButton!
1515

16+
// Sign In Manually
17+
@IBOutlet weak var signInManuallyButton: UIButton!
18+
19+
// Other components
20+
@IBOutlet weak var lineView: UIView!
21+
@IBOutlet weak var orLabel: UILabel!
22+
1623
// Footnote
1724
@IBOutlet weak var versionLabel: UILabel! {
1825
didSet {
@@ -32,6 +39,20 @@ class SignInWithQRViewController: UIViewController {
3239
}
3340
}
3441

42+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
43+
switch segue.identifier {
44+
case "scanQR":
45+
guard let qrCodeVC = segue.destination.children.first as? QRCodeViewController else { return }
46+
qrCodeVC.delegate = self
47+
if #available(iOS 13.0, *) { qrCodeVC.isModalInPresentation = true }
48+
case "manual":
49+
guard let signInVC = segue.destination.children.first as? SignInManuallyViewController else { return }
50+
signInVC.delegate = self
51+
if #available(iOS 13.0, *) { signInVC.isModalInPresentation = true }
52+
default: return
53+
}
54+
}
55+
3556
func startLoading() {
3657
self.activityIndicator.center = self.view.center
3758
self.activityIndicator.hidesWhenStopped = true
@@ -48,13 +69,24 @@ class SignInWithQRViewController: UIViewController {
4869
}
4970

5071
func resetButtonUI() {
72+
let animator = UIViewPropertyAnimator(duration: 0.3, curve: .easeOut) {
73+
self.lineView.isHidden = false
74+
self.orLabel.isHidden = false
75+
self.signInManuallyButton.isHidden = false
76+
}
77+
animator.startAnimation()
78+
5179
self.scanButton.setTitleColor(UIColor(red: 1, green: 1, blue: 1, alpha: 0.88), for: .normal)
5280
self.scanButton.backgroundColor = UIColor(red: 123 / 255, green: 83 / 255, blue: 239 / 255, alpha: 1.0)
53-
self.scanButton.setTitle("Scan QR Code", for: .normal)
81+
self.scanButton.setTitle("Sign in with QR code", for: .normal)
5482
self.scanButton.isEnabled = true
5583
}
5684

5785
func updateButtonUI() {
86+
self.lineView.isHidden = true
87+
self.orLabel.isHidden = true
88+
self.signInManuallyButton.isHidden = true
89+
5890
self.scanButton.backgroundColor = UIColor(red: 240 / 255, green: 240 / 255, blue: 240 / 255, alpha: 1.0)
5991
self.scanButton.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 0.12), for: .normal)
6092
self.scanButton.setTitle("Signing In...", for: .normal)
@@ -63,19 +95,17 @@ class SignInWithQRViewController: UIViewController {
6395
}
6496

6597
// MARK: - QR Code
66-
extension SignInWithQRViewController: QRCodeScanDelegate {
67-
68-
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
69-
guard segue.identifier == "scanQR", let QRCodeVC = segue.destination.children.first as? QRCodeViewController else { return }
70-
QRCodeVC.delegate = self
71-
}
72-
98+
extension SignInWithQRViewController: SignInDelegate {
7399
@IBAction func didTapScanQRCode() {
74100
performSegue(withIdentifier: "scanQR", sender: nil)
75101
}
76102

103+
@IBAction func didTapSignInManually() {
104+
performSegue(withIdentifier: "manual", sender: nil)
105+
}
106+
77107
// Delegate method
78-
func didScanQRCode(appId: String, userId: String, accessToken: String?) {
108+
func didSignIn(appId: String, userId: String, accessToken: String?) {
79109
SendBirdCall.configure(appId: appId)
80110

81111
UserDefaults.standard.appId = appId
@@ -107,6 +137,7 @@ extension SignInWithQRViewController {
107137
let errorDescription = String(error?.localizedDescription ?? "")
108138
self.presentErrorAlert(message: "Failed to authenticate\n\(errorDescription)")
109139
}
140+
UserDefaults.standard.clear()
110141
return
111142
}
112143
UserDefaults.standard.autoLogin = true

0 commit comments

Comments
 (0)