今天在做一个音乐播放器的项目,发现这个点击退出程序的功能不能实现终于找到了一些有用的资料,就去网上看了半天资料,下面是退出程序的代码:

在动画里面可以自己添加一些,动画,达到相应的效果。

   AppDelegate *app = [UIApplication sharedApplication].delegate;
UIWindow *window = app.window;
[UIView animateWithDuration:1.0f animations:^{
window.alpha = ;
window.frame = CGRectMake(, window.bounds.size.width, , );
} completion:^(BOOL finished) {
exit();
}];

在一个程序中打开另一个应用程序 参考

应用之间跳转的原理:

在iOS中打开一个应用程序只需要拿到这个应用程序的协议头即可,所以我们只需配置应用程序的协议头即可。

假设有应用A应用B两个应用,现在需要从应用A跳转到应用B中。

  • 原理:通过设置跳转到应用B的URL Schemes(自定义的协议头),应用B将其自身“绑定”到一个自定义URL Schemes上,就可以从应用A中利用应用B的URL Schemes启动应用B了。

具体怎么做呢,下面一步步来教你,先来个简单点的:从应用A跳转到应用B。设置App-B的URL Schemes

应用A跳转到应用B
  1. 首先我们用Xcode创建两个iOS应用程序项目,项目名称分别为App-A、App-B。

  2. 选择项目App-B -> TARGETS -> Info -> URL Types -> URL Schemes,设置App-B的URL Schemes为AppB。

在应用程序App-A中添加一个用来点击跳转的Button,并监听点击事件,添加跳转代码。

//添加跳转按钮
- (IBAction)jumpToAppB:(id)sender {
// 1.获取应用程序App-B的URL Scheme
NSURL *appBUrl = [NSURL URLWithString:@"AppB://"]; // 2.判断手机中是否安装了对应程序
if ([[UIApplication sharedApplication] canOpenURL:appBUrl]) {
// 3. 打开应用程序App-B
[[UIApplication sharedApplication] openURL:appBUrl];
} else {
NSLog(@"没有安装");
}
}

注意:

  1. 如果是iOS9之前的模拟器或是真机,那么在相同的模拟器中先后运行App-B、App-A,点击按钮,就可以实现跳转了。

  2. 如果是iOS9之后的模拟器或是真机,那么则需要再在应用程序App-A中将App-B的URL Schemes添加到白名单中,原因和做法如下。

    • iOS9引入了白名单的概念。

    • 在iOS9中,如果使用 canOpenURL:方法,该方法所涉及到的 URL Schemes 必须在"Info.plist"中将它们列为白名单,否则不能使用。key叫做LSApplicationQueriesSchemes ,键值内容是对应应用程序的URL Schemes。

具体做法就是在App-A的Info文件中,添加LSApplicationQueriesSchemes数组,然后添加键值为AppB的字符串。

 
添加LSApplicationQueriesSchemes数组,然后添加键值为AppB的字符串

添加白名单之后在相同的模拟器中先后运行App-B、App-A,点击按钮,就可以实现跳转了。

假设有两个程序如下: App_a、App_b,我们要是现在 App_a 里触发某个事件去打开 App_b,具体实现如下所示:

建立一个新的项目 App_a 步骤如下所示:

在 App_a 中设置跳转按钮,如下所示:

并做好关联,在 ViewController.m 中的代码如下所示:

