简体   繁体   English

如何以编程方式查询当前应用程序的URL方案?

[英]How do I interrogate the current app's URL scheme programmatically?

My iOS app has 50+ targets, each with their own custom URL scheme. 我的iOS应用程序有50多个目标,每个目标都有自己的自定义URL方案。 I need to detect if a request from a webview matches the scheme of the currently running app. 我需要检测来自webview的请求是否与当前正在运行的应用程序的方案匹配。 In order to do this, I need to be able to interrogate the current app's URL scheme(s) from code. 为此,我需要能够从代码中查询当前应用程序的URL方案。

Similar questions deal with attempting to interrogate other apps to discover their URL schemes, which seems like it is not possible. 类似的问题涉及试图询问其他应用程序以发现其URL方案,这似乎是不可能的。 I need to find the scheme out for my own app, from within my app. 我需要在我的应用程序中找到我自己的应用程序的方案。

I would like to avoid having to set another plist value to the URL scheme and including that with the target. 我想避免为URL方案设置另一个plist值,并将其包含在目标中。

Is there any way to get the URL scheme of the current app? 有没有办法获得当前应用程序的URL方案?

This is the function I came up with to accomplish this. 这是我想出来实现这一目标的功能。

- (BOOL)doesMatchURLScheme:(NSString *)scheme {
  if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"]) {
    NSArray *urlTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
    for(NSDictionary *urlType in urlTypes)
    {
      if(urlType[@"CFBundleURLSchemes"])
      {
        NSArray *urlSchemes = urlType[@"CFBundleURLSchemes"];
        for(NSString *urlScheme in urlSchemes)
          if([urlScheme caseInsensitiveCompare:scheme] == NSOrderedSame)
            return YES;
      }

    }
  }
  return NO;
}

In Swift 4 : Working version of getting the custom url scheme from Info.plist programmatically 在Swift 4中编程方式从Info.plist获取自定义URL方案的工作版本

func externalURLScheme() -> String? {
        guard let urlTypes = Bundle.main.infoDictionary?["CFBundleURLTypes"] as? [AnyObject],
            let urlTypeDictionary = urlTypes.first as? [String: AnyObject],
            let urlSchemes = urlTypeDictionary["CFBundleURLSchemes"] as? [AnyObject],
            let externalURLScheme = urlSchemes.first as? String else { return nil }

        return externalURLScheme
    }

(with the assumption that there is only 1 element in the urlTypes and urlSchemes array, hence taking the first element). (假设urlTypes和urlSchemes数组中只有1个元素,因此获取第一个元素)。 Here is my plist entry of URL Scheme 这是我的URL方案的plist条目

在此输入图像描述

Extension for Bundle swift 4 syntax Bundle swift 4语法的扩展

extension Bundle {

    static let externalURLSchemes: [String] = {
        guard let urlTypes = main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]] else {
            return []
        }

        var result: [String] = []
        for urlTypeDictionary in urlTypes {
            guard let urlSchemes = urlTypeDictionary["CFBundleURLSchemes"] as? [String] else { continue }
            guard let externalURLScheme = urlSchemes.first else { continue }
            result.append(externalURLScheme)
        }

        return result
    }()

}

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

相关问题 我的应用程序正在由 iOS 中的 URL 方案打开,如何获取完整的 URL? - My app is being opened by a URL scheme in iOS, how do I get the complete url? 应用使用自定义网址方案启动。 完成后如何将数据返回到调用应用程序? - App launched with custom URL scheme. How do I return data to the calling app when done? 如何判断应用程序是从URL方案启动还是正常启动? - How do I tell if the app is launched from a URL scheme or a normal launch? 如何使用URL方案启动iOS Clock应用程序? - How can I launch the iOS Clock app using a a URL scheme? 如何以编程方式从已安装的应用中获取方案? - How to programmatically get scheme from an installed app? 如何创建将被识别为链接的自定义网址方案? - How do I create a custom url scheme that will be recognized as a link? 如何在iOS上通过单元或UI测试URL方案 - How do I unit or UI test a URL scheme on iOS 如何以编程方式检查 ios 设备的当前 userInterfaceStyle? - How can I check ios device's current userInterfaceStyle programmatically? 当应用程序使用URL方案在iOS中打开我的应用程序时,如何获取呼叫者的信息 - How to get the caller's info When a app using URL Scheme to open my App in iOS 在iPhone中以编程方式添加URL方案 - Add URL Scheme Programmatically in iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM