访问 iOS URL Scheme,在运行时

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

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:

  1. if let types = Bundle.main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]] {
  2. var result = [String]()
  3. for type in types {
  4. guard let schemes = type["CFBundleURLSchemes"] as? [String] else { continue }
  5. guard let scheme = schemes.first else { continue }
  6. result.append(scheme)
  7. }
  8. print(result)
  9. }
英文:

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

  1. if let types = Bundle.main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]] {
  2. var result = [String]()
  3. for type in types {
  4. guard let schemes = type["CFBundleURLSchemes"] as? [String] else { continue }
  5. guard let scheme = schemes.first else { continue }
  6. result.append(scheme)
  7. }
  8. print(result)
  9. }

答案2

得分: 1

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

  1. /* ###################################################################################################################################### */
  2. // MARK: - 扩展可寻址实体(通用) -
  3. /* ###################################################################################################################################### */
  4. public extension LGV_MeetingSDK_AddressableEntity_Protocol {
  5. /* ################################################################## */
  6. /**
  7. 获取包的第一个URL方案,将其呈现为字符串并返回。
  8. */
  9. var urlScheme: String {
  10. guard let bundleTypes = Bundle.main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]],
  11. let myURLScheme = (bundleTypes.first?["CFBundleURLSchemes"] as? [String])?.first
  12. else { return "" }
  13. return myURLScheme
  14. }
  15. }
  16. /* ###################################################################################################################################### */
  17. // MARK: - 扩展可寻址实体(用户) -
  18. /* ###################################################################################################################################### */
  19. public extension HeartOfRecovrr_Member {
  20. /* ################################################################## */
  21. /**
  22. 返回此成员记录的可寻址URL。
  23. */
  24. var urlString: String { "\(urlScheme)://user/\(id)" }
  25. }

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

英文:

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

  1. /* ###################################################################################################################################### */
  2. // MARK: - Fleshing out the Addressable (General) -
  3. /* ###################################################################################################################################### */
  4. public extension LGV_MeetingSDK_AddressableEntity_Protocol {
  5. /* ################################################################## */
  6. /**
  7. This fetches the first URL scheme from the bundle, renders it as a String, and returns it.
  8. */
  9. var urlScheme: String {
  10. guard let bundleTypes = Bundle.main.infoDictionary?["CFBundleURLTypes"] as? [[String: Any]],
  11. let myURLScheme = (bundleTypes.first?["CFBundleURLSchemes"] as? [String])?.first
  12. else { return "" }
  13. return myURLScheme
  14. }
  15. }
  16. /* ###################################################################################################################################### */
  17. // MARK: - Fleshing out the Addressable (User) -
  18. /* ###################################################################################################################################### */
  19. public extension HeartOfRecovrr_Member {
  20. /* ################################################################## */
  21. /**
  22. This returns an addressable URL for this member record.
  23. */
  24. var urlString: String { "\(urlScheme)://user/\(id)" }
  25. }

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:

确定