在iOS中,你可以使用NSString的replaceOccurrencesOfString方法来替换字符串中的特定内容。然后,你可以使用NSData的writeToURL方法将替换后的内容保存到指定的文件路径。

以下是一个示例代码:

NSString *str = @"www.baidu.com";

// 获取文件路径
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index.b1c2c99e" withExtension:@"js" subdirectory:@"h5/static/js"];
NSString *filePath = [url path];

// 读取文件内容
NSError *error;
NSString *fileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

if (error) {
    // 处理读取文件错误
    NSLog(@"读取文件错误:%@", error.localizedDescription);
} else {
    // 替换内容
    NSString *newContent = [fileContent stringByReplacingOccurrencesOfString:@"localhost.com" withString:str];

    // 保存到文件
    NSData *newData = [newContent dataUsingEncoding:NSUTF8StringEncoding];
    BOOL success = [newData writeToFile:filePath atomically:YES];

    if (success) {
        NSLog(@"保存成功");
    } else {
        NSLog(@"保存失败");
    }
}

注意:由于iOS应用程序的资源文件(包括NSBundle mainBundle中的文件)是只读的,所以你不能直接修改它们。上述代码中,我们先将文件内容读取到内存中进行修改,然后再将修改后的内容写入到同一文件路径中,实现替换并保存的效果

ios 获取到了NSString str = wwwbaiducom NSURL url = NSBundle mainBundle URLForResourceindexb1c2c99e withExtensionjs subdirectoryh5staticjs; 文件里面的内容替换里面所有的localhostcom 替换成str 然后保存到NSBundle mainBundle URLFor

原文地址: https://www.cveoy.top/t/topic/itVM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录