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

释放双眼,带上耳机,听听看~!

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

人已赞赏
iOS文章

iOS NSObject的load和initialize方法

2020-3-31 10:31:49

iOS文章

iOS在NSObject子类的执行代码中实现UIViewController的跳转

2020-3-31 11:46:57

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索