美文网首页iOS Developer程序员
iOS app跳转app且传值或者传图片

iOS app跳转app且传值或者传图片

作者: 申申申申申 | 来源:发表于2018-04-11 22:22 被阅读37次

iOS如何在两个app之间跳转,并且传值或者传图


先看看效果
app切换的时候,实现传值和传图片(某些app的大图分享功能,就可以这样实现,当然还有其他方法)

app -> app
在浏览器中打开app
browser -> app
  1. 需要在info.plist中添加URL types
    如下图,URL Schemes下的item 0中填入代表当前app的标识
  1. iOS 9app需要在info.plist中将要使用到的URL Schemes添加到白名单,才可正常跳转或者检查是否安装(比如,想要在当前app中跳转到微信中,那么就需要将微信的URL Schemes添加到白名单)
    info.plist中添加LSApplicationQueriesSchemes,如下
  1. 打开对应app需要使用对应的URL SchemesFFWinter://,后面可以拼接一些需要传递的信息)
    UIApplication.shared.open(URL.init(string: "FFWinter://****")!, options: [:], completionHandler: nil)
  2. 如果跳转的时候,需要携带图片的话需要借助剪贴板UIPasteboard
  3. 跳转到的app中:
    OC在入口类中的- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options中处理
    Swift在入口类中的func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool中处理
  4. 看👇栗子

栗子:

  1. 首先创建两个项目FFWinterFFSpring,并且各自添加对应的URL SchemesLSApplicationQueriesSchemes
  2. FFWinter
    AppDelegate
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let url = url.absoluteString;
    let alertController = UIAlertController.init(title: "", message: url, preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "确认", style: .cancel, handler:nil))
    app.keyWindow?.rootViewController!.present(alertController, animated: true, completion: nil)
    return false
}

vc

@IBAction func openSpringAppClcik(_ sender: Any) {
    UIPasteboard.remove(withName: UIPasteboardName.general)
    UIPasteboard.general.setData(Data.init(), forPasteboardType: "FFWinter")

    let url = URL.init(string: "FFSpring://showSomething")!
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        let alertController = UIAlertController.init(title: "无法打开FFSpring", message: "", preferredStyle: .alert)
        alertController.addAction(UIAlertAction.init(title: "确认", style: .cancel, handler: nil))
        self.present(alertController, animated: true, completion: nil)
    }
}

@IBAction func openSpringAppWithImageClick(_ sender: Any) {
    let pasteboard = UIPasteboard.general
    pasteboard.setData(UIImageJPEGRepresentation(UIImage.init(named: "ali.jpeg")!, 0)!, forPasteboardType: "FFWinter")

    let url = URL.init(string: "FFSpring://showSomething")!
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        let alertController = UIAlertController.init(title: "无法打开FFSpring", message: "", preferredStyle: .alert)
        alertController.addAction(UIAlertAction.init(title: "确认", style: .cancel, handler: { (action) in

        }))
        self.present(alertController, animated: true, completion: nil)
    }
}
  1. FFSpring
    AppDelegate
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let url = url.absoluteString;
    let alertController = UIAlertController.init(title: "", message: url, preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "确认", style: .cancel, handler:nil))

    let vc = app.keyWindow?.rootViewController as! ViewController
    vc.present(alertController, animated: true, completion: nil)

    if let data = UIPasteboard.general.data(forPasteboardType: "FFWinter") {
        vc.imgView.isHidden = false
        vc.imgView.image = UIImage.init(data: data)
    } else  {
        vc.imgView.isHidden = true
    }

    return false
}

vc

@IBAction func openWinterAppClick(_ sender: Any) {
    let url = URL.init(string: "FFWinter://")!
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        let alertController = UIAlertController.init(title: "无法打开FFSpring", message: "", preferredStyle: .alert)
        alertController.addAction(UIAlertAction.init(title: "确认", style: .cancel, handler: nil))
        self.present(alertController, animated: true, completion: nil)
    }
}

即可实现文章开头的效果 ~


不定期更新 不合适的地方 还请指点~ 感激不尽

相关文章

网友评论

    本文标题:iOS app跳转app且传值或者传图片

    本文链接:https://www.haomeiwen.com/subject/uufzhftx.html