iOS应用跳转(包括iPhone原有应用跳转和第三方应用跳转)

article/2025/9/22 13:55:32

文章目录

      • iOS应用间跳转简介
      • 应用间跳转应用场景
      • 应用间跳转实现步骤
      • 跳转到本地或第三方应用的一些URL Schemes

iOS应用间跳转简介

  • 在开发的过程中,我们经常会遇到需要从一个应用程序跳转到另一个应用程序的场景。这就需要我们掌握iOS应用程序之间的相互跳转知识。

应用间跳转应用场景

  • 使用第三方用户登录,跳转到需授权的App。如QQ登录,微信登录等。需要用户授权,还需要"返回到调用的程序,同时返回授权的用户名、密码"。
  • 应用程序推广,跳转到另一个应用程序(本机已经安装),或者跳转到iTunes并显示应用程序下载页面(本机没有安装)。
  • 第三方支付,跳转到第三方支付App,如支付宝支付,微信支付。
  • 内容分享,跳转到分享App的对应页面,如分享给微信好友、分享给微信朋友圈、分享到微博。
  • 显示位置、地图导航,跳转到地图应用。
  • 使用系统内置程序,跳转到打电话、发短信、发邮件、Safari打开网页等内置App中。

应用间跳转实现步骤

  • 在iOS中打开一个应用程序只需要拿到这个应用程序的协议头即可,所以我们只需配置应用程序的协议头即可。
  • 具体步骤
  1. 首先我们用Xcode创建两个iOS应用程序项目,项目名称分别为A_app、B_app。(A跳B)
  2. 选择项目TARGETS->B_app -> Info -> URL Types -> URL Schemes,设置B_app的URL Schemes为Bapp(也可以在B_app的info.plist文件里设置)。如下图:
    设置URL Schemes
  3. 如果是iOS 9之后还需要在A_app的info.plist文件中添加B_app的URL Schemes白名单。因为在iOS9中,如果使用 canOpenURL:方法,该方法所涉及到的 URL Schemes 必须在"Info.plist"中将它们列为白名单,否则不能使用。key叫做LSApplicationQueriesSchemes ,键值内容是对应应用程序的URL Schemes。具体设置如下图:
    添加白名单
  4. 都设置完成后,A_app调用UIApplicationcanOpenURL方法,协议头后面可以拼接参数传递过去,如下所示:
NSURL * appurl = [NSURL URLWithString:@"Bapp://"];//判断是否安装了对应的应用,安装了就打开if ([[UIApplication sharedApplication] canOpenURL:appurl]) {[[UIApplication sharedApplication]openURL:appurl];}else{UIAlertView * alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请安装相应的应用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];[alertV show];}

5.当A_app调用了canOpenURL方法,B_app会响应- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options代理方法,在该代理方法中可以获取传过来的URL,通过拦截url传过来的参数可以处理跳到对应的模块。

  • 主要示例如下:
    A_app中Main.storyboard中添加三个按钮
    这里写图片描述
    相关按钮点击事件:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)jumpToHome:(id)sender {//注意设置URL Schemes 事里面不要有“—”,否则可能会跳不过去NSURL * appurl = [NSURL URLWithString:@"Bapp://"];//判断是否安装了对应的应用,安装了就打开if ([[UIApplication sharedApplication] canOpenURL:appurl]) {[[UIApplication sharedApplication]openURL:appurl];}else{UIAlertView * alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请安装相应的应用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];[alertV show];}
}
- (IBAction)jumpToBlue:(id)sender {NSURL * appurl = [NSURL URLWithString:@"Bapp://BlueViewController"];if ([[UIApplication sharedApplication] canOpenURL:appurl]) {//iOS10以后过期[[UIApplication sharedApplication]openURL:appurl];}else{UIAlertView * alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请安装相应的应用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];[alertV show];}
}
- (IBAction)jumpToPurple:(id)sender {NSURL * appurl = [NSURL URLWithString:@"Bapp://SubViewController"];if ([[UIApplication sharedApplication]canOpenURL:appurl]) {//iOS10以后有效[[UIApplication sharedApplication]openURL:appurl options:nil completionHandler:nil];}
}
@end

应用程序B_app中文件结构和AppDelegate主要实现方法:
B_app中文件结构
主要实现的代理方法:

#import "AppDelegate.h"
@interface AppDelegate ()
@end@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];self.window.backgroundColor = [UIColor whiteColor];UITabBarController * tabbarVC = [[UITabBarController alloc]init];NSArray * rootVCNameArray = [NSArray arrayWithObjects:@"RedViewController",@"GreenViewController",@"BlueViewController", nil];for (NSString * vcName in rootVCNameArray) {UIViewController * vc = [[NSClassFromString(vcName) alloc]init];UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:vc];navVC.navigationBar.tintColor = [UIColor brownColor];navVC.title = vcName;[tabbarVC addChildViewController:navVC];}self.window.rootViewController = tabbarVC;[self.window makeKeyAndVisible];return YES;
}
//当其他应用跳转到当前应用就会调用该代理方法,iOS9及以后有效
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{//获取跟控制器UITabBarController * tabbarVC = (UITabBarController *)self.window.rootViewController;NSLog(@"scheme : %@",url.scheme);NSLog(@"absoluteString : %@",url.absoluteString);if ([url.absoluteString containsString:@"BlueViewController"]) {tabbarVC.selectedIndex = 2;}else if ([url.absoluteString containsString:@"SubViewController"]){tabbarVC.selectedIndex = 0;UIViewController * vc = [[NSClassFromString(@"SubViewController") alloc]init];[tabbarVC.childViewControllers.firstObject pushViewController:vc animated:YES];}return YES;
}
//-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
//    return YES;
//}
//-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
//
//    return YES;
//}

@end
最终效果图:
最终效果图
如果想跳回A_app,可以添加A的URL Schemes,在B_app中添加白名单,调用openURL方法即可。

跳转到本地或第三方应用的一些URL Schemes

  • 苹果自带应用URL Schemes:
@{"APP":@"打电话",@"Schemes":@"tel"},
@{@"APP":@"发短信",@"Schemes":@"sms"},
@{@"APP":@"打开日历",@"Schemes":@"calshow"},
@{@"APP":@"提醒",@"Schemes":@"x-apple-reminder"},
@{@"APP":@"邮件",@"Schemes":@"message"},
@{@"APP":@"iTunes Store",@"Schemes":@"itms"},
@{@"APP":@"App Store",@"Schemes":@"itms-apps"},
@{@"APP":@"iBooks",@"Schemes":@"ibooks"},
@{@"APP":@"Facetime",@"Schemes":@"facetime"}                                                                                                                                                                                                                     
  • 一些常见的第三方应用URL Schemes:
<key>LSApplicationQueriesSchemes</key><array><!-- 微信 URL Scheme 白名单--><string>wechat</string><string>weixin</string><!-- 新浪微博 URL Scheme 白名单--><string>sinaweibohd</string><string>sinaweibo</string><string>sinaweibosso</string><string>weibosdk</string><string>weibosdk2.5</string><!-- QQ、Qzone URL Scheme 白名单--><string>mqqapi</string><string>mqq</string><string>mqqOpensdkSSoLogin</string><string>mqqconnect</string><string>mqqopensdkdataline</string><string>mqqopensdkgrouptribeshare</string><string>mqqopensdkfriend</string><string>mqqopensdkapi</string><string>mqqopensdkapiV2</string><string>mqqopensdkapiV3</string><string>mqzoneopensdk</string><string>wtloginmqq</string><string>wtloginmqq2</string><string>mqqwpa</string><string>mqzone</string><string>mqzonev2</string><string>mqzoneshare</string><string>wtloginqzone</string><string>mqzonewx</string><string>mqzoneopensdkapiV2</string><string>mqzoneopensdkapi19</string><string>mqzoneopensdkapi</string><string>mqzoneopensdk</string><!-- 支付宝  URL Scheme 白名单--><string>alipay</string><string>alipayshare</string>
</array>
  • 如果跳苹果自带的应用则不需要在info.plist文件中添加白名单,其他的都需要添加白名单如下:
    设置第三方应用URL Schemes
    关键代码如下:
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic ,copy) NSArray * dataSource;
@end
@implementation ViewController
-(NSArray *)dataSource{if (!_dataSource) {_dataSource =  @[@{@"APP":@"打电话",@"Schemes":@"tel"},@{@"APP":@"发短信",@"Schemes":@"sms"},@{@"APP":@"打开日历",@"Schemes":@"calshow"},@{@"APP":@"提醒",@"Schemes":@"x-apple-reminder"},@{@"APP":@"邮件",@"Schemes":@"message"},@{@"APP":@"iTunes Store",@"Schemes":@"itms"},@{@"APP":@"App Store",@"Schemes":@"itms-apps"},@{@"APP":@"iBooks",@"Schemes":@"ibooks"},@{@"APP":@"Facetime",@"Schemes":@"facetime"},@{@"APP":@"微信",@"Schemes":@"wechat"},@{@"APP":@"微博",@"Schemes":@"sinaweibo"},@{@"APP":@"QQ",@"Schemes":@"mqq"},@{@"APP":@"支付宝",@"Schemes":@"alipay"},];}return _dataSource;
}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JHCell"];NSDictionary * dic = self.dataSource[indexPath.row];cell.textLabel.text = [dic valueForKey:@"APP"];cell.detailTextLabel.text = [dic valueForKey:@"Schemes"];return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSDictionary * dic = self.dataSource[indexPath.row];NSString * schemes = [dic valueForKey:@"Schemes"];NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@://",schemes]];if ([[UIApplication sharedApplication] canOpenURL:url]) {[[UIApplication sharedApplication]openURL:url options:nil completionHandler:nil];}
}
@end