#import "ViewController.h"
#import "AppDelegate.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} // 跳转到 App——b
- (IBAction)backToApp_b_method:(UIButton *)sender { // 1.获取应用程序App-B的URL Scheme
// 下面代码是跳转的关键代码;AppB:// 就是我们第二个 APP 的 URL Schemes 。
NSString *urlStr = @"AppB://";
NSURL *url = [NSURL URLWithString:urlStr]; // 2.判断手机中是否安装了对应程序
if ([[UIApplication sharedApplication] canOpenURL:url]) {
// 3. 打开应用程序App-B
[[UIApplication sharedApplication] openURL:url];
} else {
NSLog(@"没有安装");
} } // 退出 App——a 程序
- (IBAction)exitApp_a_method:(UIButton *)sender { AppDelegate *app = [UIApplication sharedApplication].delegate;
UIWindow *window = app.window;
[UIView animateWithDuration:1.0f animations:^{
window.alpha = ;
window.frame = CGRectMake(, window.bounds.size.width, , );
} completion:^(BOOL finished) {
exit();
}]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

在 App_a 的 info 文件中设置如下:

我们这里已经将错误的 URL  Schemes  改为正确的 如下所示:

在 App_b 中也做如上的设置,新建 App_b ,设置 info.plist 文件, 在 info.plist 文件里添加   LSApplicationQueriesSchemes  数组,在数组中插入一个字符串元素为 App_a 就是一个能够打开的 App 的名字, 在 URL Types 里新增 URL (AppB) 并绑定 App_b 的 bundleID (com.jw.AppB) 如下图所示。

在 App_b 的 Main 文件里也做几个按钮,用于跳转到 App_a 中 并做好sb 与代码的关联,代码如下:

#import "ViewController.h"
#import "AppDelegate.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} // 跳转到 App——a 程序
- (IBAction)backToApp_a_method:(UIButton *)sender { // 下面代码是跳转的关键代码;App_a:// 就是我们第一个 APP 的 URL Schemes 。
NSString *urlStr = @"AppA://";
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:url];
} // 退出 App——b 程序
- (IBAction)exitApp_b_method:(UIButton *)sender { AppDelegate *app = [UIApplication sharedApplication].delegate;
UIWindow *window = app.window; [UIView animateWithDuration:1.0f animations:^{ window.alpha = ;
window.frame = CGRectMake(, window.bounds.size.width, , ); } completion:^(BOOL finished) { exit(); }];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

此时运行两个 App ,我们在 App_a 的页面中点击跳转到 App_b 就能够实现页面的跳转了。

应用App_a跳转到应用App_b的特定界面

很多时候,我们做应用程序之间的跳转并不只是跳转到其他程序就可以了,而是要跳转到其他程序的特定页面上。比如我们在浏览网页时,会有分享到微信朋友圈或是分享给微信朋友,这就需要跳转到微信朋友圈界面或是微信朋友选择界面。

具体如何做呢?

1、首先我们先来为App-b搭建两个页面 FFPageViewController 和 HHPageViewController 。这里用导航控制器 Push 两个 ViewController,通过Push 来进入 FFPaeViewControler 所以将 FFPageViewController 的页面标示绑定为 FFPage,通过Storyboard Segue设置HHPageViewController的标识符绑定,"HHPage"。之所以这样做,是为了实现两种技术下的页面跳转。如下所示:

代码为:

Appdelegate.m 文件修改代码如下:

#import "AppDelegate.h"
#import "ViewController.h"
#import "FFPageViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
} - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
// 1.获取导航栏控制器
UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
// 2.获得主控制器
ViewController *mainVc = [rootNav.childViewControllers firstObject]; // 保存完整的App-A的URL给主控制器
mainVc.urlString = url.absoluteString; // 3.每次跳转前必须是在跟控制器(细节)
[rootNav popToRootViewControllerAnimated:NO]; // 4.根据字符串关键字来跳转到不同页面
if ([url.absoluteString containsString:@"FFPage"]) { // 跳转到应用App_b的FFPage页面 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"FFPageViewController" bundle:[NSBundle mainBundle]]; FFPageViewController * ffPage = [storyboard instantiateViewControllerWithIdentifier:@"FFPageViewController"]; ffPage.urlString = mainVc.urlString;//传递原来的App 的 URL scheme
[mainVc.navigationController pushViewController:ffPage animated:YES]; } else if ([url.absoluteString containsString:@"HHPageSegue"]) { // 跳转到应用App_b的HHViewController页面 // 根据segue标示进行跳转
[mainVc performSegueWithIdentifier:@"HHPageSegue" sender:nil]; } return YES; } @end

ViewController.h文件修改

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// 用来保存App_a完整的URL
@property (nonatomic, copy) NSString *urlString; @end

ViewController.m文件修改

