Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

딥링크 - 02 #18

Open
Brandnew-one opened this issue Jan 2, 2023 · 0 comments
Open

딥링크 - 02 #18

Brandnew-one opened this issue Jan 2, 2023 · 0 comments
Assignees
Labels
bug Something isn't working SwiftUI

Comments

@Brandnew-one
Copy link
Owner

Brandnew-one commented Jan 2, 2023

왜 .onOpenURL이 동작하지 않을까?

프로젝트 레포

토이 프로젝트를 작성하고 회사 코드에 딥링크를 적용하는 과정 중에 겪은 버그를 정리하고자 한다.
(사실 거의 한달 정도 지난것 같은데 너무 정신없이 바빴다.)

결론부터 얘기하자면 SwiftUI 기반이 아닌 UIKit 기반의 RootView를 바꾸는 동작이 문제였다.

let window = UIApplication.shared
              .connectedScenes
              .flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
              .first { $0.isKeyWindow }

guard let window = window else { return }
let tabViewModel = TestTabViewModel()
window.rootViewController = UIHostingController(
  rootView: TestTabView().environmentObject(tabViewModel)
)

window.makeKeyAndVisible()
UIView.transition(
  with: window,
  duration: 0.5,
  options: .transitionCrossDissolve,
  animations: nil,
  completion: nil
)

기존에는 특정 프로세스가 끝나면 위와 같은 방식으로 RootView를 바꿔주는 방식을 이용해서 화면 전환을 하고 있었다.

정확하게 정리하면

  • Lottie를 이용한 스플래쉬 뷰를 실행
  • 해당 뷰에서 처음 앱이 실행될 때 필요한 정보를 서버로 부터 받아옴
  • 토큰이 유효한지를 확인

위의 3가지 로직을 타면서 성공 유무에 따라 시작 하는 화면을 스플래쉬 뷰에서 UIKit 기반의 RootView를 바꿔주는 방법을 사용하고 있었다.

하지만 위와 같은 방식으로는 우리가 토이 프로젝트에서 확인했던 .openURL 이 동작하지 않는다 ㅠㅠ
사실 정확한 이유와 함께 정리하고 싶었는데 밀린 일이 산더미라 우선 해결방법이라도 정리해놓고자 한다.

struct AppRootView: View {
  @StateObject
  var viewModel = AppRootViewModel()

  var body: some View {
    Group {
      switch viewModel.nextView {
      case .splash:
        SplashView()
      case .main:
        TodoTabView()
      }
    }
  }
}
final class AppRootViewModel: ObservableObject {
  enum NextView {
    case splash
    case main
//    case login
  }

  @Published var nextView: NextView = .splash

  init() {
    fetchInitInfo()
  }
}

extension AppRootViewModel {
// BL에 따라 처리
  func fetchInitInfo() {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
      guard let self = self else { return }
      self.nextView = .main
    }
  }
}

토이 프로젝트라 Lottie 애니메이션이나 뷰모델의 비즈니스 로직은 전부 생략했다.

기존의 RootViewCon을 바꾸는 로직을 걷어내고 AppRootView를 만들어서 비즈니스 로직의 결과에 따라 nextView를 바꿔주는 방식을 사용했다.

@Brandnew-one Brandnew-one added bug Something isn't working SwiftUI labels Jan 2, 2023
@Brandnew-one Brandnew-one self-assigned this Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working SwiftUI
Projects
None yet
Development

No branches or pull requests

1 participant