效果图如下:
效果图:

  • 相关原码下载:iOS应用间跳转

  • 参考文档:
    iOS开发–从一个应用跳转到设置选项(从一个应用跳转到另外一个应用)
    iOS两个APP之间的跳转和传值
    史上最全canOpenURL: failed问题解决办法

http://chatgpt.dhexx.cn/article/ussNcXx1.shtml

相关文章

iOS开发~社区分享

1、概要 随着iOS平台逐渐成熟&#xff0c;连最初让人恼火的社区分享功能现在都变的几行代码就可以搞定&#xff0c;下面分享下“分享”功能原生实现。 2、实现 iOS6之前使用TWTweetComposeViewController实现分享功能&#xff0c;但iOS6之后改用SLComposeViewController实现&…

番茄助手 VS2015

转载地址&#xff1a; https://www.cnblogs.com/DswCnblog/p/5625893.html 首先在官网下载原版&#xff1a; VA_X_Setup2270_0.exe VA_X官方下载链接&#xff1a;http://www.wholetomato.com/downloads/default.asp 安装官版。 下载补丁 。 下载链接: http://pan.baidu.c…

linux安装番茄时钟,Tomato Pie番茄钟

Tomato Pie插件简介 Tomato Pie 是一款开源的Chrome 新标签页扩展&#xff0c;适用于番茄工作法&#xff0c;直观&#xff0c;简单&#xff0c;拥有马赛克统计图、浏览器通知、扩展栏图标倒计时等功能。我们之前介绍过一款just Focus - 番茄钟免费应用软件(Mac、iOS)。 番茄钟是…

番茄学习--番茄工具推荐

首先,这个工具是青峰推荐的,自己觉得用的很顺手,就推荐给大家。 这是一个在线的番茄学习工具--番茄土豆。他的网址是:https://pomotodo.com/ 下面我来说说这个工具的特点吧: 首先,在开始番茄之前,你什么都不用想,只需要点击“开始一个番茄”按钮即可。这样你就开始了一…

番茄助手 VS2015

转载地址&#xff1a; https://www.cnblogs.com/DswCnblog/p/5625893.html 1. 首先在官网下载原版&#xff1a; VA_X_Setup2270_0.exe VA_X官方下载链接&#xff1a;http://www.wholetomato.com/downloads/default.asp 2. 安装官版。 3. 下载补丁 。 下载链接: http:/…

短视频平台-小说推文(番茄小说)推广任务详情

字节旗下平台:番茄小说 今日头条&#xff0c;抖音故事&#xff0c;抖音漫画官方每周只出一次数据!!!!! 预计每周二出上周四之前的数据&#xff0c;有时官方回传数据较晚&#xff0c;会延迟到周三出! 请达人知悉! 注意: 再次强调!番茄拉新规则! 是以设备第一次下载番茄小说后搜…

找不到或无法加载主类

springboot项目时不时的就会出现这个错误&#xff0c; 错误: 找不到或无法加载主类 com.ruoyi.RuoYiApplication 解决方案&#xff1a; 自己在maven clean之后&#xff0c;会把target里边的清理表&#xff0c;项目就需要重新进行编译&#xff0c;这时候如果运行的话就会出现以上…

运行mvn -v时报错:找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher

运行 mvn -v 时候出现报错 找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher 其中的可能原因为下载maven时下载了这个版本的maven&#xff08;src&#xff09; 应该下载这个版本maven&#xff08;bin&#xff09;

关于无法加载localhost/9/Login的问题以及解决办法(小白的我,大佬略过)

关于无法加载localhost&#xff1a;8088/9/Login的问题以及解决办法&#xff08;小白的我&#xff0c;大佬略过&#xff09; **这是我出现的问题。**那么如何解决呢&#xff1f;请看下面的图 其实就是正常的代码问题&#xff0c;我认为这个是我之前的习惯造成的&#xff01; 正…

