ios 通过编程从plist中获取URL方案,从今天的扩展中

l0oc07j2  于 11个月前  发布在  iOS
关注(0)|答案(4)|浏览(120)

最后,我尝试通过执行以下操作从Widget启动Main应用程序

UIApplication.SharedApplication.OpenUrl(new NSUrl("MyURLScheme//"));

字符串
所以我有一个iOS应用程序与今天的扩展。我想访问我的主应用程序的URL方案。这是可能的吗?我的URL方案是在我的主应用程序的Plist文件,而不是我的今天的扩展。我可以只引用从主应用程序的一个?我尝试从CFBundleURLTypes和CFBundleURLSchemes获取它,但它们都显示为“null”。可能是因为它在小部件的Plist中查找,而不是主应用程序的Plist。

var asdf2 = NSBundle.MainBundle.InfoDictionary["CFBundleURLTypes"];
var asdf3 = NSBundle.MainBundle.InfoDictionary["CFBundleURLSchemes"];


下面是URL Scheme在主应用程序的Plist文件中的位置的屏幕截图。我不希望在今天扩展的Plist文件中存储重复的信息。我也不希望将URL Scheme硬编码到今天扩展的代码中。


的数据

thtygnil

thtygnil1#

您可以参考下面的URL stackoverflow

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

}

字符串

kd3sttzy

kd3sttzy2#

会是这样的(丑陋的):

var arr = (NSBundle.MainBundle.InfoDictionary["CFBundleURLTypes"] as NSMutableArray);
var urlTypes = arr.GetItem<NSDictionary>(0);
var schemes = urlTypes.ObjectForKey(new NSString("CFBundleURLSchemes")) as NSMutableArray;
var scheme = schemes.GetItem<NSString>(0).ToString();

字符串

olmpazwi

olmpazwi3#

您可能需要为您的应用程序和扩展程序创建一个App Group,这将允许您将plist或任何其他文件放置到磁盘上的共享位置,以便在两个应用程序之间共享。
下面有一篇文章解释了如何做到这一点:https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system#configure-an-app-group

vyswwuz2

vyswwuz24#

extension Bundle {
    static let externalURLSchemes: [String] = {
        guard let urlTypes = main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]],
              let urlSchemesDictionary = urlTypes.first(where: { $0["CFBundleURLSchemes"] != nil }),
              let urlSchemes = urlSchemesDictionary["CFBundleURLSchemes"] as? [String]
        else {
            return []
        }
        return urlSchemes
    }()
}

字符串

相关问题