IOS 应用程序互相跳转

1. 应用场景

  1. 使用第三方用户登录,需要用户授权,还需要"返回到调用的程序,同时返回授权的用户名"

  2. 应用程序推广,网易彩票,设置-推荐应用-有很多应用程序图标
    -如果本机已经安装过,会直接跳转到另外一个应用程序
    -软件的广告,推广结果,后续会有一些列的金钱上的结算

  3. 支付宝,第三方支付,淘宝,电话费充值。。。

2. 要打开本机上的其他应用程序,需要设置schemes,自定义的协议头,可以打开其他的应用程序

设置

步骤

  1. 定义需要跳转到的APP 的scheme
  2. 直接打开对应的scheme

iOS 9.0之前下面代码可以直接打开

之后需要设置一个白名单
在info.plist文件中添加LSApplicationQueriesSchemes 字段 ,是数组类型,将你想要跳转的app的schema加上去

跳转的代码如下:

- (IBAction)openWangyi:(id)sender
{
   // 跳转到其他应用程序
   // schemes: 网易的scheme wangyi
   NSURL *url = [NSURL URLWithString:@"wangyi://view?newsid=201410130001"];

   // 判断本机是否安装了目标程序
   if ([[UIApplication sharedApplication] canOpenURL:url]) {
       [[UIApplication sharedApplication] openURL:url];
   } else {
       NSLog(@"没有安装,可以再给定下载地址,前往");
   }
}

模仿微信

设置自己的url

截屏2020-05-28下午8.43.55.png

设置info.plist

截屏2020-05-28下午8.44.09.png
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    {
        // 打电话 tel://  发短信  sms:// http://
        // 如果想要跳转到不同的APP , 就是打开对应的 URL(scheme, )
        let url = URL(string: "news://")
        // http: 协议
               // host
               // path
               // query
        
        if UIApplication.shared.canOpenURL(url!)
        {
          print("keyi")
            UIApplication.shared.openURL(url!)
            
        }else
        {
            print("没有安装news")
        }
    }

模仿新闻 设置同微信一样将scheme改一下

跳转到不同的界面

首先在跳转的app里面设置不同的url,

@IBAction func gotoFriend(_ sender: Any)
    {
        let url = URL(string: "weixin://friend?news")
        // http: 协议
               // host
               // path
               // query
        
        if UIApplication.shared.canOpenURL(url!)
        {
            UIApplication.shared.openURL(url!)
            
        }
    }
    @IBAction func gotoMessage(_ sender: Any)
    {
        let url = URL(string: "weixin://message?news")
        // http: 协议
               // host = message
               // path
               // query = news
        
        if UIApplication.shared.canOpenURL(url!)
        {
          print("keyi")
            UIApplication.shared.openURL(url!)
            
        }
    
    }

在被跳转的app的appDelegate里面来进行url的处理,识别host来进行跳转页面

  • app跳转过来会执行下面的方法
//ios 9.0后过期
    func application(_ application: UIApplication, handleOpen url: URL) -> Bool
    {
        print("olde")
        jump(url: url)
    return true
    }
    //ios 9.0之后使用
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
    {
        print("xinde")
        jump(url: url)
        
     return true
    }


 func jump(url:URL)
    {
        let query = url.query
        backScheme = query
        guard let host = url.host else
        {
            return
        }
        print(host)
        //获取跟控制器
        let nav = window?.rootViewController as! UINavigationController
       //自动回到跟控制器 防止跳转层数过多
        nav.popToRootViewController(animated: false)
        
        //得到导航控制器分顶层控制器
        let rootVC  = nav.children[0]
        
        
        //根据实际情况跳转
        
        if host == "friend"
        {
            rootVC.performSegue(withIdentifier: "toFriend", sender: nil)
        }
        if host == "message"
        {
            rootVC.performSegue(withIdentifier: "toMessage", sender: nil)
        }
  • 如果需要点击回到原来的app
//delegate里面加一个参数
class AppDelegate: UIResponder, UIApplicationDelegate {

   var window: UIWindow?
   var backScheme : String?


//在需要回去的地方执行下面的方法
    @IBAction func backToAppAction(_ sender: Any)
   {
       let  appDelegate = UIApplication.shared.delegate as! AppDelegate
           let scheme = appDelegate.backScheme
           guard let myScheme = scheme else{ return}
       let url = URL(string: myScheme + "://")

         if UIApplication.shared.canOpenURL(url!)
         {
             UIApplication.shared.openURL(url!)
             
         }else
         {
             print("没有安装news")
         }
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值