#import "ViewController.h"
#import "AppDelegate.h"
#import "HHViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} // 跳转到 App——a 程序
- (IBAction)backToApp_a_method:(UIButton *)sender { // 1.获取应用程序App-A的URL Scheme
// 下面代码是跳转的关键代码;App_a:// 就是我们第一个 APP 的 URL Schemes 。
NSString *urlStr = @"AppA://";
NSURL *url = [NSURL URLWithString:urlStr]; // 2.判断手机中是否安装了对应程序
if ([[UIApplication sharedApplication] canOpenURL:url]) {
// 3. 打开应用程序App-A
[[UIApplication sharedApplication] openURL:url];
} else {
NSLog(@"没有安装");
} } // 退出 App——b 程序
- (IBAction)exitApp_b_method:(UIButton *)sender { AppDelegate *app = (id)[UIApplication sharedApplication].delegate;
UIWindow *window = app.window; [UIView animateWithDuration:1.0f animations:^{ window.alpha = ;
window.frame = CGRectMake(, window.bounds.size.width, , ); } completion:^(BOOL finished) { exit(); }];
} // 使用 segue 进行跳转
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"HHPageSegue"]) {
// 获得将要跳转的界面HHViewController的控制器
HHViewController *HHPage = segue.destinationViewController;
// 保存完整的App_a的URL给跳转界面HHViewController,方便后期能够返回的操作等....这里不再写了
HHPage.urlString = self.urlString;
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end

FFPageViewController.h 文件

#import <UIKit/UIKit.h>

@interface FFPageViewController : UIViewController

// 用来保存App_a完整的URL
@property (nonatomic, copy) NSString *urlString; @end

FFPageViewController.m 文件

#import "FFPageViewController.h"

@interface FFPageViewController ()

@end

@implementation FFPageViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
} // 返回 App_a 程序
- (IBAction)backToApp_aMethod:(UIButton *)sender { // 1.拿到对应应用程序的 URL Schemes
NSString *urlSchemeString = [[self.urlString componentsSeparatedByString:@"?"] lastObject];
NSString *urlString = [urlSchemeString stringByAppendingString:@"://"]; // 1.获取对应应用程序的URL
NSURL *url = [NSURL URLWithString:urlString]; // 2.判断是否可以打开
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else {
NSLog(@"没有安装原来的程序,着说不通啊,你是咋跳转过来的昂?");
} } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
 

在 HHViewController.h 文件里添加属性 urlString 字段。

在 HHViewController.m 文件里添加如下代码,并与 StoryBoard 文件关联:

// 返回 App_a 程序
- (IBAction)backTo_App_aMethod:(UIButton *)sender { // 1.拿到对应应用程序的URL Scheme
NSString *urlSchemeString = [[self.urlString componentsSeparatedByString:@"?"] lastObject];
NSString *urlString = [urlSchemeString stringByAppendingString:@"://"]; // 1.获取对应应用程序的URL
NSURL *url = [NSURL URLWithString:urlString]; // 2.判断是否可以打开
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else {
NSLog(@"没有安装原来的程序,着说不通啊,你是咋跳转过来的昂?");
} }

2、然后再在 App_a 中添加一些用来跳转的页面,并进行关联,如下所示:

3、注意,从应用程序 App_b 跳回 App_a 的实现原理如下:

步骤分析:

  1. 我们想要从应用B再跳转回应用A,那么在跳转到应用B的时候,还应将应用A的URL Schemes传递过来。这样我们才能判断应该跳转回哪个应用程序。

    • 这样我们指定一个传递URL的规则:协议头://应用B的URL Schemes?应用A的URL Schemes。即:AppB://Page1?AppA

    • 说明:

      • AppB是跳转过来的应用App-B的URL Schemes;

      • Page1是用来区别跳转页面的标识;

      • ? 是分割符;

      • AppA是跳转回的应用App-A的URL Schemes

  2. 我们根据传递来的数据,进行反跳回去。

    1. 之前我们在应用App-B中通过AppDelegate执行不同页面的跳转。在对应方法中我们可以拿到完整的URL,在主控制器ViewController中设定一个属性,将该URL保存在主控制器中。

    2. 在主控制器中我们可以通过- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;方法获取将要跳转的页面控制器。

    3. 在将要跳转的页面控制器中定义一个属性,用于接受、截取出跳转回的应用(即App-A)的URL Schemes,执行跳转。

4、实现效果图如下:

首先运行两个程序,保证安装成功。

5、资源链接,记得点赞!

 
 
 

