2011-07-12 42 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