-[NSString writeToFile:] 不改变文件的内容

发布于 2024-12-03 16:37:04 字数 647 浏览 1 评论 0原文

我正在尝试写入资源中的文件“index.html”。我可以毫无问题地加载文件,但我似乎无法写入它。没有任何内容显示为错误,它只是不写入。该应用程序不会中止或发生任何事情,但是当我重新加载文件时,没有任何变化。

我的编写代码:

NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *path = [thisBundle pathForResource:@"index" ofType:@"html"];
NSString *myString = [[NSString alloc] initWithFormat:@""];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];

我的加载代码:

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

我做错了什么?

I'm trying to write to a file "index.html" that I have in my resources. I can load the file with no problem, but I can't seem to write to it. Nothing is showing up as an error, it simply doesn't write. The app doesn't abort or anything, but when I re-load the file nothing has changed.

My writing code:

NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *path = [thisBundle pathForResource:@"index" ofType:@"html"];
NSString *myString = [[NSString alloc] initWithFormat:@""];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];

My loading code:

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎樣才叫好 2024-12-10 16:37:04

现有代码不会覆盖 index.html 文件的原因是应用程序无法覆盖其资源。 Apple 的 iOS 应用程序编程指南说:

这是包含应用程序本身的捆绑目录。不要向此目录写入任何内容。为了防止篡改,捆绑目录在安装时进行了签名。写入此目录会更改签名并阻止您的应用程序再次启动。

相反,写入您的文档目录。您可以像这样获取文档目录的路径:

NSString * docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

请注意,iOS simple 上的 NSHomeDirectory() 返回应用程序包的路径。一旦获得了文档目录的路径,您就可以写入资源,比如说index.html,如下所示:

NSString * path = [docsDir stringByAppendingPathComponent:@"index.html"];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

请注意,我将您的 error: 参数更改为 nil 。这实际上不会影响任何东西,但通常的做法是使用 nil 来指示 NULL Objective-C 对象。

The reason that your existing code doesn't overwrite the index.html file is that an application can not overwrite its resources. Apple's iOS Application Programming Guide specifically says:

This is the bundle directory containing the application itself. Do not write anything to this directory. To prevent tampering, the bundle directory is signed at installation time. Writing to this directory changes the signature and prevents your application from launching again.

Instead, write to your documents directory. You can get the path to the documents directory like this:

NSString * docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

Note that NSHomeDirectory() on iOS simple returns the path to your application's bundle. Once you have the path to the documents directory, you can write to a resource, let's say index.html, as follows:

NSString * path = [docsDir stringByAppendingPathComponent:@"index.html"];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

Note that I changed your error: parameter to nil. This will not actually affect anything, but it is common practice to use nil to indicate NULL Objective-C objects.

等往事风中吹 2024-12-10 16:37:04

尝试在执行操作之前将文件移动到文档目录,这堆代码使工作

.h

#import <Foundation/Foundation.h>

@interface NSFileManager (NSFileManagerAdds)
+ (NSString*) copyResourceFileToDocuments:(NSString*)fileName withExt:(NSString*)fileExt;
@end

.m

#import "NSFileManager + NSFileManagerAdds.h"

@implementation NSFileManager (NSFileManagerAdds)

+ (NSString*) copyResourceFileToDocuments:(NSString*)fileName withExt:(NSString*)fileExt
{
    //Look at documents for existing file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, fileExt]];

    NSFileManager* fileManager = [NSFileManager defaultManager];

    if(![fileManager fileExistsAtPath:path])
    {
        NSError *nError;
        [fileManager copyItemAtPath:[[NSBundle mainBundle] pathForResource:fileName ofType:fileExt] toPath:path error:&nError];
    }

    return path;
}

@end

最后,您应该在类似的情况下使用它:

[NSFileManager copyResourceFileToDocuments:@"index" withExt:@"html"];

Try to move your file to Documents Directory before perform the operation this bunch of code makes the work

.h

#import <Foundation/Foundation.h>

@interface NSFileManager (NSFileManagerAdds)
+ (NSString*) copyResourceFileToDocuments:(NSString*)fileName withExt:(NSString*)fileExt;
@end

.m

#import "NSFileManager + NSFileManagerAdds.h"

@implementation NSFileManager (NSFileManagerAdds)

+ (NSString*) copyResourceFileToDocuments:(NSString*)fileName withExt:(NSString*)fileExt
{
    //Look at documents for existing file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, fileExt]];

    NSFileManager* fileManager = [NSFileManager defaultManager];

    if(![fileManager fileExistsAtPath:path])
    {
        NSError *nError;
        [fileManager copyItemAtPath:[[NSBundle mainBundle] pathForResource:fileName ofType:fileExt] toPath:path error:&nError];
    }

    return path;
}

@end

Finally you should use it in something like that:

[NSFileManager copyResourceFileToDocuments:@"index" withExt:@"html"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文