1

I have an app that is using the ABAdressBook API & NSFileManager.

When i'm running the application from the command line - the API doesn't seems to retrieve the AddressBook/NSFileManager information - but when i run it from the springboard - it does show me the information required.

Is it because my app doesn't have a UI when it run from the command line?

int main(int argc, char *argv[]) 
{
    @autoreleasepool 
    {
        printf("HELLO TEST\n");       
        NSLog(@"---------");
        NSMutableArray* dbList = [[NSMutableArray alloc] init];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSLog(@"file manager address is %@",fileManager);
        NSDirectoryEnumerator *dirnum = [fileManager enumeratorAtPath: @"/private/"];
        NSLog(@"dirNum is %@",dirnum);
        NSString *nextItem = [NSString string];

        while( (nextItem = [dirnum nextObject])) {
            if ([[nextItem pathExtension] isEqualToString: @"db"] ||
                [[nextItem pathExtension] isEqualToString: @"sqlitedb"]) {
                if ([fileManager isReadableFileAtPath:nextItem]) {
                    [dbList addObject:nextItem];

                    NSLog(@"%@", nextItem);
                }
            }
        }
    }
}

Edit: I pinned the issue down to the following selector: [fileManager isReadableFileAtPath:nextItem] when i run this function from the SpringBoard - it returns true - for several of the files...

when i run it in the command line - it return false to all of the files - although i'm running the app with root privileges.

any idea what can cause this change in behavior?

Thanks, Itay

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Itay Levin
  • 1,579
  • 2
  • 16
  • 23

1 Answers1

1

Both ABAddressBook and NSFileManager have a class method that returns a unique, shared object – in your code above, the call to [NSFileManager defaultManager] retrieves "the default NSFileManager object for the file system", according to the docs. When you run your app from the command line, you're probably not executing some or all of the code that initializes the shared object; you may even be getting back nil from the class method.

If so, you may be able to work around this problem by creating your own object (that is, by using [[NSFileManager alloc] init] or [ABAddressBook addressBook]) rather than relying on the class method to return a shared object.

Scott Forbes
  • 7,397
  • 1
  • 26
  • 39
  • Will the [ABAddressBook addressBook] return me the address book of the device? filled with the address book contacts? or it will be an empty one? – Itay Levin Dec 24 '11 at 12:38
  • i can't seem to call [ABAddressBook addressBook] the compiler doesn't find this function. – Itay Levin Dec 25 '11 at 06:25
  • `[ABAddressBook addressBook]` is a class method, which means that you call it from the class itself – that is, your code should use the exact literal string `[ABAddressBook addressBook]`. `[myInstanceVariable addressBook]` doesn't work. The documentation is a little fuzzy on whether the address book returned by `[ABAddressBook addressBook]` can be used to search for existing records – it does say that if you write to that address book and then save, the results will show up in the user's address book. – Scott Forbes Dec 25 '11 at 09:12
  • didn't managed to compile it. (gone the other road for now..accessing the db directly) – Itay Levin Dec 29 '11 at 08:46