Reputation: 11871
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
Upvotes: 5
Views: 2137
Reputation: 1650
Two things to keep in mind while accessing a file in iOS application:
If you are accessing a file from iOS application sandbox(document or cache directory) then use this:
NSURL *url = [NSURL fileURLWithPath:strUrl];
If you are accessing a file from any server in iOS application then use this:
NSURL *url = [NSURL URLWithString:strUrl];
NOTE: You cannot access a file from your mac in iOS application when running from simulator.
Hope this helps!
Upvotes: 0
Reputation: 9017
Make sure URL contains http://
OR https://
.
For example: http://SERVER/FOLDER/FILE.pdf
Upvotes: 0
Reputation: 489
please check your 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];
}
}
Upvotes: -1
Reputation: 21254
+[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.
Upvotes: 7
Reputation: 285072
If you have a valid windows file path with backslash separators, there is a CoreFoundation
method to convert it to a CFURL
/NSURL
object
NSString *stringURL = @"\\\\SERVER\\FOLDER\\FOLDER\\FTP\\ANC\\ANC.pdf";
NSURL *url = (NSURL *)CFBridgingRelease(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, stringURL, kCFURLWindowsPathStyle, false));
If the string starts with file://
it's a Mac URL string with the file
scheme and you can use
NSString *fileURLString = @"file:///Applications";
NSURL *url = [NSURL URLWithString:fileURLString];
Consider that a valid file scheme string must have a third slash for the root folder after the two slashes for the scheme.
The method fileURLWithPath
can only be used if the string starts with a single slash, it adds the file://
scheme implicitly.
NSString *pathString = "/Applications";
NSURL *url = [NSURL fileURLWithPath: pathString];
Upvotes: 0
Reputation: 1181
Just need to do some minor change, Actually your pdf file is on server so you have to change your code like this.
NSURL *url = [NSURL URLWithString:stringURL];
I tried this and its working fine.
If your file is in application's main bundle than you can use fileURLWithPath
.
Upvotes: 0