-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
91 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-3.45 KB
(97%)
...it-a-pet-client.xcworkspace/xcuserdata/maclove.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
64 changes: 29 additions & 35 deletions
64
fit-a-pet-client/fit-a-pet-client/API/AlamofireManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,89 @@ | ||
|
||
|
||
import Foundation | ||
import Alamofire | ||
import os.log | ||
|
||
class AlamofireManager{ | ||
class AlamofireManager { | ||
|
||
//싱글턴 적용 | ||
static let shared = AlamofireManager()//자기 자신의 인스턴스를 가져옴 | ||
// 싱글턴 적용 | ||
static let shared = AlamofireManager() // 자기 자신의 인스턴스를 가져옴 | ||
|
||
//로거 설정 | ||
//자료형이 EventMonitor | ||
let monitors = [MyLogger(), ApiStatusLogger()] as [EventMonitor]//여러개 추가 가능 | ||
// 로거 설정 | ||
// 자료형이 EventMonitor | ||
let monitors = [MyLogger(), ApiStatusLogger()] as [EventMonitor] // 여러 개 추가 가능 | ||
|
||
let session = Session.default | ||
|
||
//MARK: - Alamofire methods | ||
func sendSms(_ phone: Int, completion: @escaping(Result<Data?, Error>) -> Void) { | ||
// MARK: - Alamofire methods | ||
func sendSms(_ phone: String, completion: @escaping(Result<Data?, Error>) -> Void) { | ||
|
||
print("MyAlamofireManager - sendSms() called userInput : \(phone) ") | ||
os_log("MyAlamofireManager - sendSms() called userInput : %@", log: .default, type: .info, phone) | ||
|
||
self | ||
.session //세션 설정 | ||
.session // 세션 설정 | ||
.request(MySearchRouter.sendSms(to: phone)) | ||
// .validate(statusCode: 200..<401)//200에서 401이전까지만 | ||
.response { response in | ||
switch response.result { | ||
case .success(let data): | ||
completion(.success(data)) | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
func checkSms(_ phone: Int, _ code: Int, completion: @escaping(Result<Data?, Error>) -> Void) { | ||
|
||
print("MyAlamofireManager - checkSms() called userInput : \(phone) ,, \(code) ") | ||
func checkSms(_ phone: String, _ code: Int, completion: @escaping(Result<Data?, Error>) -> Void) { | ||
os_log("MyAlamofireManager - checkSms() called userInput : %@ ,, %d", log: .default, type: .info, phone, code) | ||
|
||
self | ||
.session | ||
.request(MySearchRouter.checkSms(to: phone, code: code)) | ||
self.session.request(MySearchRouter.checkSms(to: phone, code: code)) | ||
.response { response in | ||
switch response.result { | ||
case .success(let data): | ||
//액세스 토큰 저장 | ||
if let responseHeaders = response.response?.allHeaderFields as? [String: String], | ||
let accessToken = responseHeaders["accessToken"] { | ||
UserDefaults.standard.set(accessToken, forKey: "accessToken") | ||
print("AccessToken: \(accessToken)") | ||
KeychainHelper.saveAccessToken(accessToken: accessToken) | ||
} | ||
|
||
completion(.success(data)) | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func login(_ uid: String, _ password: String, completion: @escaping(Result<Data?, Error>) -> Void){ | ||
print("MyAlamofireManager - login() called userInput : \(uid) ,, \(password) ") | ||
|
||
os_log("MyAlamofireManager - login() called userInput : %@ ,, %@", log: .default, type: .info, uid, password) | ||
|
||
self | ||
.session | ||
.request(MySearchRouter.login(uid: uid, password: password)) | ||
.response{ response in | ||
.response { response in | ||
switch response.result{ | ||
case .success(let data): | ||
if let responseHeaders = response.response?.allHeaderFields as? [String: String], | ||
let accessToken = responseHeaders["accessToken"] { | ||
KeychainHelper.saveAccessToken(accessToken: accessToken) | ||
os_log("login token: %@", log: .default, type: .info, accessToken) | ||
} | ||
completion(.success(data)) | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
func regist(_ uid: String, _ name: String, _ password: String, _ email: String, _ profileImg: String, completion: @escaping(Result<Data?, Error>) -> Void){ | ||
print("MyAlamofireManager - regist() called userInput : \(uid) ,, \(password) ,, \(name) ,, \(email) ,, \(profileImg) ") | ||
os_log("MyAlamofireManager - regist() called userInput : %@ ,, %@ ,, %@ ,, %@ ,, %@", log: .default, type: .info, uid, password, name, email, profileImg) | ||
|
||
self | ||
.session | ||
.request(MySearchRouter.regist(uid: uid, name: name, password: password, email: email, profileImg: profileImg)) | ||
.response{ response in | ||
.response { response in | ||
switch response.result{ | ||
case .success(let data): | ||
completion(.success(data)) | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
fit-a-pet-client/fit-a-pet-client/Utils/KeyChainHelper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Foundation | ||
import Security | ||
|
||
class KeychainHelper { | ||
static func saveAccessToken(accessToken: String) { | ||
let keychainQuery: [CFString: Any] = [ | ||
kSecClass: kSecClassGenericPassword, | ||
kSecAttrAccount: "accessToken", | ||
kSecValueData: accessToken.data(using: .utf8)!, | ||
] | ||
|
||
let status = SecItemAdd(keychainQuery as CFDictionary, nil) | ||
if status == errSecDuplicateItem { | ||
SecItemUpdate(keychainQuery as CFDictionary, [kSecValueData: accessToken.data(using: .utf8)!] as CFDictionary) | ||
} else if status != noErr { | ||
print("Failed to save AccessToken to Keychain") | ||
} | ||
} | ||
|
||
static func loadAccessToken() -> String? { | ||
let query: [CFString: Any] = [ | ||
kSecClass: kSecClassGenericPassword, | ||
kSecAttrAccount: "accessToken", | ||
kSecReturnData: kCFBooleanTrue, | ||
] | ||
|
||
var item: CFTypeRef? | ||
let status = SecItemCopyMatching(query as CFDictionary, &item) | ||
|
||
if status == noErr, let data = item as? Data, let token = String(data: data, encoding: .utf8) { | ||
return token | ||
} else { | ||
return nil | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters