iOS应用内打开其他应用

  • 新建工程demo1,添加URL Types,填写 URL Schemes,如下图
iOS应用内打开其他应用_第1张图片
Paste_Image.png
  • 在demo1的appDelegate.m文件中,实现代理方法,如下
#pragma mark
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if (url)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"你唤醒了您的应用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
    return YES;
}
  • 新建工程Demo2,添加按钮,单击事件,方法如下
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(30, 10, 90, 90);
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor lightGrayColor]];
//    [btn addTarget:self action:@selector(dragMove:withEvent:) forControlEvents:UIControlEventTouchDragInside];
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

//跳转到demo1
- (void)btnAction
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mahong://"]];
}

- (void)dragMove:(UIButton *)sender withEvent:(UIEvent *)event
{
    sender.center = [[[event allTouches] anyObject] locationInView:self.view];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

如果想跳转到appStore,则写法如下:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://"]];

附:[常用URL Schmes查询网站](http://handleopenurl.com/ 可以通过搜索主流app名称,找到URL Schemes,然后做应用内跳转,例如QQ的URL Schemes 为 mqq:open

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mqq:open//"]];
iOS应用内打开其他应用_第2张图片
Paste_Image.png

你可能感兴趣的:(iOS应用内打开其他应用)