美文网首页
IOS 应用程序互相跳转

IOS 应用程序互相跳转

作者: 越天高 | 来源:发表于2020-05-28 23:17 被阅读0次

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")
         }
   }

相关文章

  • IOS 应用程序互相跳转

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

  • 应用间跳转

    本文简单介绍iOS应用程序互相跳转的方法 2.要打开本机上的其他应用程序,需要设置schemes,自定义的协议头,...

  • 本地通知

    iOS7 iOS8要获取用户允许 接收到通知时带参数应用程序跳转

  • iOS app互相跳转

    只为了自己日后记忆, 应用A跳转到应用B 应用B的处理(在白名单中加入) URL identifier 可以没有 ...

  • iOS项目与flutter项目,互相跳转传值

    iOS已有项目集成flutter,网上大把资料,这里主要记录互相跳转传值 OC跳转到flutter界面,如跳转到H...

  • iOS - 应用程序跳转schemes

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

  • 应用间跳转

    iOS 9.0之后 应用程序跳转 需要设置白名单info.plist 增加 LSApplicationQueri...

  • ios flutter混编时键盘不隐藏问题

    在ios项目中嵌入flutter项目时,会有flutter界面和ios原生界面直接的互相跳转,如果flutter界...

  • 看别人博客留存的重要知识:iOS应用程序间相互跳转

    应用间相互跳转简介 在iOS开发的过程中,我们经常会遇到需要从一个应用程序A跳转到另一个应用程序B的场景。这就需要...

  • 一分钟学会app跳转

    在iOS开发的过程中,我们经常会遇到需要从一个应用程序A跳转到另一个应用程序B的场景。这就需要我们掌握iOS应用程...

网友评论

      本文标题:IOS 应用程序互相跳转

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