应用间跳转

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值