iOS 从一个app打开另一个app

本文详细介绍了如何在iOS应用A中通过定义协议(URLSchemes和URLidentifier),并使用UIApplication的openURL方法,在iOS应用B中实现相互调用的功能。包括在B应用的info.plist中配置URLSchemes,以及在A应用中编写响应代码来处理接收到的URL请求。

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

iOS-定义协议从一个app打开另一个app  

注:当前app为A,被打开的为B


第一步:对B操作(对B的info.plist中自定义url types)

技术分享

注意, 这里的URL Schemes必填, URL identifier选填。

另外,URL Schemes建议都小写,由于之后接收到数据的时候,不区分大写和小写, 都是转为小写。

规定的格式是   URL Schemes://URL identifier




第二步:对A操作(在响应方法中添加响应代码,如图所示)

上图代码:
        NSURL *url = [NSURL URLWithString:@"martinapp://com.martin.app"];
        [[UIApplication sharedApplication] openURL:url];

第三步:
    模拟器运行:先对B进行Build操作,而对A进行run即可
    真机运行:首先保证B存在于真机中,而后运行A即可。

 你须要在 Appdelegate.m中加入例如以下代码, 来处理接受到请求后做出的处理
//处理URL请求
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    NSLog(@"%@", url);
    
    if ([[url scheme] isEqualToString:@"myurltest"])
    {
        //处理链接
        NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"新消息" message:text delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        [myAlert show];
        
        return YES;
    }
    
    return NO;
}

PS E:\tool\my-desktop-app> npm install electron robotjs npm warn deprecated npmlog@4.1.2: This package is no longer supported. npm warn deprecated are-we-there-yet@1.1.7: This package is no longer supported. npm warn deprecated boolean@3.2.0: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info. npm warn deprecated gauge@2.7.4: This package is no longer supported. npm warn cleanup Failed to remove some directories [ npm warn cleanup [ npm warn cleanup 'E:\\tool\\my-desktop-app\\node_modules', npm warn cleanup [Error: EPERM: operation not permitted, rmdir 'E:\tool\my-desktop-app\node_modules\bl\node_modules\readable-stream'] { npm warn cleanup errno: -4048, npm warn cleanup code: 'EPERM', npm warn cleanup syscall: 'rmdir', npm warn cleanup path: 'E:\\tool\\my-desktop-app\\node_modules\\bl\\node_modules\\readable-stream' npm warn cleanup } npm warn cleanup ] npm warn cleanup ] npm error code 1 npm error path E:\tool\my-desktop-app\node_modules\robotjs npm error command failed npm error command C:\WINDOWS\system32\cmd.exe /d /s /c prebuild-install || node-gyp rebuild npm error gyp info it worked if it ends with ok npm error gyp info using node-gyp@10.1.0 npm error gyp info using node@18.20.7 | win32 | x64 npm error gyp info find Python using Python version 3.10.0 found at "C:\Users\lijian\AppData\Local\Programs\Python\Python310\python.exe" npm error gyp ERR! find VS npm error gyp ERR! find VS msvs_version not set from command line or npm config npm error gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt npm error gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details. npm error gyp ERR! find VS npm error gyp ERR! find VS Failure details: undefined npm error gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details.
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值