简体   繁体   English

用于 iOS 应用的具有多个捆绑标识符的 URL 类型

[英]URL types with multiple bundle identifier for iOS app

I'd like my iOS project to handle multiple URL types and multiple bundle identifiers, so I can install 2 versions of the app on the same device.我希望我的 iOS 项目能够处理多个 URL 类型和多个包标识符,因此我可以在同一设备上安装 2 个版本的应用程序。

I have 2 different bundle IDs: com.mycompany.myapp and com.mycompany.myapp-test我有 2 个不同的捆绑包 ID:com.mycompany.myapp 和 com.mycompany.myapp-test

I'm registering 2 URL types for both bundle identifiers in the Info.plist file我在 Info.plist 文件中为两个包标识符注册了 2 个 URL 类型

在此处输入图像描述<\/a>

I generate 2 builds with the 2 bundle identifiers and installing both apps on the same device.我使用 2 个捆绑标识符生成 2 个构建,并将两个应用程序安装在同一设备上。 Then I'm trying to reach my apps using 2 urls: myapp:\/\/feed and myapp-test:\/\/feed.然后我尝试使用 2 个网址访问我的应用程序:myapp:\/\/feed 和 myapp-test:\/\/feed。

However for some reason, I cannot open myapp-test and it always opens myapp when I click on both links.但是由于某种原因,我无法打开 myapp-test 并且当我单击两个链接时它总是会打开 myapp。

There is certainly something wrong in the way I handle this so I'm wondering how I can manage multiple bundle ID with multiple URL scheme within the same Xcode project?我处理这个问题的方式肯定有问题,所以我想知道如何在同一个 Xcode 项目中使用多个 URL 方案管理多个包 ID?

Thanks谢谢

"

A bit of investigation produced a few solutions.一些调查产生了一些解决方案。

Automatic自动的

Use $(PRODUCT_BUNDLE_IDENTIFIER) for both identifier and scheme then open each app using URL like com.mycompany.myapp:// .使用$(PRODUCT_BUNDLE_IDENTIFIER)作为标识符和方案,然后使用像com.mycompany.myapp://这样的 URL 打开每个应用程序。 This will always work regardless on how many targets, schemes or build settings you have.无论您拥有多少目标、方案或构建设置,这都将始终有效。

Semi-automatic半自动

Set user-defined variables.设置用户定义的变量。 You need to put these variables in your info.plist file in order to reference them.您需要将这些变量放在info.plist文件中以便引用它们。 This approach is more flexible but you have to write the scheme names by hand (only once).这种方法更灵活,但您必须手动编写方案名称(仅一次)。 Actually it's the way I chosen in my applications.实际上这是我在应用程序中选择的方式。

By hand用手

This is more a workaround than a proper solution but 100% works in all cases: set identifier and scheme before build each bundle.这与其说是一个合适的解决方案,不如说是一种变通方法,但 100% 在所有情况下都有效:在构建每个包之前设置标识符和方案。

For example if you have identifier/scheme pairs like com.mycompany.myapp/myapp and com.mycompany.myapp-test/myapp-test then do the following:例如,如果您有像com.mycompany.myapp/myappcom.mycompany.myapp-test/myapp-test这样的标识符/方案对,则执行以下操作:

  1. Set identifier com.mycompany.myapp and scheme myapp then build app bundle myapp设置标识符com.mycompany.myapp和方案myapp然后构建应用程序包myapp
  2. Set idientifier com.mycompany.myapp-test and scheme myapp-test then build app bundle myapp-test设置标识符com.mycompany.myapp-test和方案myapp-test然后构建应用程序包myapp-test

This way you should be able to open each bundle calling the relative scheme.通过这种方式,您应该能够打开调用相关方案的每个包。 Though you will need to change the URL types before each build.尽管您需要在每次构建之前更改 URL 类型。 Personally I would never do like this.就我个人而言,我永远不会这样做。

Make sure that:确保:

1) for app with com.mycompany.myapp bundle ID you registered mayapp schema, there should be no myapp-test schema registered; 1) 对于com.mycompany.myapp bundle ID 的应用程序,您注册了mayapp架构,应该没有注册myapp-test架构;

same time,同时,

2) for app with com.mycompany.myapp-test bundle ID you registered myapp-test schema, there should be no myapp schema registered. 2) 对于具有com.mycompany.myapp-test捆绑 ID 的应用程序,您注册了myapp-test架构,应该没有注册myapp架构。

If I've got the problem correctly, the reason of such a behavior is both your apps are registered for both schemes.如果我正确地解决了问题,则出现这种行为的原因是您的应用程序都为这两种方案注册了。 When you try to open some URL (myapp-test://... or myapp://... ) iOS detects that there is more then one app registered and picks first to handle it.当您尝试打开某个 URL(myapp-test://... 或 myapp://...)时,iOS 检测到注册的应用程序不止一个,并选择第一个来处理它。

It's not needed to register schemes that you intended to open - you need to register schemes you're going to handle.不需要注册您打算打开的方案 - 您需要注册您要处理的方案。

Hope this helps.希望这可以帮助。

I created config files for each env then I created identifier and schema variables there and then used them in the url identifier and scheme worked like a charm.我为每个环境创建了配置文件,然后在那里创建了标识符和模式变量,然后在 url 标识符中使用它们,并且方案就像一个魅力。

great tutorial for configuring env with config files<\/a>使用配置文件配置 env 的好教程<\/a>

在此处输入图像描述<\/a>

"

What I did (in Xcode 12), partially based on the input from @clamiax我做了什么(在 Xcode 12 中),部分基于来自@clamiax 的输入

Targets -> Your App -> Build Settings -> + -> Add User-Defined setting ->目标 -> 你的应用程序 -> 构建设置 -> + -> 添加用户定义的设置 ->

Setting: URL_SCHEME<\/code>设置: URL_SCHEME<\/code>

Value: yourapp-debug<\/code> , yourapp<\/code> , yourapp-test<\/code> etc. for different schemes.值:用于不同方案的yourapp-debug<\/code> 、 yourapp<\/code> 、 yourapp-test<\/code>等。

Targets -> Your App -> Info -> URL Types目标 -> 您的应用程序 -> 信息 -> URL 类型

URL Schemes: $(URL_SCHEME)<\/code> URL 方案: $(URL_SCHEME)<\/code>

定义价值<\/a> 使用价值<\/a>

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM