• Swift URL Schemes使用


    URL Schemes通常用于分享和第三方登录,但有时需要在html跳至APP,或者APP跳至另外一个APP.这时也需要使用URL Schemes.

    一.html跳转至APP

    eg:html跳转至test1

    在APP中添加URL Schemes,这里的URL Schemes随意写均可,如图:

    2.在AppDelegate.swift中加入,func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject])
        -> Bool这个方法:

     1 //MARK: - 通过下面的方法实现点击html可以打开app
     2   func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject])
     3     -> Bool {
     4       if url.host == nil
     5       {
     6         return true;
     7       }
     8       let urlString = url.absoluteString  //url 的绝对字符串
     9       let queryArray = urlString.componentsSeparatedByString("/")  //通过/把URL切成数组
    10       //传参
    11 //      let alertController = UIAlertController(title: "参数如下",
    12 //                                              message: "(queryArray[2])  (queryArray[3])", preferredStyle: .Alert)
    13 //      let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
    14 //      alertController.addAction(cancelAction)
    15 //      self.window!.rootViewController!.presentViewController(alertController, animated: true, completion: nil)
    16       //打开相应界面
    17       let tababarController = self.window!.rootViewController!.childViewControllers[0] as! UITabBarController
    18       if queryArray[2] == "item1" {
    19         tababarController.selectedIndex = 0  //使tabcarController选中第一个tabbaritem
    20       }else if queryArray[2] == "item2"{
    21         tababarController.selectedIndex = 1  //使tabcarController选中第二个tabbaritem
    22       }
    23       //测试方法:
    24       //先安装test1
    25       //打开浏览器地址栏输入
    26       //  test1://       打开test1
    27       //  test1://item1  打开test1的第一个item
    28       //  test1://item2  打开test1的第二个item
    29       //上面的test1就是工程中配置的URL Schemes
    30       return true
    31   }

    二.APP打开APP

    eg:test1打开test2

    先在test2中配置URl Schemes,然后在test1中写代码即可:

    在点击test1中的一个按钮时,在其点击方法中通过openURL打开即可.

     1  @IBOutlet var btn: UIButton!
     2     override func viewDidLoad() {
     3         super.viewDidLoad()
     4       btn.addTarget(self, action: #selector(Tab2ViewController.btnClick), forControlEvents: .TouchUpInside)
     5         // Do any additional setup after loading the view.
     6     }
     7    func btnClick(){
     8     let urlString = "test2://"
     9     let url = NSURL(string: urlString)
    10     UIApplication.sharedApplication().openURL(url!)
    11    }

    同时,因为iOS9的安全因素,需要在test1中设置白名单.设置方法参考:http://www.cnblogs.com/shaoting/p/5148323.html

    其余常见应用的URL Schemes:http://www.cnblogs.com/shaoting/p/5148323.html

    demo下载:https://github.com/pheromone/URLSchemes

                  http://download.csdn.net/detail/shaoting19910730/9496841

  • 相关阅读:
    Linux netstat命令详解
    【转】Jenkins怎么启动和停止服务
    Jenkins权限配置失误后导致登录失败的解决办法
    Linux下查看某一进程所占用内存的方法
    Linux集群配置ntp时间同步服务
    Linux下安装MySQL数据库
    Redis集群的部署
    Linux下安装Nginx服务器
    为Linux服务器设置静态IP的方法
    vSphere Client无法连接到服务器 出现未知错误的解决方法
  • 原文地址:https://www.cnblogs.com/shaoting/p/5412390.html
Copyright © 2020-2023  润新知