write to a html file and display on iphone - iphone-sdk-3.0

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

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 Save data into Project Folder?

I download a XML file from web and save it in a folder.With these Codes
NSURL *goo = [[NSURL alloc]initWithString:#"......xml"];
NSData *data = [[NSData alloc] initWithContentsOfURL:goo];
NSString *xml = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *documentsDirectory = #"/Users/MacBook/Desktop/Bebegim_Icin_Yarisiyorum/Milyoner/Class";
NSString *xmlFilePath = [documentsDirectory stringByAppendingPathComponent:#"qpack1.xml"];
[xml writeToFile:xmlFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
Class Folder is in myProject Folders.
But Apple does not have this path.I need to say save the
xml file in Class folder of My project Folders.
/Users/MacBook/Desktop/Bebegim_Icin_Yarisiyorum/Milyoner/Class
Can anyone help me,I should say ../Class or .Class or ./Class?
What should I say when I am creating documentDirectory.
Try:
NSString *documentsDirectory = [NSHomeDirectory()stringByAppendingPathComponent:#"Documents/qpack2.xml"];
Or:
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *docsPAth = [pathArray objectAtIndex:0];
NSString *documentsDirectory = [documentsDirectory stringByAppendingPathComponent:#"qpack2.xml"];

How to upload image, which is not saved on the device to the dropbox account?(IOS)

Dropbox restClient saves only files. So i want to save the image in local folder first and then to upload it, as a result it saves file, but it is corrupted.
NSString *localPath = [[NSBundle mainBundle] pathForResource:#"Info" ofType:#"plist"];
NSString *jpegFilePath = [NSString stringWithFormat:#"%#/test.jpeg",localPath];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data2 writeToFile:jpegFilePath atomically:YES];
NSString *filename = #"test.jpeg";
NSString *destDir = #"/";
[[self restClient] uploadFile:filename toPath:destDir
withParentRev:nil fromPath:localPath];
I am an idiot, solved
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingString:#"test.jpg"];
NSData * data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data writeToFile:path atomically:YES];
[self.restClient uploadFile:#"test.jpg" toPath:#"/" withParentRev:nil fromPath:path];
you will have to call DBRestClient methods from the main thread or a thread that has a run loop. Otherwise the delegate methods will not be called.
what you will have to do is first alloc init yoir DBRestClient object then make it's delegate self and then you can easily upload your file.below is an example
NSString *destDir = #"/";
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
[restClient uploadFile:yourfilename toPath:destDir withParentRev:nil fromPath:sourcepath];
Happy Coding!!!!!!
You can't save into the bundle. You should use documents or cache folder:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

How can I read user input text from a textfield and write it to a text file in Xcode?

Is there a way to write a string , which is read from text field, to a .txt file?
NSString *input = [textfield text];
NSString *path = #"myText.txt";
[input writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
Yes, ultimately that is the way; but there's a number of other steps you do in order to get from start to finish (you need to have textfield connected to an IBOutlet or in some other way accessible; the path where myText.txt is writing to needs to be writable and therefore you'd probably need to have a longer, more precise path than just the filename; you'd probably need to also send in an actual error parameter that could be set so you could look at the errors being returned when the writeToFile call fails the first few times you run this code).
You will need to define a path, basically where to save the file.
This code will find the Documents folder
NSArray *path = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirPath = [path objectAtIndex:0];
Here is the code I use to read and write files.
-(void)writeFileToDisk:(id)data
{
NSArray *path = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirPath = [path objectAtIndex:0];
NSString *fileName = #"MyFileName.txt";
NSString *fileAndPath = [documentDirPath stringByAppendingPathComponent:fileName];
[data writeToFile:fileAndPath atomically:YES];
}
-(void)readFileFromDisk
{
NSArray *path = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirPath = [path objectAtIndex:0];
NSString *fileName = #"MyFileName.txt";
NSString *fileAndPath = [documentDirPath stringByAppendingPathComponent:fileName];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:fileAndPath];
NSLog(#"%#",array);
[array release];
}

NSMutable Array to XML file

I have an NSMutableArray of custom objects called Clip. Each clip Object has some NSString properties (clipName,tcIn, tcOut, file path and so on..). How do I write an xml File on disk where each clip in the array is a node of the xml file and its properties are elements of the node?
Thanks in advance
Dario
I think this is what should do you:
//Create xml string
NSMutableString *xmlString = [[NSMutableString alloc] init];
//Add all the data to the string
[xmlString appendFormat:#"<clips>"];
for (Clip *c in clipsArray)
{
[xmlString appendFormat:#"\n\t<clip>"];
[xmlString appendFormat:#"\n\t\t<clipName>%#</clipName>", c.clipName];
[xmlString appendFormat:#"\n\t\t<tcIn>%#</tcIn>", c.tcIn];
...
[xmlString appendFormat:#"</clip>"];
}
[xmlString appendFormat:#"</clips>"];
//Create a variable to represent the filepath of the outputted file
//This is taken from some code which saves to an iPhone app's documents directory, it might not be ideal
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:#"output.xml"];
//Actually write the information
NSData *data = [xmlString dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil];