應用跳轉.png
- 場景需求:一個應用A(以news應用為示例)跳轉到另外一個應用B(以weChat為示例),常見需求如下
- 1.應用推薦
- 2.支付寶支付
- 3.第三方登錄
- 4.微信分享
- 注意:iOS9中打開一個應用程序的URL必須配置info.plist文件
- 添加LSApplicationQueriesSchemes的key
- 添加對應url的scheme
一: 打開系統的應用程序
打開打電話應用程序
URL:tel://電話號碼
打開發短信應用程序
URL:sms://電話號碼
打開系統的設置界面,必須先在info.plist中配置URL Schemes
在URL Types中添加prefs
打開Wifi設置
URL:prefs:root=WIFI
打開定位服務
URL:prefs:root=LOCATION_SERVICES
打開藍牙服務
URL:prefs:root=Bluetooth
打開FaceTime
URL:prefs:root=FACETIME
打開音樂
URL:prefs:root=MUSIC
打開墻紙設置
URL:prefs:root=Wallpaper
配置如圖下圖
sc.png
- 分析:核心代碼:
[[UIApplication sharedApplication] openURL:url]
一:應用A跳轉應用B
1.界面搭建如圖
設置.png
2.A應用打開B應用的方法
- (IBAction)jumpToweChat:(id)sender {
NSURL *url = [NSURL URLWithString:@"weixin://"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
- (IBAction)jumpToFriends:(id)sender {
NSURL *url = [NSURL URLWithString:@"weixin://session?news"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
- (IBAction)jumpToTimeLIne:(id)sender {
NSURL *url = [NSURL URLWithString:@"weixin://timeLine?news"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
2.設置應用A和應用B的URL Types中的URL Schemes
A.png
B.png
3.在應用B中監聽跳轉,進行判斷,執行不同的跳轉
- 在AppDelegate中實現下面的方法監聽
// 已過期
//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
// 已過期
//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
/**
* 通過別的應用打開我們的應用時會來到這里
* @param url 通過什么URL打開的
* @param sourceApplication 打開我們應用的sourceApplication
*/
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
// 1.獲取通過那一個URL打開我的應用程序
NSString *urlStr = url.absoluteString;
// 2.取出window的根控制器
UINavigationController *rootNav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
// 首先回到根控制器
[rootNav popToRootViewControllerAnimated:NO];
// 3.取出MainViewController,使用主要控制器就可以跳轉到另外兩個控制器
ViewController *rootVc = rootNav.childViewControllers.firstObject;
// 傳值
rootVc.urlPath = urlStr;
if ([urlStr containsString:@"session"]) { // 好友界面
[rootVc performSegueWithIdentifier:@"session" sender:nil];
}else if ([urlStr containsString:@"timeLine"]) {// 朋友圈界面
[rootVc performSegueWithIdentifier:@"timeLine" sender:nil];
}
return YES;
}
4.從應用B跳轉到應用A
// 這里用storyboard做的,監聽程序的跳轉
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"session"]) {
SessionViewController *vc = segue.destinationViewController;
vc.urlPath = self.urlPath;
}else if ([segue.identifier isEqualToString:@"timeLine"]) {
TimeLineViewController *vc = segue.destinationViewController;
vc.urlPath1 = self.urlPath;
}
}
5.微信朋友圈界面設置
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"微信朋友圈";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
}
- (void)clickLeftButton
{
// 截取字符串,拿到scheme
NSInteger location = [self.urlPath1 rangeOfString:@"?"].location;
NSString *scheme = [self.urlPath1 substringFromIndex:location + 1];
// 通過scheme返回新聞
NSString *news = [NSString stringWithFormat:@"%@://", scheme];
NSURL *newsUrl = [NSURL URLWithString:news];
if ([[UIApplication sharedApplication] canOpenURL:newsUrl]) {
[[UIApplication sharedApplication] openURL:newsUrl];
}
}
6.微信好友界面設置
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"微信好友界面";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
}
- (void)clickLeftButton
{
// 截取字符串,拿到scheme
NSInteger location = [self.urlPath rangeOfString:@"?"].location;
NSString *scheme = [self.urlPath substringFromIndex:location + 1];
// 通過scheme返回新聞
NSString *news = [NSString stringWithFormat:@"%@://", scheme];
NSURL *newsUrl = [NSURL URLWithString:news];
if ([[UIApplication sharedApplication] canOpenURL:newsUrl]) {
[[UIApplication sharedApplication] openURL:newsUrl];
}
}
示例圖:
demo.gif