2011-07-12 44 views
1

我已经HTML阅读串并改变了它,我想它(字符串)写入一些临时文件(HTML),之后还要写了从它新的HTML文件串,如何才能做到这一点?我完全失去了现在XCODE写入HTML文件

NSString *htmlString = [NSString stringWithContentsOfFile:@"pathToFile" encoding:NSUTF8StringEncoding error:nil]; 

NSString *finalString = [htmlString stringByReplacingOccurrencesOfString:@"75" withString:@"900"]; 

,现在我应该把它写回一些临时的HTML文件!?

回答

2

您可以在字符串写入到您的应用程序的文件目录(该应用程序有写权限,您可以选择允许iTunes访问该目录文件的沙箱内部的区域的HTML文件,让您的用户轻松地获取文件你的应用程序)是这样的:

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