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 tried datawithcontentsoffile, but I got a warning when using the url variable with it.

When I go to this file url: //SERVER/FOLDER/FOLDER/FTP/ANC.pdf on Windows, it opens the file, but on mac it is not.

I 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 the following errors:

 NSErrorFallingURLStringKey NSErrorFallingURLKey NSLocalizedDescription NSUnderlyingError 

UPDATE I can get a URL that does 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 an error: dataWithContentsoFURL

and he returned it:

 connectionError NSError * domain: @"NSCocoaErrorDomain" - code: 260 0x166d8af0 

I looked at this file via Get Info and it starts as follows smb://server/folder/folder/ANC/ANC.pdf

+5
source share
6 answers

NSData and URLs: dragons exist

+[NSData dataWithContentsOfURL:] can return nil for any reason. If something goes wrong when you try to use this URL, this method will return nil . For example, the file may not exist, the network connection may be disconnected, or the URL may even be malformed. nil returns and the application does not know why.

+ [NSData dataWithContentsOfURL:options:error:] , on the other hand, will tell the caller what went wrong. When nil returned, the error argument will be populated with an object that describes the problem that has occurred. Using this method will directly answer the question "why."

Both of these methods are synchronous and their use for working with files, network resources and especially files served from a network resource is not recommended. These methods block the caller and are not really intended for such uses. It is better to use an input stream or NSURLSession .

SMB is not supported

From your question, although it seems that you are trying to access a file that exists on the SMB share. Unfortunately, iOS does not support SMB - even if you have a correctly formatted smb URL (for example, smb: //servername/sharename/filename.pdf), you cannot access it without using a third-party SMB implementation.

Using FTP instead of SMB should work.

+7
source

You just need to make a small change, in fact your pdf file is located on the server, so you need to change your code as follows.

 NSURL *url = [NSURL URLWithString:stringURL]; 

I tried this and worked great.

If your file is in the main application bundle, you can use fileURLWithPath .

0
source

If you have a valid Windows file path with backslash separators, there is a CoreFoundation way to convert it to a CFURL / NSURL

 NSString *stringURL = @"\\\\SERVER\\FOLDER\\FOLDER\\FTP\\ANC\\ANC.pdf"; NSURL *url = (NSURL *)CFBridgingRelease(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, stringURL, kCFURLWindowsPathStyle, false)); 

If the line starts with file:// , this is the Mac URL string using the file scheme, and you can use

 NSString *fileURLString = @"file:///Applications"; NSURL *url = [NSURL URLWithString:fileURLString]; 

Note that the line of the correct file scheme must have a third slash for the root folder after two slashes for the scheme.

The fileURLWithPath method can only be used if the line begins with a single slash, it implicitly adds the file:// scheme.

 NSString *pathString = "/Applications"; NSURL *url = [NSURL fileURLWithPath: pathString]; 
0
source

Make sure the URL contains http:// OR https:// . For example: http://SERVER/FOLDER/FILE.pdf

0
source

Two things to keep in mind when accessing a file in an iOS app:

  • If you are accessing a file from the IOS application sandbox (a directory of documents or caches), use this:

     NSURL *url = [NSURL fileURLWithPath:strUrl]; 
  • If you access the file from any server in an iOS application, use this:

     NSURL *url = [NSURL URLWithString:strUrl]; 

NOTE. You cannot access the file from your Mac in the iOS application while working with the simulator.

Hope this helps!

0
source

please check the code with this code

  -(void)downloadPdfBtnPressed:(id)sender { NSString *dowloadURL = [DownloadUrlArray objectAtIndex:[sender tag ]]; NSString *filename =[dowloadURL lastPathComponent]; NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/Download"]; if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath]) [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:nil]; NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:dowloadURL]]; if(pdfData) { stringPath = [stringPath stringByAppendingPathComponent:filename]; [pdfData writeToFile:stringPath atomically:YES]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[singletonObj getTranslationForKey:@"AlereTitleContactus"] message:@"PDF file is downloaded" delegate:nil cancelButtonTitle:[singletonObj getTranslationForKey:@"alertOK"] otherButtonTitles:nil]; [alert show]; } } 
-1
source

All Articles