URL:统一资源定位符http://www.baidu.com tel://110 file:///apple/Desktop/
协议头Scheme:http:// tel:// file://
资源路径path:www.baidu.com 110 /apple/Desktop/
要想实现应用间的跳转,必须配置协议头
项目->info->url types -> + ->配置协议头
示例:
新闻应用:
@interface ViewController ()
/**
* 点击之后跳转到微信
*/
- (IBAction)jump;
/**
* 点击微信好友按钮跳转到微信
*/
- (IBAction)sesstion;
/**
* 点击朋友圈按钮跳转到微信
*/
- (IBAction)timeline;
@end
@implementation ViewController
// 跳转到主页
- (IBAction)jump {
[self openWeiXin:@"weixin://"];
}
// 跳转到好友列表
- (IBAction)sesstion {
[self openWeiXin:@"weixin://session?news"];
}
// 跳转到朋友圈
- (IBAction)timeline {
[self openWeiXin:@"weixin://timeline"];
}
- (void)openWeiXin:(NSString *)urlStr
{
// 1.创建要打开的App的URL
NSURL *weixinURL = [NSURL URLWithString:urlStr];
// 2.判断是否该URL可以打开
if ([[UIApplication sharedApplication] canOpenURL:weixinURL]) {
// 3.打开URL
[[UIApplication sharedApplication] openURL:weixinURL];
}
}
@end
微信应用:
AppDelegate
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
/**
* 当通过别应用打开该应用的时候会执行该方法
*
* @param url 通过哪一个URL跳转过来的
*/
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// 取出来根控制器
UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
[rootNav popToRootViewControllerAnimated:NO];
// 取出ViewController
ViewController *mainVc = [rootNav.childViewControllers firstObject];
NSString *urlStr = url.absoluteString;
if ([urlStr rangeOfString:@"session"].length) {
mainVc.appURLStr = urlStr;
// 跳转到微信好友界面
[mainVc performSegueWithIdentifier:@"session" sender:nil];
} else if ([urlStr rangeOfString:@"timeline"].length){
// 跳转到朋友圈界面
[mainVc performSegueWithIdentifier:@"timeline" sender:nil];
}
return YES;
}
@end
ViewController
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic,copy)NSString *appURLStr;
@end
#import "ViewController.h"
#import "SessionViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"session"]) {
SessionViewController *sessionVc = segue.destinationViewController;
sessionVc.appURLStr = self.appURLStr;
}
}
@end
SessionViewController
#import <UIKit/UIKit.h>
@interface SessionViewController : UIViewController
@property(nonatomic,copy)NSString *appURLStr;
@end
#import "SessionViewController.h"
@interface SessionViewController ()
/**
* 返回到跳转过来的APP
*/
- (IBAction)backToApp;
@end
@implementation SessionViewController
- (IBAction)backToApp {
NSRange range = [self.appURLStr rangeOfString:@"?"];
NSString *appStr = [self.appURLStr substringFromIndex:(range.location + 1)];
NSString *appURL = [NSString stringWithFormat:@"%@://", appStr];
NSURL *url = [NSURL URLWithString:appURL];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
@end