iOS 一个app启动另一个app

2020-10-23  本文已影响0人  gaookey

新建两个项目 demo1 和 demo2。完成项目 demo1 跳转到项目 demo2

在项目 demo2 的 Info.plist 中添加字段 URL types -> URL Schemes,配置供别的app跳转使用的唯一URL demo2JumpUniqueKey(不要使用 _ ,不然会跳转失败)。

image.png

在项目 demo1的 Info.plist 中添加字段 LSApplicationQueriesSchemes, 类型设置为 Array,添加项目 demo2 中配置的URL demo2JumpUniqueKey

image.png

项目 demo1 中跳转的方法

//不带参数的跳转
//guard let urlLocal = URL(string: "demo2JumpUniqueKey:") else { return }
//带参数跳转
guard let urlLocal = URL(string: "demo2JumpUniqueKey://name=lisi,password=123"), UIApplication.shared.canOpenURL(urlLocal) else {
    print("跳转失败")
    return
}
UIApplication.shared.openURL(urlLocal)

项目 demo2 中接收项目 demo1 跳转传来的参数

如果存在 SceneDelegate 则使用 SceneDelegate.swift 的方法

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    
    guard let context = URLContexts.first else { return }
    
    //demo2JumpUniqueKey://name=lisi,password=123
    print(context.url)
    
    //<UISceneOpenURLOptions: 0x600002cb26a0; sourceApp: com.swiftprimer.demo1; annotation: (null); openInPlace: NO>
    print(context.options)
}

如果不存在 SceneDelegate 则使用 AppDelegate.swift 的方法

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    
    //demo2JumpUniqueKey://name=lisi,password=123
    print(url)

    return true
}
上一篇下一篇

猜你喜欢

热点阅读