美文网首页
iOS open url 传值回调

iOS open url 传值回调

作者: 小奉不在乎 | 来源:发表于2018-11-16 15:57 被阅读0次

    简单说说吧,open url传值这个网上教程确实不少,关键是都没有说道关键点子上,把值从A传到B容易,再传回来的时候就尴尬了吧,求人不如求己,我就简单记录下吧。废话少说,直接开干。

    1. 新建两个Demo一个为Demo1,一个为Demo2,同时打开倒开info,添加一项URL types为自己的URL Scheme
    image.png image.png
    1. 确定谁给谁传值,就Demo1给给Demo2吧,然后在demo1中把demo2添加为白名单。


      image.png
    2. 搞事情了,在demo1中加个Button然后牵条线整个点击事件试试,顺带传点值

     @IBAction func btnClick(_ sender: Any) {
            
            /// 如果有中文就用
            /// addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)转一下就好了
            let stringUrl = "Demo2://testContent?id=0&title=test&content=testETH&scheme=Demo1"
            
            if let url = URL(string: stringUrl) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
    
    1. 再demo2中的AppDelegate.swift中加入下面的代码
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            let stringUrl = "Demo1://testContent?id=6666&title=test&content=testEOS&scheme=Demo2"
            if let url = URL(string: stringUrl) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
            return true
        }
    
    1. 最后再到demo1中去接收我们修改过后的东西吧,一切都是那么自然
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            
            let s = UIAlertView.init(title: nil, message: url.absoluteString, delegate: nil, cancelButtonTitle: "确定")
            s.show()
            return true
        }
    

    好啦,装逼结束,希望我的朋友路过就点个👍吧,谢谢你们的支持。

    相关文章

      网友评论

          本文标题:iOS open url 传值回调

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