[iOS/Swift]์นด์นด์คํก ๊ณต์ ํ๊ธฐ ๊ตฌํ 1.์ด๋ฏธ์ง URL ์ค์ + FileManager์ UIImage ์ ์ฅ
์ฐ๋ฆฌ ์๋น์ค roome๋ ๋ฐฉํ์ถ ํ๋กํ์ ์์ฑํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ฃผ๋ ๊ธฐ๋ฅ์ด๋ค. ๋๋ฌธ์ ์ด ํ๋กํ์ ๋ค๋ฅธ ์ฌ๋๋ค์๊ฒ ๊ณต์ ํ๋ ๊ฒ์ด ํ์์๋ค. ๋ค์ด๋ฒ, ์ธ์คํ ๋ฑ ์ฌ๋ฌ ๊ฐ์ง ๋งค์ฒด๋ฅผ ๊ณ ๋ฏผํ๋๋ฐ ํ๋กํ
mintraum.tistory.com
[iOS/Swift]์นด์นด์คํก ๊ณต์ ํ๊ธฐ ๊ตฌํ 2. ์ฌ์ฉ์ ์ ์ ํ ํ๋ฆฟ ์ฌ์ฉ
2024.07.05 - [iOS/ํธ๋ฌ๋ธ ์ํ ] - [iOS/Swift]์นด์นด์คํก ๊ณต์ ํ๊ธฐ ๊ตฌํ 1.์ด๋ฏธ์ง URL ์ค์ + FileManager์ UIImage ์ ์ฅ [iOS/Swift]์นด์นด์คํก ๊ณต์ ํ๊ธฐ ๊ตฌํ 1.์ด๋ฏธ์ง URL ์ค์ + FileManager์ UIImage ์ ์ฅ์ฐ๋ฆฌ ์๋น์ค roo
mintraum.tistory.com
์์ ๋๊ฐ์ ํฌ์คํธ์์๋ ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ผ ๋์ ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์๋ค. ์ด์ ๋ ์ ๋ง ์ด ๋ก์ง๋๋ก ๋ฐ์์ ์ด๋๋ก ๊ฐ ๊ฒ์ธ์ง๋ฅผ ๊ฒฐ์ ํด์ฃผ์ด์ผ ํ๋ค.
์ฑ์ด ๋ฏธ์ค์น๋ ๊ฒฝ์ฐ๋ ์ฐ๋ฆฌ๊ฐ ์ฝ๋๋ก ์์ฑํด์ค ๊ฒ์ ์๋ค. Kakao Developers > ๋ด ์ ํ๋ฆฌ์ผ์ด์ > ํ๋ซํผ ์ผ๋ก ๋ค์ด๊ฐ ๋ง์ผ ์ฃผ์๋ฅผ ์ ๋ ฅํ๋ฉด ๋๋ค. id๋ง ์ ๋ ฅํ๋ฉด url์ ์๋์ผ๋ก ๋ค์ด๊ฐ๋ค.
๊ทธ๋ ๋ค๋ฉด ์ด์ ์ฑ์ด ์ค์น๋์ด ์๋ ๊ฒฝ์ฐ๋ ์ด๋ป๊ฒ ๋ ๊น? ์ฑ์ ๋ผ์ดํ ์ฌ์ดํด ์ํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ค. ์นด์นด์คํก์ ๋ณด๊ณ ์์์ผ๋ ์ฑ์ Background ํน์ Suspended ๋ ๊ฐ์ง ๊ฒฝ์ฐ์ผ ๊ฒ์ด๋ค.
Background
- ์ฑ์ด ์ด๋ ธ๋ ์ ์ด ์๊ณ Foreground์๋ ์์ผ๋ ์คํ๋๊ณ ์๋ ์ฝ๋๊ฐ ์กด์ฌํ๋ ์ํ.
- ๊ณต์ ๋ฐ์ ๋ฒํผ์ ๋๋ฅด๋ฉด SceneDelegate์ openURLContexts๊ฐ ์คํ๋๋ค.
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
if (AuthApi.isKakaoTalkLoginUrl(url)) {
_ = AuthController.handleOpenUrl(url: url)
}
//url์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ค์ ๋์
๋๋ฆฌ๋ก ๋ณ๊ฒฝ
let params = self.queryParams(url: url)
let nickname = params["value"]
if params["type"] == "profile" {
//๊ณต์ ๋ฐ์ ์ ์ ์ ๋๋ค์์ผ๋ก SharingContainer ๊ฐ์ฒด ์์ฑ
let sharingContainer = SharingContainer(nickname: nickname ?? "๋๋ค์")
Task {
do {
//SharingContainer์ ํ๋กํ ์
๋ฐ์ดํธ
try await sharingContainer.updateSharingUserProfile(nickname: nickname ?? "๋๋ค์")
//์ฑ์ ๋ก๊ทธ์ธํ ์ ์ด ์๋ค๋ฉด, ์ ์ ์ ํ๋กํ๋ ์
๋ฐ์ดํธ -> ์ถํ์ ์ ์ ํ๋กํ ์ฌ๋ถ์ ๋ฐ๋ผ ๊ณต์ ๋ฐ์ ํ๋กํ ๋ฐ ๋ฒํผ ํ
์คํธ๊ฐ ๋ณ๊ฒฝ๋จ.
if KeyChain.read(key: .hasToken) == "true" {
try await UserContainer.shared.updateUserProfile()
}
//๊ณต์ ๋ฐ์ ํ๋กํ๋ก ํ์ด์ง ๋ง๋ค์ด์ ๋์ฐ๊ธฐ
let sharedViewController = SharingViewController(sharingContainer: sharingContainer)
changeRootViewController(sharedViewController, animated: true)
} catch {
print(error)
}
}
} else {
changeRootViewController(SplashView(), animated: false)
}
}
Suspended
- ๋ฉ๋ชจ๋ฆฌ์๋ง ์๊ณ ์ฝ๋๋ฅผ ์คํํ์ง ์๊ณ ์๋ ์ํ
- ๊ณต์ ๋ฐ์ ๋ฒํผ์ ๋๋ฅด๋ฉด ์ฒ์๋ถํฐ ์คํ๋๋ฏ๋ก SceneDelegate์ willConnectTo์์ openURLContexts๋ฅผ ์ ์ ํ๊ฒ ํธ์ถํด์ฃผ์ด์ผ ํ๋ค. ์๋๋ผ๋ฉด ์ฑ์ ์ข ๋ฃํ ์ํ์์ ๋ฒํผ์ ๋๋ ์ ๋ ๊น๋ง ํ๋ฉด๋ง ๋จ๊ณ ์ ๋๋ก ๋์ค์ง ์๋๋ค.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
DIManager.shared.registerAll()
//urlContexts๊ฐ ๋ค์ด์๋์ง ์๋์ง ํ์ธ. ๋ถ๊ธฐ ์ฒ๋ฆฌ.
if connectionOptions.urlContexts.isEmpty {
window?.rootViewController = SplashView()
} else {
self.scene(scene, openURLContexts: connectionOptions.urlContexts)
}
window?.makeKeyAndVisible()
}
openURL์ ํ๊ธ์ด ์๋ค๋ฉด
url๋ก ์ ๋ฌํ parameter์ ํ๊ธ์ด ์๋ค๋ฉด ์ด๋ป๊ฒ ์ค๊ฒ ๋ ๊น?
์ด๋ ๊ฒ ์นด์นด์ค ์๋ฒ์์ ์์์ ์ธ์ฝ๋ฉ ํ ๋ณด๋ด์ค๋ค. ๊ทธ๋ ๋ค๋ฉด ์ฐ๋ฆฌ๋ ์ด๊ฑธ ๋์ฝ๋ฉํด์ ์ฌ์ฉํด์ผ ํ๋ค. (์๋๋ก์ด๋๋ ์๋์ผ๋ก ๋์ฝ๋ฉํ๋๋ฐ) ์ฒ์์๋ ๊ทธ๋ฅ ์ฌ์ฉํ๋ค๊ฐ 2000 ์ฌ์ฉ์๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค ๋ผ๋ ์ํ ์ฝ๋๋ฅผ ๋ฐ์๋ค.
private func queryParams(url: URL) -> Dictionary<String, String> {
var parameters = Dictionary<String, String>()
//query๋ &๋ก ๊ตฌ๋ถ๋์ด ์์ผ๋ฏ๋ก ๋๋ ์ค๋ค.
if let queryComponents = url.query?.components(separatedBy: "&") {
for queryComponent in queryComponents {
//key1=value1์ ํ์์ด๋ฏ๋ก =๋ก๋ ๋๋ ์ค๋ค.
let paramComponents = queryComponent.components(separatedBy: "=")
var object: String? = nil
if paramComponents.count > 1 {
//String ํํ๋ก ๋ง๋ค์ด ๋์
๋๋ฆฌ์ key์ value๋ฅผ ์ ์ฅํด์ฃผ๋๋ฐ removingPercentEncoding์ ํตํด ๋์ฝ๋ฉํด์ค๋ค.
object = paramComponents[1].removingPercentEncoding
}
let key = paramComponents[0]
parameters[key] = object
}
}
return parameters
}
๊ทธ๋์ removingPercentEncoding๋ฅผ ์ฌ์ฉํ์ฌ ๋์ฝ๋ฉํ ๊ฐ์ ํ๋ผ๋ฏธํฐ ๋์ ๋๋ฆฌ์ ์ ์ฅํ๋๋ก ๋ณ๊ฒฝํ์๋๋, ์ ๋๋ก ๋ ๊ฐ์ด ๋ค์ด์๋ค!
https://developer.apple.com/documentation/foundation/nsstring/1409569-removingpercentencoding
removingPercentEncoding | Apple Developer Documentation
Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.
developer.apple.com
https://apps.apple.com/kr/app/roome/id6503616766
roome
๋ฐฉ ํ์ถ ํ๋กํ ์ ์ ์๋น์ค ๋ฃจ๋ฏธ๋ฅผ ํตํด ๋์ ์ฑํฅ์ ๋น ๋ฅด๊ฒ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด ๋ณด์ธ์. ๋ฃจ๋ฏธ๋ ๋ฐฉ ํ์ถ์ ์ข์ํ๋ ์ฌ๋๋ค์ด ๋์ ์ทจํฅ์ ๋ด์ ํ๋กํ์ ์ด๋ฏธ์ง 1์ฅ์ผ๋ก ๋ง๋ค ์ ์๋ ์๋น์ค
apps.apple.com