简体   繁体   中英

Getting the name of an app from a Bundle ID other than my own on iOS8

I'm trying to use this code:

NSString *appName = [[NSBundle bundleWithIdentifier:item] objectForInfoDictionaryKey:@"CFBundleExecutable"];

To get the name of an app from its Bundle Identifier, however for all non-stock apps this method is returning nil. I have tried to look for ways to solve this to no avail. How can I get the name of an app from the bundle ID in iOS 8?

EDIT: To clarify, I want to use this for apps other than my own - I asked a question earlier on the same topic but all of the answers were for my app only, so I have explained the question further.

You can't access the bundle of another app. You have only access to your app's bundle. When you use bundleWithIdentifier: the iOS looks for a bundle that matches the provided identifier within your app's sandbox. In your above mentioned case, there will be no such NSBundle available, so it'll return nil.

According to NSBundle Class Reference

bundleWithIdentifier:

Returns the previously created NSBundle instance that has the specified bundle identifier.

Declaration

Swift

init?(identifier identifier: String) -> NSBundle

Objective-C

+ (NSBundle *)bundleWithIdentifier:(NSString *)identifier

Parameters

identifier

The identifier for an existing NSBundle instance.

Return Value

The previously created NSBundle instance that has the bundle identifier identifier. Returns nil if the requested bundle is not found.

Discussion

This method is typically used by frameworks and plug-ins to locate their own bundle at runtime. This method may be somewhat more efficient than trying to locate the bundle using the bundleForClass: method. However, if the initial lookup of an already loaded and cached bundle with the specified identifier fails, this method uses potentially time-consuming heuristics to attempt to locate the bundle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM