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

来源:互联网 发布:不用网络的手机收音机 编辑:程序博客网 时间:2024/06/05 17:10

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中打开相应页面.

输出log:

-canOpenURL: failed for URL: "instagram://" - error: "(null)"instagram : 0-canOpenURL: failed for URL: "instagram://location?id=1" - error: "(null)"

未安装instagram, 则调用canOpenURL: failed for URL. 返回0
调用openURL, 直接跳转至instagram的网页.

Demo

Demo地址: DemoOpenURL

1 0
原创粉丝点击