Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-04-2009, 01:49 PM   #3 (permalink)
Shmoopi
Shmoopi LLC
 
Shmoopi's Avatar
 
Join Date: Jun 2009
Location: Virginia
Posts: 203
Default

Quote:
Originally Posted by Bovn View Post
The first one is not possible; all applications are installed in their own sandbox, meaning that they cannot do anything outside their own application. This is true for all scenarios, but certain things are allowed by Apple, such as retrieving the users image folder etc. But I'm not sure if it works when it comes to music. I do not think Apple are keen to have compeditors when it comes to their iPod.

However, I'm not 100% sure about the first one, although it would surprise me a great deal if it was possible.
That's wrong, their is a way to check if an application is installed or not. Sometimes you may want to check if a specific app is installed on the device, in case you use custom URL schemes that require some other app to be installed (you could just gray out/disable some buttons then). Unfortunately, Apple apparently does not have any function that checks this for you, so I whipped one up. It does not enumerate every single app, instead it uses the MobileInstallation cache which is always up-to-date with SpringBoard and holds the Info dictionaries of all apps installed. Although you're not "supposed" to access the cache, it's readable my App Store apps. Here is my code which at least works perfectly fine with the Simulator:
Code:
// Declaration
BOOL APCheckIfAppInstalled(NSString *bundleIdentifier); // Bundle identifier (eg. com.apple.mobilesafari) used to track apps

// Implementation

BOOL APCheckIfAppInstalled(NSString *bundleIdentifier)
{
	static NSString *const cacheFileName = @"com.apple.mobile.installation.plist";
	NSString *relativeCachePath = [[@"Library" stringByAppendingPathComponent: @"Caches"] stringByAppendingPathComponent: cacheFileName];
	NSDictionary *cacheDict = nil;
	NSString *path = nil;
	// Loop through all possible paths the cache could be in
	for (short i = 0; 1; i++)
	{
	
		switch (i) {
	case 0: // Jailbroken apps will find the cache here; their home directory is /var/mobile
		path = [NSHomeDirectory() stringByAppendingPathComponent: relativeCachePath];
		break;
	case 1: // App Store apps and Simulator will find the cache here; home (/var/mobile/) is 2 directories above sandbox folder
		path = [[NSHomeDirectory() stringByAppendingPathComponent: @"../.."] stringByAppendingPathComponent: relativeCachePath];
		break;
	case 2: // If the app is anywhere else, default to hardcoded /var/mobile/
		path = [@"/var/mobile" stringByAppendingPathComponent: relativeCachePath];
		break;
	default: // Cache not found (loop not broken)
		return NO;
		break; }
		
		BOOL isDir = NO;
		if ([[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] && !isDir) // Ensure that file exists
			cacheDict = [NSDictionary dictionaryWithContentsOfFile: path];
		
		if (cacheDict) // If cache is loaded, then break the loop. If the loop is not "broken," it will return NO later (default: case)
			break;
	}
	
	NSDictionary *system = [cacheDict objectForKey: @"System"]; // First check all system (jailbroken) apps
	if ([system objectForKey: bundleIdentifier]) return YES;
	NSDictionary *user = [cacheDict objectForKey: @"User"]; // Then all the user (App Store /var/mobile/Applications) apps
	if ([user objectForKey: bundleIdentifier]) return YES;
	
	// If nothing returned YES already, we'll return NO now
	return NO;
}
Here is an example of this, assuming that your app is named "yourselfmadeapp" and is an app in the app store.
Code:
NSArray *bundles2Check = [NSArray arrayWithObjects: @"com.apple.mobilesafari", @"com.yourcompany.yourselfmadeapp", @"com.blahblah.nonexistent", nil];
for (NSString *identifier in bundles2Check)
	if (APCheckIfAppInstalled(identifier))
		NSLog(@"App installed: %@", identifier);
	else
		NSLog(@"App not installed: %@", identifier);
Log Output:
Code:
2009-01-30 12:19:20.250 SomeApp[266:20b] App installed: com.apple.mobilesafari
2009-01-30 12:19:20.254 SomeApp[266:20b] App installed: com.yourcompany.yourselfmadeapp
2009-01-30 12:19:20.260 SomeApp[266:20b] App not installed: com.blahblah.nonexistent
Your welcome
Trackback for this code goes to:
Code Sample: Check if an app is installed - iDevKit
Shmoopi is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,836
Threads: 89,209
Posts: 380,644
Top Poster: BrianSlick (7,129)
Welcome to our newest member, abbylowin
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:46 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.