How to save filename with space on Iphone - iphone

I am trying to save file in document directory using
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filename=[NSString stringWithFormat:#"abc 1.pdf",];
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:filename];
I am opening this file using a web view
NSURL *url = [NSURL URLWithString:pdfLocation];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:urlRequest];
[urlRequest release];
This fails if the the file name has a space.How should I save file so that space in the filename is maintained.

UIWebView expects a file URL in this case. Change the line to:
NSURL *url = [NSURL fileURLWithPath:pdfLocation];

Related

Display Local HTML file from documents directory in a UIWebView on iPhone

How can I display a HTML file from the Documents directory in my app?
Below is the code that I'm using and nothing is showing up in my UIWebView
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"Notes.html"];
NSURL *url = [NSURL URLWithString:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[notesListWeb loadRequest:request];
Why is this not working out?
I've found other answers but they aren't from the documents directory.
Your help is greatly appreciated.
The proper way to create an NSURL object with a file path is:
NSURL *url = [NSURL fileURLWithPath:filePath];
You should also change how you create your filePath:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Notes.html"];
Also keep in mind that filenames are case sensitive on a real iOS device. Is the file really named Notes.html and notes.html?

Objective-c: Downloading a PDF file

I am trying to eneble a button to download a pdf file from a server.
(Onto the divice)
I'm not being successful with this code (to post this question, i've changed the address)
Would you give me some advice to make it work..?
Thanks in advance.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.mydomain.com/mypdffile.pdf"]];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"<Application_Home>/Documents/"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];
You are not building a correct NSURL object to reach a server. fileURLWithPath: is a method to build URL that points to files in the file system.
Use URLWithString: writing the complete url, that is for example "http://myserver.com/myfile.pdf"

How to fetch pdf file in Controller file in iphone

I have to read pdf file from my controller. How to load it ?
Thanks
You could use UIWebView to view the pdf file.
Check the below SO post.
Display pdf from the weburl iphone sdk
Tutorial link1
From apple
Using UIWebView to display select document types
Here's how you can read and store your pdf locally:
First create UIWebView and include <<UIWebViewDelegate>>
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
Or, if you simply want to read/load pdf from your Resource folder then simply do this :
NSString* filePath= [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"pdf"]];
/// or you can even read docs file as : pathForResource:#"sample" ofType:#"docx"]
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];

iOS download and save HTML file

I am trying to download a webpage (html) then display the local html that has been download in a UIWebView.
This is what I have tried -
NSString *stringURL = #"url to file";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"index.html"];
[urlData writeToFile:filePath atomically:YES];
}
//Load the request in the UIWebView.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]]; // Do any additional setup after loading the view from its nib.
But this gives a 'SIGABRT' error.
Not too sure what I have done wrong?
Any help would be appreciated. Thanks!
The path passed to the UIWebView is incorrect, like Freerunnering mentioned, try this instead:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.google.nl"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
Note: Correct error-handling needs to be added.
NSDocumentDirectory will be backed up to iCloud. You might be better off using NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html
Your app is a folder with a .app extension on the end. On the iPhone once it is installed you can't change this folder, hence why you save it to the documents directory.
In your example code you save the file to Documents/index.html and ask it to load appname.app/index.html.
[NSBundle mainBundle] doesn't give you the documents directory it gives you (i think) the .app folder (though it might be the folder that contains the app, documents, and other folders).
To give you'll want to change this line.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
To (providing this is the same method as the rest of the code, else recreate the object 'filePath')
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath isDirectory:NO]]];
Here is the Swift 2.x version:
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var filePath: String = "\(paths[0])/\("index.html")"
// Download and write to file
var url: NSURL = NSURL(string: "http://www.google.nl")!
var urlData: NSData = NSData.dataWithContentsOfURL(url)
urlData.writeToFile(filePath, atomically: true)
// Load file in UIWebView
web.loadRequest(NSURLRequest(URL: NSURL.fileURLWithPath(filePath)))

How to create iPhone application can download content

Now I try to build iPhone application, like magazine online, that can download new magazine form web and store in app (can play offline).
I no idea for implement feature download new magazine form online source and store in the app.
Please help me.
Here's an example how you can download a file to your iPhone and reuse it later (in this case it's stores an HTML document that is then loaded to UIWebView from local store).
// downloading file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *dataPath = [paths objectAtIndex:0];
NSString *fileName = [[url path] lastPathComponent];
NSString *filePath = [dataPath stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO) {
NSString *fileString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
[fileString writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
}
// use local file
NSURL *loadUrl = [[NSURL alloc] initFileURLWithPath: filePath];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: loadUrl];
[webView loadRequest: request];
[request release];
[loadUrl release];