Question

I've got an NSData object, that I am placing inside a dictionary. I am then writing the dictionary to the documents directory. Then I'm immediately making the URL to the file available to the uiactivityviewcontroller so that it is included for attachment in the Mail app.

The problem is that when the user clicks on this file for import into the app, everything in the dictionary is available (there are other NSString objects) except the NSData object! When I test to see what is inside the object for the relevant key that should hold the NSData object, it returns (null)..

Here are the relevant bits of the code (I've simplified some of it for clarity, but it's essentially the same code):

    NSDictionary *dictionaryItems = @{@"stringobj":        @"string",
                                @"dataobj":       dataobject};

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSURL *dictUrl = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:@"myinfo.myappinfo"]]; //my app will open this file type
    [dictionaryItems writeToURL:dictURL atomically:YES];

// I will include the dictURL in the activityitems array so it can be attached to the the email when the uiacitivityitemcontroller is presented

When the app is opened, I simply load the dictionary from the file, and attempt to retrieve the nsdata object from the "dataobj" key.. but it returns null.

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if (url !=nil && [url isFileURL]) {
        self.importedDictionary = [NSDictionary dictionaryWithContentsOfURL:url];
        NSLog("%@",[self.importedDictionary objectForKey:@"dataobj"]); //returns null.. why?
    }
}

I'm not sure why the NSData object I placed in the dictionary isn't there - any thoughts? Any help appreciated!

Was it helpful?

Solution

Thanks just solved it with the help of the comments to my question.

The problem was that on some occasions the file was not writing to disk due to nil values.

If there are any keys in the dictionary that refer to nil items then the dictionary will not write to file. Resolved the issue by checking if each object that I want to include in the dictionary is nil or not, and if it is nil then I omit that from the dictionary to ensure it writes to file.

For example

NSDictionary *dictionaryItems = {@"key1":nil,@"key2":@"string object"};
BOOL didItWrite = [dictionaryItems writeToURL:dictURL atomically:YES];

//didItWrite will return NO in this case since we have a nil object in the dictionary

NSDictionary *dictionaryItems = {@"key1":dataobj,@"key2":@"string object"};
BOOL didItWrite = [dictionaryItems writeToURL:dictURL atomically:YES];

//didItWrite will return YES here since there are no objects that evaluate to nil in the dictionary.

Quoting @GuyKogus in the question comments:

The reason why that happens is that you're really [NSDictionary dictionaryWithObjectsAndKeys:]. You'll find in the documentation that it takes as a parameter a null-terminated list of alternating values and keys. So the moment a value in your list is nil, it ignores the rest of the parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top