IOS:程序的退出、App间的跳转的更多相关文章

  1. iOS中两个APP之间的跳转和通信

    app间的跳转 一:在第一个app首先要做下面这些操作: 1.在info.plist文件中的Information Property List下添加一项:URL types. 2.点开URL type ...

  2. iOS应用程序开发之应用间的跳转(用着微信等第三方分享登陆)

    简介 配置和实现 判断应用启动方式 一.简介 最实际项目开发中,我们难免会遇到需要从一个应用跳转到另一个应用的情况.比如微信分享,实际就是一种应用间的跳转.但是有时候我们需要实现自己的两个应用间的跳转 ...

  3. iOS应用程序开发之应用间的跳转

    简介 配置和实现 判断应用启动方式 一.简介 最实际项目开发中,我们难免会遇到需要从一个应用跳转到另一个应用的情况.比如微信分享,实际就是一种应用间的跳转.但是有时候我们需要实现自己的两个应用间的跳转 ...

  4. App间相互跳转及图片分享

    A-app: Info--URL Types--URL Schemes:A-app(一个标识,允许别的app调用本App) info.plist 添加白名单: LSApplicationQueries ...

  5. iOS用户是否打开APP通知开关跳转到系统的设置界面

    1.检测用户是否打开推送通知  /** 系统通知是否打开 @return 是否打开 */ //检测通知是否打开iOS8以后有所变化 所以需要适配iOS7 + (BOOL)openThePushNoti ...

  6. iOS程序崩溃*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [37.5 nan]'

    今天上班打开昨天的程序运行,昨天跑的很溜的程序今天竟然crash了,好郁闷啊!下面附上crash的栈打印信息: 经过一番调试终于找到了原因,程序crash是因为CALayer的位置中含有不存在的数,就 ...

  7. iOS 总结APP间跳转的常用以及非常用需求 APP跳转Safari APP跳转APP

    需求驱动技术,有了新的需求,旧技术无法实现时,就会有新的技术出现. 一般的APP跳转需求有以下几种: 1.  从自己的APP跳转到别人的APP. 2. 从自己的APP跳转系统APP. 3. 让别人的A ...

  8. Url Scheme实现APP间通信、分享

    代码地址如下:http://www.demodashi.com/demo/12748.html 简介: URL Scheme就是一个可以让app相互之间可以跳转的对外接口.通过给APP定义一个唯一的U ...

  9. iOS 程序间跳转传参(支付和地图)

    两个APP之间的跳转是通过[[UIApplication sharedApplication] openURL:url]这种方式来实现的. 1.首先设置第一个APP的url地址 2.接着设置第二个AP ...

随机推荐

  1. BZOJ3786 星际探索

    @(BZOJ)[DFS序, Splay] Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其 ...

  2. 让win7任务条上的文件夹打开是c,d,e,f而不是库

    如果资源管理器是打开的,则右键点击资源管理器的图标,在跳出的菜单中,右键点击“Windows资源管理器”,选择“属性”. 在“快捷方式’选项卡,“目标”一栏,默认的是 %windir%\explore ...

  3. (转) go Cron的使用

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...

  4. Go -- 中开启gctrace

    godebug是一个跨平台的Go程序调试工具,传统的编译型语言调试器使用底层系统调用并读取二进制文件用于调试各类符号.使用起来很麻烦而且很难移植. godebug使用不同的方法,直接把源码作为目标程序 ...

  5. 大众车机天宝187A Hack笔记

    0×00前言 自从去年买了车,对汽车电子系统的兴趣就上来了.这不,前一阵子逛汽车论坛,发现了有网友将老版本的天宝车机被刷上了2017新帕萨特车机的系统,支持超级蓝牙和苹果CarPlay,百度CarLi ...

  6. Zabbix监控Mongo

    安装Zabbix-agent # groupadd zabbix # useradd -g zabbix zabbix # yum -y install gcc mysql-community-dev ...

  7. linux系统之shell编程-正則表達式

    shell编程正則表達式: 1:元字符   [ ]  .   *  ? + ( )  |  {  }  ^  $ 2 : [a-z0-9]  表示匹配随意数字和字母的一个 3 :  [^a-z]    ...

  8. 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期

    腾讯云图片鉴黄集成到C#   官方文档:https://cloud.tencent.com/document/product/641/12422 请求官方API及签名的生成代码如下: public c ...

  9. 整理对Spark SQL的理解

    Catalyst Catalyst是与Spark解耦的一个独立库,是一个impl-free的运行计划的生成和优化框架. 眼下与Spark Core还是耦合的.对此user邮件组里有人对此提出疑问,见m ...

  10. UWP 新手教程2——怎样实现自适应用户界面

    系列文章 UWP新手教程1--UWP的前世今生 如上文所说的,布局面板依据可用的屏幕空间.指定界面元素的大小和位置. 比如StackPanel 会水平或垂直排列界面元素.Grid 布局与CSS 中的表 ...