访问 iOS URL Scheme,在运行时

huangapple go评论54阅读模式
英文:

Accessing an iOS URL Scheme, at Runtime

问题

这相当简单。

我有一个发布URL方案的应用程序,就像这样

为了遵循DRY的原则,我想避免使用固定的字符串来引用它。相反,我想从捆绑包中获取它。

我该怎么做?

英文:

This is fairly straightforward.

I have an app that publishes a URL scheme, like so.

In the spirit of DRY, I'd like to avoid referencing it, using constant strings. Instead, I'd like to fetch it from the bundle.

How do I do that?

答案1

得分: 2

这个片段打印了在应用的 Info.plist 中定义的URL schemes:

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)
}
英文:

This snippet prints the URL schemes defined in an app's Info.plist:

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

答案2

得分: 1

这是通过借用Gereon的答案来解决的。这是我是如何做的:

/* ###################################################################################################################################### */
// MARK: - 扩展可寻址实体(通用) -
/* ###################################################################################################################################### */
public extension LGV_MeetingSDK_AddressableEntity_Protocol {
    /* ################################################################## */
    /**
     获取包的第一个URL方案,将其呈现为字符串并返回。
     */
    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: - 扩展可寻址实体(用户) -
/* ###################################################################################################################################### */
public extension HeartOfRecovrr_Member {
    /* ################################################################## */
    /**
     返回此成员记录的可寻址URL。
     */
    var urlString: String { "\(urlScheme)://user/\(id)" }
}

这是它在我的通用Swift扩展包中的位置

英文:

This was solved by leveraging Gereon's answer. Here's how I did it:

/* ###################################################################################################################################### */
// 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)" }
}

And here it is, in my general-purpose Swift extensions package.

huangapple
  • 本文由 发表于 2023年2月6日 20:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定