XCODE writing from string to html file
I have read html to string and changed it, I would like to write it (string) to some temporary file(html), and afterwards also would like to write over it new html file from string, how can this be done?? I am total开发者_C百科ly lost for now
NSString *htmlString = [NSString stringWithContentsOfFile:@"pathToFile" encoding:NSUTF8StringEncoding error:nil];
NSString *finalString = [htmlString stringByReplacingOccurrencesOfString:@"75" withString:@"900"];
and now I should write it back to some temporary html file !?
You can write that string to an HTML file in your app’s documents directory (an area inside the sandbox that the app has write access to—you can optionally allow iTunes to access files in that directory, letting your users easily get files out of your app) like this:
NSString *rootDocumentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [rootDocsPath stringByAppendingPathComponent:@"myFile.html"];
NSError *error = nil;
if([finalString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error])
// hooray, it worked
else
// it didn't work; "error" will now contain a description of what went wrong
精彩评论