iOS app跳转app且传值或者传图片
2018-04-11 本文已影响37人
申申申申申
iOS
如何在两个app
之间跳转,并且传值或者传图
先看看效果
app
切换的时候,实现传值和传图片(某些app
的大图分享功能,就可以这样实现,当然还有其他方法)
在浏览器中打开
app
browser -> app
- 需要在
info.plist
中添加URL types
如下图,URL Schemes
下的item 0
中填入代表当前app
的标识
-
iOS 9
中app
需要在info.plist
中将要使用到的URL Schemes
添加到白名单,才可正常跳转或者检查是否安装(比如,想要在当前app
中跳转到微信中,那么就需要将微信的URL Schemes
添加到白名单)
在info.plist
中添加LSApplicationQueriesSchemes
,如下
- 打开对应
app
需要使用对应的URL Schemes
(FFWinter://
,后面可以拼接一些需要传递的信息)
UIApplication.shared.open(URL.init(string: "FFWinter://****")!, options: [:], completionHandler: nil)
- 如果跳转的时候,需要携带图片的话需要借助剪贴板
UIPasteboard
- 跳转到的
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
中处理 - 看👇栗子
栗子:
- 首先创建两个项目
FFWinter
与FFSpring
,并且各自添加对应的URL Schemes
和LSApplicationQueriesSchemes
-
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)
}
}
-
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)
}
}
即可实现文章开头的效果 ~
不定期更新 不合适的地方 还请指点~ 感激不尽