xcode 在运行时访问iOS URL方案

myzjeezk  于 2023-02-09  发布在  iOS
关注(0)|答案(2)|浏览(120)

这是相当简单的。
我有一个发布URL方案like so的应用程序。
本着DRY的精神,我希望避免使用常量字符串来引用它,而是希望从bundle中获取它。
我该怎么做呢?

guz6ccqo

guz6ccqo1#

此代码段打印应用的Info.plist中定义的URL方案:

if let types = Bundle.main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]] {
    var result = [String]()
    for type in types {
        guard let schemes = type["CFBundleURLSchemes"] as? [String] else { continue }
        guard let scheme = schemes.first else { continue }
        result.append(scheme)
    }
    print(result)
}
mfuanj7w

mfuanj7w2#

这个问题是通过利用Gereon's答案解决的。下面是我的做法:

/* ###################################################################################################################################### */
// MARK: - Fleshing out the Addressable (General) -
/* ###################################################################################################################################### */
public extension LGV_MeetingSDK_AddressableEntity_Protocol {
    /* ################################################################## */
    /**
     This fetches the first URL scheme from the bundle, renders it as a String, and returns it.
     */
    var urlScheme: String {
        guard let bundleTypes = Bundle.main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]],
              let myURLScheme = (bundleTypes.first?["CFBundleURLSchemes"] as? [String])?.first
        else { return "" }

        return myURLScheme
    }
}

/* ###################################################################################################################################### */
// MARK: - Fleshing out the Addressable (User) -
/* ###################################################################################################################################### */
public extension HeartOfRecovrr_Member {
    /* ################################################################## */
    /**
     This returns an addressable URL for this member record.
     */
    var urlString: String { "\(urlScheme)://user/\(id)" }
}

它就在这里,在我的通用Swift扩展包中。

相关问题