Skip to content

Commit cab5a31

Browse files
urklcshazron
authored andcommitted
“exists” command added to check if an app with given bundle id is
installed on device or not (closes #107)
1 parent 56abf4e commit cab5a31

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

ios-deploy.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,40 @@ void list_files(AMDeviceRef device)
12741274
}
12751275
}
12761276

1277+
int app_exists(AMDeviceRef device)
1278+
{
1279+
if (bundle_id == NULL) {
1280+
printf("Bundle id is not specified\n");
1281+
return false;
1282+
}
1283+
1284+
AMDeviceConnect(device);
1285+
assert(AMDeviceIsPaired(device));
1286+
assert(AMDeviceValidatePairing(device) == 0);
1287+
assert(AMDeviceStartSession(device) == 0);
1288+
1289+
CFStringRef cf_bundle_id = CFStringCreateWithCString(NULL, bundle_id, kCFStringEncodingASCII);
1290+
1291+
NSArray *a = [NSArray arrayWithObjects:@"CFBundleIdentifier", nil];
1292+
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:a forKey:@"ReturnAttributes"];
1293+
CFDictionaryRef options = (CFDictionaryRef)optionsDict;
1294+
1295+
CFDictionaryRef result = nil;
1296+
afc_error_t resultStatus = AMDeviceLookupApplications(device, options, &result);
1297+
assert(resultStatus == 0);
1298+
1299+
CFDictionaryRef app_dict = CFDictionaryGetValue(result, cf_bundle_id);
1300+
1301+
int appExists = (app_dict == NULL) ? -1 : 0;
1302+
1303+
CFRelease(cf_bundle_id);
1304+
1305+
assert(AMDeviceStopSession(device) == 0);
1306+
assert(AMDeviceDisconnect(device) == 0);
1307+
1308+
return appExists;
1309+
}
1310+
12771311
void copy_file_callback(afc_connection* afc_conn_p, const char *name,int file)
12781312
{
12791313
const char *local_name=name;
@@ -1456,6 +1490,8 @@ void handle_device(AMDeviceRef device) {
14561490
upload_file(device);
14571491
} else if (strcmp("download", command) == 0) {
14581492
download_tree(device);
1493+
} else if (strcmp("exists", command) == 0) {
1494+
exit(app_exists(device));
14591495
}
14601496
exit(0);
14611497
}
@@ -1636,7 +1672,8 @@ void usage(const char* app) {
16361672
" -o, --upload <file> upload file\n"
16371673
" -w, --download download app tree\n"
16381674
" -2, --to <target pathname> use together with up/download file/tree. specify target\n"
1639-
" -V, --version print the executable version \n",
1675+
" -V, --version print the executable version \n"
1676+
" -e, --exists check if the app with given bundle_id is installed or not \n",
16401677
app);
16411678
}
16421679

@@ -1666,11 +1703,12 @@ int main(int argc, char *argv[]) {
16661703
{ "upload", required_argument, NULL, 'o'},
16671704
{ "download", optional_argument, NULL, 'w'},
16681705
{ "to", required_argument, NULL, '2'},
1706+
{ "exists", no_argument, NULL, 'e'},
16691707
{ NULL, 0, NULL, 0 },
16701708
};
16711709
char ch;
16721710

1673-
while ((ch = getopt_long(argc, argv, "VmcdvunrILi:b:a:t:g:x:p:1:2:o:l::w::", longopts, NULL)) != -1)
1711+
while ((ch = getopt_long(argc, argv, "VmcdvunrILei:b:a:t:g:x:p:1:2:o:l::w::", longopts, NULL)) != -1)
16741712
{
16751713
switch (ch) {
16761714
case 'm':
@@ -1742,6 +1780,10 @@ int main(int argc, char *argv[]) {
17421780
command = "download";
17431781
list_root = optarg;
17441782
break;
1783+
case 'e':
1784+
command_only = true;
1785+
command = "exists";
1786+
break;
17451787
default:
17461788
usage(argv[0]);
17471789
return exitcode_error;

0 commit comments

Comments
 (0)