2012-01-16 17 views

risposta

374

Objective-C

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

Swift 1,2

let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier 

Swift 3,0

let bundleIdentifier = Bundle.main.bundleIdentifier 
+9

Questa risposta non è limitata a iOS. Funziona anche per le app Mac. – Jonny

+7

In Swift, usare 'let bundleIdentifier = NSBundle.mainBundle(). BundleIdentifier' –

+1

(è possibile eliminare questo commento) ma amo la sensazione di leggere la risposta quindi nel commento, vedere qualcosa come @Jonny s e Tim (anche se tu può vedere questo in un'altra intera risposta), si collega a qualcos'altro che è ancora rilevante e utile. Grazie per i fantastici ragazzi della community. – haxpor

45
[[NSBundle mainBundle] bundleIdentifier]; 

(documentation)

+2

+1 per collegamento documentazione. –

+0

In Swift, usare 'let bundleIdentifier = NSBundle.mainBundle(). BundleIdentifier' –

1

Potrebbe essere necessario un approccio Core Foundation per ottenere il valore. ARC esempio è la seguente:

NSString *value = (__bridge_transfer NSString *)CFDictionaryGetValue(CFBundleGetInfoDictionary(CFBundleGetMainBundle()), 
                    (const void *)(@"CFBundleIdentifier")); 
0

f si sta cercando di ottenere a livello di codice, è possibile utilizzare sotto la linea di codice:

Objective-C:

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

Swift 3.0:

let bundleIdentifier = Bundle.main.bundleIdentifier

Aggiornato per ultimo swift Sarà lavoro per entrambe le app iOS e Mac.

0

Io uso queste macro per rendere molto più breve:

#define BUNDLEID [NSString stringWithString:[[NSBundle mainBundle] bundleIdentifier]] 

#define BUNDLEIDEQUALS(bundleIdString) [BUNDLEID isEqualToString:bundleIdString] 

quindi posso solo confrontare in questo modo:

if (BUNDLEIDEQUALS(@"com.mycompany.myapp") { 
    //do this 
} 
1

Per ottenere l'identificatore fascio di programmazione in Swift 3.0:

Swift 3.0

let bundle = Bundle.main.bundleIdentifier 
Problemi correlati