Search code examples
iosswiftframeworksbundle-identifier

Access Bundle Identifier of App in Custom Framework


I am designing a sdk where I am making some url call at some point of time. For this I want the context of the app which is making a call. As a context I want to pass the bundle identifier of the app using that sdk.

SDK is in form of library which some third party app will import and use. I have the reference of UIViewController which is invoking the url call in third party app. I have no idea if having only UIViewController reference is enough or not.

So how can I get access to bundle identifier of their app?

Thanks


Solution

  • It's quite easy.

    Each type (class or struct) knows which bundle it is associated with.

    Step 1# Get the type of any object inside the framework.

    Step 2# Get the bundle of that type. And access its id.

    // VC is ref to the class object of the main app
    
    // # 1 -> This will give the class type of the object
    let objectType = type(of: vc) 
    
    // # 2 -> This will get you the bundle of the main app
    let bundle = Bundle(for: objectType.self)
    
    // This will finally give you the bundle id
    let bundleId = bundle.bundleIdentifier