Language/iOS,AOS
URI 스킴 적용 (URL Scheme)
건담아빠
2023. 3. 6. 23:29
URI 스킴 (URI Scheme)란?
사용법
오픈하고자 하는 앱이 myapp라면 myaapp:// 라는 스킴값으로 이용할수 있습니다.
사용 목적?
iOS에서 유니버셜 링크 (Univeral Link)를 적용 하였는데 대표적인 채팅사이트인 카카오에서 안된다.. OTL..
젝12345678910시간 없으니 고민하지 말고 일단 ㄱㄱ싱 해보도록 합니다!!유니버셜 링크는 이전 포스팅을 참고하도록 하자(https://dchkang83.tistory.com/171)
myapp Scheme 등록
SceneDelegate 설정
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
...
// MARK: 카카오, 네이버, 페이스북 로그인
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if URLContexts.first?.url.scheme == "neoflat",
let url = URLContexts.first?.url {
AppInfo.shared.uriSchemeURL = url.absoluteString.replacingOccurrences(of: "neoflat://", with: "https://")
NotificationCenter.default.post(name: Notification.Name("didReceiveUriSchemeURL"), object: nil)
}
else if let url = URLContexts.first?.url {
if (AuthApi.isKakaoTalkLoginUrl(url)) {
// MARK: 카카오 로그인
_ = AuthController.handleOpenUrl(url: url)
} else {
...
}
}
}
...
}
URI 스킴 링크 핸들링 하기
유니버셜 링크랑 동일한 방식으로 사용해 보았다.
class MainViewController: BaseViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
// Uri Scheme (앱 실행중 : active, inactive)
NotificationCenter.default.addObserver(forName: Notification.Name("didReceiveUriSchemeURL"), object: nil, queue: nil) { notification in
let uriSchemeURL:String? = AppInfo.shared.uriSchemeURL
AppInfo.shared.uriSchemeURL = nil
if(uriSchemeURL != nil) {
self.executeJavasScript(wKWeb: self.wKMainWeb, callback: "Common.Native.setDeepLink('\(uriSchemeURL!)')")
}
}
}
...
}
...
// MARK: - iOS Bridge
// 웹 액션 정의 : WebAction을 구분하는데 사용되는 타입
enum WebAction: String {
...
case getDeepLink
}
extension MainViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard message.name == "IosBridge1",
let messages = message.body as? [String: Any],
let action = messages["action"] as? String else { return }
let webAction = WebAction(rawValue: action)
switch webAction {
...
case .getDeepLink:
// Universal Link (앱 종료 : background)
guard let params: [String: String] = messages["params"] as? [String: String],
let callback: String = params["callback"] else {
return
}
let universalUrl:String? = AppInfo.shared.universalURL
AppInfo.shared.universalURL = nil
if(universalUrl != nil) {
executeJavasScript(wKWeb: self.wKMainWeb, callback: "\(callback)('\(universalUrl!)')")
}
// Uri Scheme (앱 종료 : background)
let uriSchemeURL:String? = AppInfo.shared.uriSchemeURL
AppInfo.shared.uriSchemeURL = nil
if(uriSchemeURL != nil) {
executeJavasScript(wKWeb: self.wKMainWeb, callback: "\(callback)('\(uriSchemeURL!)')")
}
default:
Utils.Log("undefined action")
}
}
}
...
유니버셜 링크를 해봐서 그런지 1시간도 안걸렸다.. iOS도 조금씩 느네?.. 할사람이 없어서 어쩔수 없이 하고 있긴한데.. 하고싶지 않다... ㅠ
참조
https://yoojin99.github.io/app/%EC%95%B1%EC%97%90-URL-Scheme-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0/
[iOS] - 앱에 URL Sceheme 적용하기
앱에 URL Scheme을 적용해보자.
yoojin99.github.io