【问题标题】:How to save a downloaded file as Read-only in iOs如何在 iOs 中将下载的文件保存为只读
【发布时间】:2012-03-23 10:50:03
【问题描述】:

在我的 iPhone 应用程序中,我正在下载一些二进制文件。我想将这些文件存储在设备的本地文件夹中。而且我想将这些文件设置为只读/隐藏。可能吗?这样我就可以为文件提供更多安全性。有没有办法实现这个?请给我一些很好的解释以及一些代码示例。 谢谢

【问题讨论】:

    标签: iphone ios download readonly hidden-files


    【解决方案1】:

    您在应用中创建的所有文件都只能由您的应用访问。

    但你可以将它设置为不可变,我不知道它会对你有什么帮助。

    NSDictionary* att = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:@"NSFileImmutable", nil]];
    [filemgr createDirectoryAtPath:@"YourPath" withIntermediateDirectories:YES attributes:nil error:nil];
    [filemgr createFileAtPath:@"YourPath/yourFile.txt" contents:data attributes:att];
    

    【讨论】:

      【解决方案2】:

      您可以使用NSFileManager对文件设置POSIX权限,使用方法:

      - (BOOL)setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error
      

      我不确定这实际上给了你多少安全性。每个 iOS 应用程序都有自己的沙箱,除了系统之外,唯一可以接触您的文件的应用程序就是您的。所以你只是保护你自己的文件。

      【讨论】:

      • 我只是想在用户尝试通过系统访问文件时阻止该用户。通过打开根文件夹。
      • 什么系统?用户是设备上所有应用程序文件的所有者,您无法阻止用户访问自己的文件。
      • 我同意,我不确定,这就是我再次询问的原因。我只是想知道是否可以将文件设置为只读或隐藏在设备中,使其无法访问或被隐藏,并且只能通过应用程序访问文件。如果我说任何愚蠢的话,我很抱歉,因为我不熟悉这个领域。并感谢您的回复。
      【解决方案3】:
      +(BOOL)saveFile2DocDirectory:(NSData*)recievedData:(NSString*)moduleNam   {
      
      NSString *location=[NSString stringWithFormat:@"%@",moduleNam];
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
      
      //NSMutableDictionary *courseInfoDict=[[NSMutableDictionary alloc]init];
      
      
      return [recievedData writeToFile:fPath options:NSDataWritingFileProtectionComplete error:nil];
      
      
      }
      

      //这个功能可以帮到你。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2014-07-11
      相关资源
      最近更新 更多