Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C dataWithContentsOfURL returns nill

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

like image 578
user979331 Avatar asked Sep 15 '15 14:09

user979331


1 Answers

NSData and URLs: There be dragons

+[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.

SMB Not Supported

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.

like image 62
quellish Avatar answered Nov 05 '22 13:11

quellish