在iOS開(kāi)發(fā)中經(jīng)常遇到需要分享的需求,分享功能的實(shí)現(xiàn)就涉及到應(yīng)用中的跳轉(zhuǎn),今天就來(lái)簡(jiǎn)單介紹一下蘋果自帶的跳轉(zhuǎn)功能實(shí)現(xiàn).
- 應(yīng)用A跳轉(zhuǎn)到應(yīng)用B
-
首先需要在B應(yīng)用中做出相應(yīng)的配置
- 在info下找到URL Types中的URL Schemes給你的B應(yīng)用綁定一個(gè)URL標(biāo)識(shí)
- 然后再在應(yīng)用A中實(shí)現(xiàn)如下代碼
NSURL *urlString = [NSURL URLWithString:@"B://"];
if ([[UIApplication sharedApplication] canOpenURL:urlString]) {
[[UIApplication sharedApplication] openURL:urlString];
}
完成以上操作就可以從A應(yīng)用跳轉(zhuǎn)到B應(yīng)用了
- 應(yīng)用A跳轉(zhuǎn)到應(yīng)用B中的其他頁(yè)面
- 在A中實(shí)現(xiàn)如下代碼
NSURL *urlString = [NSURL URLWithString:@"B://Other"];
if ([[UIApplication sharedApplication] canOpenURL:urlString]) {
[[UIApplication sharedApplication] openURL:urlString];
}
注意:NSURL *urlString = [NSURL URLWithString:@"B://Other"];這行代碼中需要在B://后面加上一個(gè)用來(lái)跟B://進(jìn)行區(qū)分的字符串
- 然后再在B的AppDelegate中實(shí)現(xiàn)如下方法
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
ViewController *rootVc = (ViewController *)self.window.rootViewController;
NSString *urlString = url.absoluteString;
ZDHomeViewController *homeVc = [rootVc.childViewControllers firstObject];
if ([urlString containsString:@"Other"]) {
[homeVc.navigationController pushViewController:[[ZDOtherController alloc] init] animated:YES];
}
return YES;
}
完成以上操作就可以從A跳轉(zhuǎn)到B中相應(yīng)的界面了
`注意:NSURL *urlString = [NSURL URLWithString:@"B://Other"];中B://后面加的字符串并不會(huì)影響從A跳轉(zhuǎn)到B`
- 從應(yīng)用B回到應(yīng)用A
當(dāng)我們從A跳轉(zhuǎn)到B做完我們想要進(jìn)行的操作后,需要再?gòu)腂回到A 那么需要就進(jìn)行以下的操作- 首先在A中的項(xiàng)目文件里面進(jìn)行相應(yīng)配置(操作見(jiàn)文章最上面的圖片)
需要注意的是第三步中的URL標(biāo)識(shí)需要進(jìn)行改動(dòng) 不能和B一樣 URL標(biāo)識(shí)就以A為例
- 首先在A中的項(xiàng)目文件里面進(jìn)行相應(yīng)配置(操作見(jiàn)文章最上面的圖片)
- 還需要在A中注意一點(diǎn) 在
NSURL *urlString = [NSURL URLWithString:@"B://Other?A"];
if ([[UIApplication sharedApplication] canOpenURL:urlString]) {
[[UIApplication sharedApplication] openURL:urlString];
}
中[NSURL URLWithString:@"B://Other?A"]
傳入的字符串最好給定一些格式 比如在后面接上?A
以進(jìn)行區(qū)分
- 再在B中實(shí)現(xiàn)如下代碼
NSString *urlSchemeString = [[self.urlString componentsSeparatedByString:@"?"] lastObject];
NSString *urlString = [urlSchemeString stringByAppendingString:@"://"];
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
注意:在B中如何獲取A中所傳入的urlString呢? 需要完成如下幾步操作
1.在B中相應(yīng)的控制器.h文件中搞一個(gè)屬性保存
@property (nonatomic,copy)NSString *urlString;
2.在上述提到的AppDelegate所實(shí)現(xiàn)的方法中接受URLString
homeVc.urlString = urlString;
完成以上操作就可以從B回到A了
以上僅僅是簡(jiǎn)單介紹了一下系統(tǒng)自帶的跳轉(zhuǎn)功能實(shí)現(xiàn) 想要更好的完成需求還需要深入的學(xué)習(xí)與研究...