skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search