以编程方式从今天的扩展中的plist获取URL方案

时间:2017-06-29 22:38:13

标签: ios xamarin.ios info.plist today-extension

最终我正试图通过

从Widget启动主应用程序
UIApplication.SharedApplication.OpenUrl(new NSUrl("MyURLScheme//"));

所以我有一个今天扩展的iOS应用程序。我想访问我的主应用程序的URL方案。那可能吗?我的URL Scheme位于我的主应用程序的Plist文件中,而不是我今天的扩展名。我可以从主应用程序中引用一个吗?我尝试从CFBundleURLTypes和CFBundleURLSchemes中获取它,但它们都变为“null”。  可能是因为它正在查看小部件的Plist而不是主应用程序的Plist。

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

以下是URL Scheme位于主应用的Plist文件中的屏幕截图。我宁愿不必在今天的扩展程序的Plist文件中存储重复的信息。而且我也不想将URL Scheme硬编码到今天的扩展代码中。

enter image description here

3 个答案:

答案 0 :(得分:0)

会像这样(丑陋):

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

答案 1 :(得分:0)

您可能需要为您的应用和扩展程序创建App Group,这样您就可以将要在2之间共享的plist或任何其他文件放入磁盘上的共享位置。

这是一篇解释如何完成的文章: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system#configure-an-app-group

答案 2 :(得分:0)

您可以在下面引用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
 }()

}