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

iOS中,APP间的相互跳转主要通过UIApplication的openURL方法实现。例如使用canOpenURL判断目标APP是否已安装,然后用openURL进行跳转。若未安装,则会默认在Safari中打开相应页面。提供了一个Demo地址用于演示这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值