错误: 找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher

起因&#xff1a;由于在做两个java项目&#xff0c;使用的jdk和maven都不一致&#xff0c;所以导致环境变量有些混乱 看来是maven出问题了 cmd mvn -v 果然&#xff0c;报错如下 The JAVA_HOME environment variable is not defined correctly This environment variable is …

找不到主类或无法加载

问题描述&#xff1a; 1、用Notepad编写了一个例子&#xff0c;在cmd模式下运行提示&#xff1a;找不到主类或无法加载。 2、打开cmd,输入java&#xff0c;java -version没有问题&#xff0c;但是javac提示不是内部命令。 问题排查&#xff1a; 1、找到java安装下的bin目录…

由于无法加载editlog导致namenode无法启动问题处理

版本:hadoop3.2.1 namenode数量:5 1. 问题描述 hadoop-nn01节点的namenode服务无法启动,报错如下Exiting with status 1: org.apache.hadoop.hdfs.server.namenode.EditLogInputException: Error replaying edit log at offset 0. Expected transaction ID was 4095226791 N…

Not allowed to launch ‘videowebplugin://‘ because a user gesture is require;高版本google浏览器,海康插件启动失败解决办

问题&#xff1a;使用海康视频插件&#xff0c;插件在谷歌浏览器中启动失败问题&#xff08;这个海康插件实在是坑人&#xff09; 原因&#xff1a; 是因为海康的插件还没支持高版本谷歌,emmmmmmmmmmmm 解决办法&#xff1a; 第一种: 使用低版本谷歌浏览器&#xff08;94版本…

错误: 找不到或无法加载主类 com.

报错&#xff1a;错误: 找不到或无法加载主类 com. 根据个人经验的一种办法&#xff0c;仅供参考&#xff1a; 1、 2、 按步骤勾选上&#xff0c;重新执行即可成功&#xff0c;如果不行的话&#xff0c;原因应该不一样&#xff0c;请另寻他法。

解决找不到或无法加载主类的问题

今天重新配置了下环境&#xff0c;但是在用cmd运行字节码文件的时候&#xff0c;显示找不到或无法加载主类 。因为环境配置我看了下没有问题&#xff0c;可以运行。也没有网上说的跟包有关系&#xff08;因为我根本就没有写包&#xff09;。 最后&#xff0c;经过一番查找&…

报错:Avoided redundant navigation to current location: “/login“.完美解决

需求&#xff1a; 登陆之后才能查看主页面&#xff0c;否则跳转至登录页 遇到的问题&#xff1a; 一开始是打算直接在拦截器跳转路由的&#xff0c;发现会报错Avoided redundant navigation to current location: "/login". 后来灵机一动&#xff0c;用了全局路由…

错误: 找不到或无法加载主类。

今儿个学习Java时看到了关于这篇博客的解释。 我来梳理下。 ⭐如果一个类有包名&#xff0c;那么就不能在任意位置存放他。否则JVM将无法加载这样的类。这就是会出现错误提示的原因。(我的主类是写在了com.book.chapter1包下的。而我存放在了桌面。) ⭐在运行有包名的源文件时&…

报错:找不到或者无法加载主类

Hello&#xff0c;World 从11.30晚上八点开始下载JDK搭建环境再到下载编辑器到cmd亮出了“Hello&#xff0c;World&#xff01;”已经是12.1的凌晨一点十三分。这个对程序员来说颇具仪式感的代码终于让我弄出来了&#xff0c;浏览器满是CSDN的页面让人心疼&#xff0c;踏入jav…

JAVA中无法加载主类什么意思_找不到或无法加载主类什么意思?

今天我们将探讨java的一个常见问题,即如何修复java中的“error:couldnotfind or load main class”错误。顾名思义,当java找不到您要执行的类时,就会发生这个错误。为了更好地理解错误,您应该熟悉类路径CLASSPATH。如果你不熟悉类路径,那么请检查一下什么是类路径以及它与…

vue项目出现此地址,但是访问时显示,嗯… 无法访问此页面localhost 已拒绝连接

vue项目出现此地址&#xff0c;但是访问时显示&#xff0c;嗯… 无法访问此页面localhost 已拒绝连接。 出现原因&#xff1a;端口号8081被占用 解决办法&#xff1a; 1、先查看端口号&#xff0c;winR打开命令提示符&#xff0c;输入netstat -an&#xff0c;出现很多端口 如图…