您的位置:首页 > 移动开发

iOS --- 通过UIApplication的openURL来实现APP之间的相互跳转

2015-09-07 09:37 459 查看
iOS设备中, APP之间的相互跳转主要是通过UIApplication的openURL来实现的.

以Instagram(未提供SDK)为例:

//
//  ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *username = @"icetime017";
    [self openUserPage:username];
}

- (BOOL)isInstagramInstalled {
    NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"];
    return [[UIApplication sharedApplication] canOpenURL:instagramURL];
}

- (void)openUserPage:(NSString *)username {
    NSURL *fansPageURL;
    if ([self isInstagramInstalled]) {
        fansPageURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://user?username=%@", username]];
    } else {
        fansPageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://instagram.com/%@", username]];
    }
    [[UIApplication sharedApplication] openURL:fansPageURL];
}

@end


即:

使用[[UIApplication sharedApplication] canOpenURL:instagramURL];来判断是否已安装该APP,

使用[[UIApplication sharedApplication] openURL:fansPageURL];来打开该APP, 若未安装, 则默认在safari中打开相应页面.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: