A App에서 B App 실행하면서 파라메터 전송하기

 

A App

// A App : 실행 시키는 앱
// testButton 버튼을 클릭하면 schemeUrl 전송

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func testButton(_ sender: UIButton) {
        if let schemeUrl = URL(string: "bapp://open?VC=testVC&color=purple&food=cake&birth=0716") {
            
            if UIApplication.shared.canOpenURL(schemeUrl) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(schemeUrl, completionHandler: { (success) in
                        if success {
                            print("The URL was delivered successfully.")
                        } else {
                            print("The URL failed to open")
                        }
                    })
                } else {
                    UIApplication.shared.openURL(schemeUrl as URL)
                }
            } else {
                print("Invalid URL specified.")
            }
        }
    }
 }
 

 

B App

Project -> TARGETS -> Info -> URL Types 추가

  - Identifier : 번들아이디

  - URL Schemes : bapp

// B App 에서 두군데 동일한 코드가 들어가야 됨. 
// IOS 12 이하 일 경우
class AppDelegate: UIResponder, UIApplicationDelegate {
	// MARK: APP to APP URL SCHEME 처리 로직 (IOS 12 이하)
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        
        log(direction: .ETC, ofType: self, datas: "스키마 : \(url)")
        
        /*
         ex : "bapp://open?VC=testVC&color=purple&food=cake&birth=0716"
         */
        guard url.scheme == "bapp", url.host == "open" else {
            return false
        }
        
        let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
        let urlQueryItems = urlComponents?.queryItems ?? []
        log(direction: .SCHEME, ofType: self, datas: "AppToApp : \(urlQueryItems)")
        
        return true
    }
}

// IOS 13 이상 일 경우
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
	// MARK: APP to APP URL SCHEME 처리 로직 (IOS 13 이상)
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let url = URLContexts.first?.url else { return }
        /*
         ex : "bapp://open?VC=testVC&color=purple&food=cake&birth=0716"
         */
        guard url.scheme == "bapp", url.host == "open" else {
            return
        }
        
        let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
        let urlQueryItems = urlComponents?.queryItems ?? []
        log(direction: .SCHEME, ofType: self, datas: "AppToApp : \(urlQueryItems)")
    }
}

+ Recent posts