Obtain bundle identifier programmatically in Swift?

前端 未结 3 1178
旧时难觅i
旧时难觅i 2020-12-30 18:11

How can I get the bundle ID in Swift?

Objective-C version:

NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
相关标签:
3条回答
  • 2020-12-30 18:44

    It's pretty much the same thing in Swift except the class and method names have been shortened:

    let bundleIdentifier = Bundle.main.bundleIdentifier // return type is String?
    
    0 讨论(0)
  • 2020-12-30 18:49

    If you are trying to get it programmatically , you can use below line of code :

    Objective-C:

    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    

    Swift 3.0:

    let bundleIdentifier =  Bundle.main.bundleIdentifier
    

    Updated for latest swift It will work for both iOS and Mac apps.

    For More Info, Check here :

    Apple Docs: https://developer.apple.com/documentation/foundation/bundle#//apple_ref/occ/instm/NSBundle/bundleIdentifier

    0 讨论(0)
  • 2020-12-30 19:02

    Try this:

    let bundleID = NSBundle.mainBundle().bundleIdentifier
    

    Swift 3+:

    let bundleID = Bundle.main.bundleIdentifier
    
    0 讨论(0)
提交回复
热议问题