IOS之URL Scheme的使用

详细讲解使用【iOS开发】打开另一个APP(URL Scheme与openURL)
【iOS开发】打开另一个APP(URL Scheme与openURL) - 简书

参考链接:IOS之URL Scheme的使用(可以在app之间调用传递数据)_保卫的专栏-CSDN博客
iOS URL Schemes 的定义和使用_暗夜_新浪博客
第一:什么是URL Scheme

与Android的URL Scheme类似,是为方便app之间互相调用而设计的。你可以通过一个类似URL的链接,通过系统的OpenURl来打开该app,并可以传递一些参数。每个URL必须能唯一标识一个APP,如果你设置的URL与别的APP的URL冲突,此时,你的APP不一定会被调用起来,原因是当APP在安装的时候就已经在系统里面注册了此APP的URL Scheme,如果你的一致但是是后安装的,那么系统不会调用你的APP,因为你的APP设置的URL scheme被覆盖了。

当然系统的APP的URL Scheme是优先级高的,不用想着能覆盖系统APP的URL Scheme的注册调用。

打开别人的项目
1.打开Mail
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://info@icloud.com"]]
    2.打开电话
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://18688886666"]];
    3.打开SMS
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:18688886666"]];

第二:URL Scheme有什么作用:

大家知道在IOS系统里面APP之间是相互隔离的,不像Android,每个组件都可以作为一个独立的功能被其他APP调用,但是,IOS系统里面也需要完成类似于三方功能如支付、搜索跳转、导航等等跨APP的功能,怎么实现呢,苹果就使用了URL Scheme来实现了这个功能。通过各个APP设计的符合苹果的统一规范的URL Scheme,系统就会自动去调用相关的APP来完成你的请求。

比如:我们的APP需要使用支付宝的三方支付功能、我的APP需要使用微信分享好的文章,那么此时就可以通过URL Scheme来传递这些数据到支付宝APP或者微信APP,系统会通过这些APP的URL Scheme来调起这些APP,完成你所需要做的跨APP的功能。
首先:配置你的APP,被别人打开我的app

要为 iOS 程序添加自定义协议的支持是一件很方便的事,只需要在程序的 Info.plist 添加一个 URL types 节点就可以了。在这个节点里,可以设置这个程序所支持的自定义协议名称,像 http、ftp 这种,一般我们可以设置为程序英文名称,像淘宝客户端中就设置了 taobao,这样 taobao:// 这个形式的 URL 就会关联到淘宝客户端的 App。

其次 处理使用你的URL Scheme来调起你的APP的请求

如果你的APP为TestB,如果处理成功的Scheme如包含了TestBAPP://callsuccess,那么说明你调用其他的APP成功了。如果不是,那么说明是别的APP如TestAAPP调用了你的APP,此时在你的APPDelegate里面添加如下函数以及实现处理,这里是直接返回告诉TestAAPP调用成功的标识TestAAPP://callsuccess

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url  
{  
    // Do something with the url here  
    if (!url)  
    {  
        return NO;  
    }  
    NSString *handleUrl = [url absoluteString];  
    if ([handleUrl isEqualToString:@"TestBApp://callsuccess"]) {  
        return YES;  
    }else{  
        NSString *urlstr = @"TestAAPP:/com.baidu.sidepath.TestA&_callback=TestAApp://callsuccess";  
        NSURL *handlbackeUrl = [NSURL URLWithString:urlstr];  
        [[UIApplication sharedApplication] openURL:handlbackeUrl];  
      
    }

如果你不想直接返回callback,而是想启动一个页面那么,此时要考虑你的应用是否已经启动,可以如下判断使用

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    NSString *handleUrl = [url absoluteString];
    if ([handleUrl isEqualToString:@"TestBApp://callsuccess"]) {
        return YES;
    }else{
        UINavigationController *vc = (UINavigationController *)_window.rootViewController;
        if (vc == nil) {
            PathViewController *controller = [[PathViewController alloc] initWithNibName:@"PathViewController" bundle:nil];
            
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            self.mUINavigationController = [[UINavigationController alloc] init];
            
            
            [self.mUINavigationController pushViewController:controller animated:YES];
            [self.window addSubview:self.mUINavigationController.view];
            
            
            // Override point for customization after application launch.
            self.window.backgroundColor = [UIColor whiteColor];
            [self.window makeKeyAndVisible];
        }
        return YES

也就是把appdelegate里面的didFinishLaunchingWithOptions初始化app的代码拷贝进去。此时会启动PathViewController这个页面。然后在这个页面里面可以添加一个返回按钮来返回到调用APP。

再次 在TestAAPp里面使用URl Scheme调起你的APP
NSURL *url = [NSURL URLWithString:urlstr];[[UIApplication sharedApplication] openURL:url];


 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native 可以通过配置 URL Scheme 实现从外部应用程序打开应用程序并传递参数。下面是 iOSAndroid 上配置 URL Scheme 的步骤: ## iOS 1. 在 Xcode 中选择项目,进入 TARGETS -> Info -> URL Types,点击“+”按钮添加 URL Scheme。 2. 在 URL Scheme 输入框中填写自定义的 URL Scheme,例如:myapp。 3. 在 URL Schemes 中填写应用程序的 Bundle ID。 4. 在 Xcode 中保存并编译项目。 5. 在应用程序中,可以通过 Linking 模块的 getInitialURL 方法获取启动应用程序的 URL,并通过 parse 函数解析 URL 中的参数。 示例代码: ```javascript import { Linking } from 'react-native'; Linking.getInitialURL().then(url => { if (url) { const { queryParams } = Linking.parse(url); console.log(queryParams); } }); ``` ## Android 1. 在 AndroidManifest.xml 中添加 intent-filter。 ```xml <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" /> </intent-filter> ``` 2. 在 data 标签中填写自定义的 URL Scheme。 3. 在应用程序中,可以通过 Linking 模块的 getInitialURL 方法获取启动应用程序的 URL,并通过 parse 函数解析 URL 中的参数。 示例代码: ```javascript import { Linking } from 'react-native'; Linking.getInitialURL().then(url => { if (url) { const { queryParams } = Linking.parse(url); console.log(queryParams); } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值