如何以编程方式询问当前应用程序的URL方案? [英] How do I interrogate the current app's URL scheme programmatically?

查看:105
本文介绍了如何以编程方式询问当前应用程序的URL方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用有50多个目标,每个目标都有自己的自定义URL方案.我需要检测来自Webview的请求是否与当前正在运行的应用程序的方案匹配.为此,我需要能够从代码中查询当前应用程序的URL方案.

My iOS app has 50+ targets, each with their own custom URL scheme. I need to detect if a request from a webview matches the scheme of the currently running app. 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. I need to find the scheme out for my own app, from within my app.

我想避免为URL方案设置另一个plist值,并将其包含在目标中.

I would like to avoid having to set another plist value to the URL scheme and including that with the target.

有什么方法可以获取当前应用程序的URL方案吗?

Is there any way to get the URL scheme of the current app?

推荐答案

这是我想到的功能.

- (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;
}

这篇关于如何以编程方式询问当前应用程序的URL方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