I have this code here:
NSString *stringURL = @"\\\\SERVER\\FOLDER\\FOLDER\\FTP\\ANC\\ANC.pdf";
NSURL *url = [NSURL fileURLWithPath:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
but urlData returned nill
I have tried datawithcontentsoffile but I got a warning when using the url variable with it.
When I goto this url file://SERVER/FOLDER/FOLDER/FTP/ANC.pdf in Windows, it opens the file, but on mac it does not not.
I have also tried the following:
NSString *stringURL = @"file://SERVER/FOLDER/FOLDER/FTP/ANC.pdf";
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:stringURL]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
}] resume];
// NSURL *url = [NSURL URLWithString:stringURL];
// NSData *urlData = [NSData dataWithContentsOfURL:url];
but get these errors:
NSErrorFallingURLStringKey
NSErrorFallingURLKey
NSLocalizedDescription
NSUnderlyingError
UPDATE I am able to get url not return nill with the following:
NSURL *url = [NSURL fileURLWithPath:@"file:///server/FOLDER/FOLDER/FTP/ANC/ANC.pdf"];
however, this returns nill:
NSData *urlData = [NSData dataWithContentsOfURL:url];
I added the error: to dataWithContentsofURL
and it returned this:
connectionError NSError * domain: @"NSCocoaErrorDomain" - code: 260 0x166d8af0
I looked at the file in question via Get Info and it starts out like this smb://server/folder/folder/ANC/ANC.pdf
+[NSData dataWithContentsOfURL:]
can return nil
for any number of reasons. If anything goes wrong when attempting to use that URL this method will return nil
. For example the file may not exist, the network connection may time out, or the URL may even be malformed. nil
is returned and the application has no idea why.
+ [NSData dataWithContentsOfURL:options:error:]
on the other hand, will tell the caller what went wrong. When nil
is returned the error
argument will be populated with an object that describes the problem that occured. Using this method would directly answer the question of "why".
Both of these are synchronous methods and their use for working with files, network resources, and especially files served from a network resource is discouraged. These methods will block the caller and are not really intended for these kinds of uses. It's better to use an input stream or NSURLSession
instead.
From your question though it seems you are trying to access a file that exists on an SMB share. Unforunately iOS does not support SMB - even if you had a correctly formatted smb URL (i.e. smb://servername/sharename/filename.pdf) you would be unable to access it without using a third party SMB implementation.
Using FTP in place of SMB, however, should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With