In your first screenshot, the debugger is still on the line that declares and assigns the fileData
variable. This means that that line hasn't actually been executed yet. -dataWithContentsOfFile:
hasn't yet been called, and thus the value that appears to be in fileData
is not meaningful; what you're seeing is just garbage data prior to the variable actually being assigned. In your second screenshot, the -dataWithContentsOfFile:
method has finished running, and it has returned nil
. What you need to do is to figure out why you're getting nil
from -dataWithContentsOfFile:
. Perhaps the path to the file is incorrect, or perhaps you don't have permission to read it, or perhaps you have a sandboxing issue.
I would suggest using -dataWithContentsOfURL:options:error:
instead of -dataWithContentsOfFile:
. This will return an error by reference (create an NSError
variable ahead of time, assign it to nil
, pass a pointer to the error as the third parameter to -dataWithContentsOfURL:options:error:
, and then check the error if the method returns nil
). More likely than not, the contents of the error will explain what went wrong when trying to read the file.
EDIT: Looking at your screenshot again, the problem is clear; from the description of the contents of attachmentPath
, we can see that it isn't a path at all, but instead it contains a URL string (with scheme file:
). So you cannot pass it to the APIs that use paths. This is okay, since the URL-based mechanisms are what Apple recommends using anyway. So, just turn it into a URL by passing the string to -[NSURL URLWithString:]
(or, even better, -[[NSURLComponents componentsWithString:] URL]
, since it conforms to a newer RFC). So, something like:
// Get the URL string, which is *not* a path
NSString *attachmentURLString = [RCTConvert NSString:options[@"attachment"][@"path"]];
// Create a URL from the string
NSURL *attachmentURL = [[NSURLComponents componentsWithString:attachmentURLString] URL];
...
// Initialize a nil NSError
NSError *error = nil;
// Pass a pointer to the error
NSData *fileData = [NSData dataWithContentsOfURL:attachmentURL options:0 error:&error];
if (fileData == nil) {
// 'error' should now contain a non-nil value.
// Use this information to handle the error somehow
}