NSURL unused variable - pdf

I have generated a PDF file within my application which is saved to the Documents directory. I am trying to fetch the pdf to display within a UIWebView but i have an unused NSURL variable. Could this be the reason why the file is not loading properly? Here is my code which is attached to an IBAction button
The only way to get rid of that warning is to NSLog that NSURL. But I want to display that saved file.
This is the code I am using.
-(IBAction)pdf:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#"client.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request];
NSLog(#"%#", webURL);
}

I figured it out. I needed to change my filePath NSString from stringByAppendingPathExtension to stringByAppendingPathcomponent. I can now view my PDF file in my UIWebView.

Related

How to save and retrieve files to apps temp folder in ios

I'm new to IOS development. I'm developing an app which involves downloading files and saving that to apps temp folder and I dont know how to do that my current code is given below
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSLog(#"%#",tmpDirURL);
NSString *myString = [tmpDirURL absoluteString];
for(int i=0;i<responseArray.count;i++){
ASIHTTPRequest *saveUrl = [ASIHTTPRequest requestWithURL:responseArray[i]];
[saveUrl setDownloadDestinationPath:myString];
[request startSynchronous];
}
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myString error:&error];
NSLog(#"%#",directoryContents);
The response array contain a list of URL for downloading files. I know something wrong with my code but I cant find out that error please help me to solve this problem
I found the solution'
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
[data writeToFile:htmlFilePath atomically:YES];
When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location. If downloadDestinationPath is not set, download data will be stored in memory
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSLog(#"%#",tmpDirURL);
NSString *myString = [tmpDirURL absoluteString];
for(int i=0;i<responseArray.count;i++){
ASIHTTPRequest *saveUrl = [ASIHTTPRequest requestWithURL:responseArray[i]];
[saveUrl setDownloadDestinationPath:[myString stringByAppendingPathComponent:[NSString stringWithFormat:#"%i",i ]]; // for each file set a new location inside tmp
[request startSynchronous];
}
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myString error:&error];
NSLog(#"%#",directoryContents);

How to refresh the HTML file which is in NSDocumentDirectory? (more than one time)

I am using HTML file to generate pdf file in DocumentDirectory. Every time I need to refresh
(with new image which is having same name in html file) HTML file when below method is calling.
Right now i am doing like this, but it is refreshing for only first time(when ever i am calling this method)
-(void)RefreshingHTML
{
// fetching path
NSArray* deletepath_forDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString* deletedocumentsDirectoryfiles = [deletepath_forDirectory objectAtIndex:0];`
// Delete HTML file in DocumentDirectory
NSString *deleteHTMLPath = [deletedocumentsDirectoryfiles stringByAppendingPathComponent:#"HTML_Demo.html"];
NSString *deletePDFPath = [deletedocumentsDirectoryfiles stringByAppendingPathComponent:#"HTML_Demo.pdf"];
NSLog(#"delete HTML file Path : %#",deleteHTMLPath);
if([[NSFileManager defaultManager] fileExistsAtPath:deleteHTMLPath]) {
[[NSFileManager defaultManager] removeItemAtPath:deleteHTMLPath error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:deletePDFPath error:NULL];
}
// fetching HTML file from supporting files
NSString *path = [[NSBundle mainBundle] pathForResource:#"HTML_Demo" ofType:#"html"];
NSURL *pathURL = [NSURL fileURLWithPath:path];
NSArray* path_forDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString* documentsDirectory = [path_forDirectory objectAtIndex:0];
// saving HTML file to DocumentDirectory
NSData* data = [NSData dataWithContentsOfURL:pathURL];
[data writeToFile:[NSString stringWithFormat:#"%#/HTML_Demo.html",documentsDirectory] atomically:YES];
// Fetching HTML file from DocumentDirectory
NSString *HTMLPath = [documentsDirectory stringByAppendingPathComponent:#"HTML_Demo.html"];
NSLog(#"delete HTML file Path: %#",HTMLPath);
if([[NSFileManager defaultManager] fileExistsAtPath:HTMLPath]) {
NSURL *targetURL = [NSURL fileURLWithPath:HTMLPath];
// Converting HTML to PDF
self.PDFCreator = [NDHTMLtoPDF createPDFWithURL:targetURL
pathForPDF:[#"~/Documents/HTML_Demo.pdf" stringByExpandingTildeInPath]
delegate:self
pageSize:kPaperSizeA4
margins:UIEdgeInsetsMake(20, 5, 90, 5)];
}
}
The URL may be loading from cache. In NDHTMLtoPDF.m, look in viewDidLoad:
Instead of: [webview loadRequest:[NSURLRequest requestWithURL:self.URL]];
Try using the constructor that allows you to set cachePolicy:
NSURLRequest requestWithURL:<#(NSURL *)#> cachePolicy:<#(NSURLRequestCachePolicy)#> timeoutInterval:<#(NSTimeInterval)#>
This policy might work better for your needs: NSURLRequestReloadIgnoringCacheData
Or you could just add something to your URL's querystring so it's never found in cache (e.g., an index or timestamp).

Is it possible for UIActivityViewController to attach AAC?

I tried this with no luck:
NSData *imageData = UIImageJPEGRepresentation([sight.photo valueForKey:#"image"], 0.8);
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsPath stringByAppendingPathComponent:#"sound.m4a"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSData *aacData = [sight.sound valueForKey:#"soundrecord"];
[aacData writeToURL:fileUrl atomically:YES];
// NSString *text = self.textField.text;
NSArray *items = #[imageData, aacData];
Yes, but you need to pass the audio file URL to the activity sheet rather than the NSData.
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:#"sound.aac"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSArray *activityItems = #[fileUrl];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
If you find that the attachment doesn't appear when launching the email composer from the activity sheet, make sure your file path and URL are correct and that the file exists (including the extension on the device/simulator drive).
To open a file on the simulator, go to folder (~/Library/Application Support/iPhone Simulator/...
To check the file name on device, go to XCode Organizer Window and select the Applications tab within the debugging device connected. You should see the file structure on the main panel.

read local html file and show on webview

I have been able to read a file called 'exerciseA.htm' from a live url. but now i need to bring the file locally add it to the project and read it from there. I have dragged the file into xcode to add it. Now i need to read its contents to a string, however the following doesnt seem to work:
NSString* exerciseAPage = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"exerciseA" ofType:#"htm"] encoding:NSUTF8StringEncoding error:&error];
[exerciseWebViewA loadHTMLString:exerciseAPage baseURL:nil];
Is this the correct way to load local htm file to a webview?
I do it the following way (Which is basically the same as you specify):
NSURL *url = [[NSBundle mainBundle] URLForResource:nameOfHtmlFile withExtension:#"html"];
NSError *error;
NSString *contentString = [NSString stringWithContentsOfURL:url encoding:NSStringEncodingConversionAllowLossy error:&error];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:contentString baseURL:baseUrl];
Hope this helps
Alex

write to a html file and display on iphone

i have a local html file that i can display on UIWebview now is there a way like i can pass my string value to that html page and display the content???
this is how i am loading html
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"index4" ofType:#"html"];
NSURL *instructionsURLd = [[NSURL alloc] initFileURLWithPath:urlAddress];
[webView loadRequest:[NSURLRequest requestWithURL:instructionsURLd]];
now index4.html content is
<html>
<body>
testing hhhhhhhhhhhhhhhhhhhhhhhhhhhhh
index1
<TD><INPUT TYPE=BUTTON OnClick="Circle_calc(this.form);" VALUE="calculate"></TD>
</body>
</html>
//
now lets say i have NSString s=#"hello"
now can i add this value to the < body > tag of html and show in UIwebview??
You cannot store mutable (changing) objects in your application bundle. However, you can put them there before building you app, open them when the app is executing, and write them to disk, and modify them whenever you want, after that.
Your code
NSString *s;
s = #"hello";
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"index4" ofType:#"html"];
NSURL *instructionsURLd = [[NSURL alloc] initFileURLWithPath:urlAddress];
NSError *error;
When your starts for the first time only Grab the resource and store it into a string
NSString *myHTML = [NSString stringWithContentsOfURL:instructionsURLd encoding:NSASCIIEncoding error:&error];
Immediately write the data to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSData *myData = [NSData dataWithBytes:[myHTML UTF8String] length:[myHTML lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"index4.html"];
[myData writeToFile:appFile atomically:YES];
Then, load it when you need it
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
NSString *appFile2 = [documentsDirectory2 stringByAppendingPathComponent:#"index4.html"];
NSData *mySavedData = [[[NSData alloc] initWithContentsOfFile:appFile2] autorelease];
NSString *myNewHTML = [[NSString alloc] initWithData:mySavedData encoding:NSUTF8StringEncoding];
Insert s into the HTML document
myNewHTML = [myHTML stringByReplacingOccurrencesOfString:#"<body>" withString:[NSString stringWithFormat:#"<body> %#",s]];
Write everything back to the file
NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory3 = [paths objectAtIndex:0];
NSMutableData *myData3 = [NSData dataWithBytes:[myNewHTML UTF8String] length:[myNewHTML lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSString *appFile3 = [documentsDirectory3 stringByAppendingPathComponent:#"index4.html"];
NSData *myNewData = [NSData dataWithBytes:[myNewHTML UTF8String] length:[myNewHTML lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
[myNewData writeToFile:appFile3 atomically:YES];
Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
You can simply do this by creating javascript method in HTML and then call it via IOS if you want to pass that string to html from IOS