Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将文本保存到应用程序内的文件_Ios - Fatal编程技术网

Ios 将文本保存到应用程序内的文件

Ios 将文本保存到应用程序内的文件,ios,Ios,是的,我以前见过这样的解决方案,但有九种似乎奏效了。我想将文本字段中的文本保存到支持文件中的文件中 最好是,我希望它是一个HTML文件,我希望它在那里,因为我想在Web视图中再次打开它 我知道如何加载,这只是节省 问题:如何将文本字段中的文本保存到支持文件目录中的文件中?请尝试以下操作: - (void)writeStringToFile:(NSString*)aString { // Build the path, and create if needed. NSString

是的,我以前见过这样的解决方案,但有九种似乎奏效了。我想将文本字段中的文本保存到支持文件中的文件中

最好是,我希望它是一个HTML文件,我希望它在那里,因为我想在Web视图中再次打开它

我知道如何加载,这只是节省


问题:如何将文本字段中的文本保存到支持文件目录中的文件中?

请尝试以下操作:

- (void)writeStringToFile:(NSString*)aString {

    // Build the path, and create if needed.
    NSString *theString = @"The text to write";
    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* fileAtPath = [filePath stringByAppendingPathComponent:@"data.txt"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
        [[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
    }
    [[theString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
}
问题解决(HTML)!!! 试试这个代码

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentDirectory stringByAppendingPathComponent:@"myfile.html"];

NSString *textFromTextField = textField.text;
NSString *finalText = [NSString stringWithFormat:@"<html><body>%@</body><html>",textFromTextField];

[finalText writeToFile:@"" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSString*documentdirectories=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString*文件名=[documentDirectory stringByAppendingPathComponent:@“myfile.html”];
NSString*textFromTextField=textField.text;
NSString*finalText=[NSString stringWithFormat:@“%@”,textFromTextField];
[finalText writeToFile:@”“原子:是编码:NSUTF8StringEncoding错误:NULL];

这与xcode无关。Xcode只是一个IDE,因为某种原因,这两种方法都不起作用。什么也没发生;没有创建/写入文件(即使以前创建)。我错过什么了吗?