Come posso ottenere una stringa dell'identificatore del pacchetto a livello di codice all'interno della mia app?
Come posso ottenere una stringa dell'identificatore del pacchetto a livello di codice all'interno della mia app?
Risposte:
Objective-C
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
Swift 1.2
let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier
Swift 3.0
let bundleIdentifier = Bundle.main.bundleIdentifier
Xamarin.iOS
var bundleIdentifier = NSBundle.MainBundle.BundleIdentifier
let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier
Bundle.main.bundleIdentifier!
[[NSBundle mainBundle] bundleIdentifier];
( documentazione )
let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier
Potrebbe essere necessario l'approccio Core Foundation per ottenere il valore. L'esempio ARC è il seguente:
NSString *value = (__bridge_transfer NSString *)CFDictionaryGetValue(CFBundleGetInfoDictionary(CFBundleGetMainBundle()),
(const void *)(@"CFBundleIdentifier"));
Uso queste macro per renderlo 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
}
Se stai cercando di ottenerlo a livello di codice, puoi utilizzare la riga di codice seguente:
Objective-C:
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
Swift 3.0:
let bundleIdentifier = Bundle.main.bundleIdentifier
Aggiornato per l'ultimo rapido Funzionerà per le app iOS e Mac.