应用间的相互跳转

来源:互联网 发布:淘宝类目搜索排名 编辑:程序博客网 时间:2024/05/19 16:47

打开其他应用,需要给被打开应用设置schemes:自定义的协议头。

设置协议头

跳转的代码如下:

- (IBAction)skipToWechat {    [self openURLWithString:@"mywechat://"];}- (IBAction)skipToTimeline {    [self openURLWithString:@"mywechat://timeline?news"];}- (IBAction)skipToSession {    [self openURLWithString:@"mywechat://session?news"];}- (void)openURLWithString:(NSString *)urlString{    // 1.获取到对应应用程序的URL    NSURL *wechatURL = [NSURL URLWithString:urlString];    // 2.判断手机中是否安装了对应的应用程序    if ([[UIApplication sharedApplication] canOpenURL:wechatURL]) {        // 3.打开应用程序        [[UIApplication sharedApplication] openURL:wechatURL];    }}

注:@”@”mywechat://timeline?news”中的?前边的mywechat://timeline是要跳转到协议头为myweichat的应用中的timeline页面,后边的news是当前应用的协议头。

跳转后,到目的应用中的AppDelegate类中获取协议,并且半段跳转到什么页面,代码如下:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{    // self.url = url.absoluteString;    // 1.将URL转成字符串    NSString *urlString = url.absoluteString;    // 获取到主页控制器    UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;    [rootNav popToRootViewControllerAnimated:NO];    ViewController *homeVc = [rootNav.childViewControllers firstObject];    homeVc.urlString = urlString;    // 2.判断是通过朋友圈还是微信好友跳转过来    if ([urlString containsString:@"timeline"]) {        [homeVc performSegueWithIdentifier:@"homeToTimeline" sender:nil];    } else if ([urlString containsString:@"session"]){        [homeVc performSegueWithIdentifier:@"homeToSession" sender:nil];    }    return YES;}

从目的应用,跳回到原应用,就用到了收到的协议?后的参数,需要注意的是拼接完整协议头,代码如下:

- (IBAction)backToApp {    // 0.拿到对应应用程序的urlScheme(wechat://session?news)    /*    NSRange range = [self.urlString rangeOfString:@"?"];    range.location;    self.urlString substringFromIndex:range.location     */    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];    }}

转载请注明出处,万分感谢!

原创粉丝点击