Obtain bundle identifier programmatically in Swift?

IosSwiftIos8Bundle Identifier

Ios Problem Overview


How can I get the bundle ID in Swift?

Objective-C version:

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

Ios Solutions


Solution 1 - Ios

Try this:

let bundleID = NSBundle.mainBundle().bundleIdentifier

Swift 3+:

let bundleID = Bundle.main.bundleIdentifier

Solution 2 - Ios

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?

Solution 3 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUserView Question on Stackoverflow
Solution 1 - IosSebastianView Answer on Stackoverflow
Solution 2 - Iosuser887210View Answer on Stackoverflow
Solution 3 - IosAshView Answer on Stackoverflow