zoukankan      html  css  js  c++  java
  • Swift中在应用沙盒中安全的存储文件

    如何保存沙盒中的文件,避免不经过授权的读取

          用户通常会信任应用,这意味着,如果应用要求提供个人信息,如姓名,用户会希望在安全支出存储这些纸,并且避免黑客或者其他临时使用用户iOS设备的人获取该信息。所以,在发布应用或者开发阶段,对开发已发布配置文件启用文件保护功能,并且确认存储在磁盘上的文件是经过保护的

     配置文件信息

    配置文件地址

    /*配置文件地址*/
    var filePath:String?{
            let filemanager = FileManager()
            do {
                //for 存放地址  地址显示方式  create:是否创建
                //documentDirectory将程序创建时产生的文件以及应用浏览产生的文件数据保存在该目录下
                //libraryDirectory存储程序的默认设置或其他状态信息NSUserDefault
                //cachesDirectory存放缓存文件,保存应用的持久化数据
                let documentFoldUrl = try filemanager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                let fileName = "file1.txt"
                let filePath = documentFoldUrl.path+"/"+(fileName)
                return filePath
                
                
            } catch {
            }
            return nil
        }
    

     文件写入:

     /**前提条件  1:经过有效的配置文件签名  2.配置文件启用文件保护 3.工程经过代码签名授权*/
            let fileManager = FileManager()
            if let path = filePath{
                let dataToWrite = "Hello,world".data(using: .utf8, allowLossyConversion: false)
                //kCFURLFileProtectionNone:该值表示对文件不采取保护措施
                //kCFURLFileProtectionComplete:对文件采用最高的保护措施,当设备锁定的时候,任何工具都不能读取文件内容
                //kCFURLFileProtectionCompleteUnlessOpen:与kCFURLFileProtectionComplete的区别是,当文件被打开时可以访问,一只访问到文件被删除
                //kCFURLFileProtectionCompleteUntilFirstUserAuthentication:应用可以在用户第一次解锁设别后读写存储文件,以后再次锁定也可以读取
                let fileAttributes = [FileAttributeKey.protectionKey : kCFURLFileProtectionComplete]
    
                let wrote = fileManager.createFile(atPath: path, contents: dataToWrite, attributes: fileAttributes as [FileAttributeKey : Any])
                if wrote{
                    print("success")
                }else{
                    print("error")
                }
            }
    

     文件读取和插入

    if let path = filePath{
                if fileManager .fileExists(atPath: path){
                    
                    //这里是读取文件
                    /*
                    let data =  fileManager.contents(atPath: path)
                    let value = String(data: data!, encoding: String.Encoding.utf8)
                    print("value:(value)")
                    */
                    /*
                    let handle =  FileHandle.init(forReadingAtPath: path)
                    let data = handle?.readDataToEndOfFile()
                    let value = String(data: data!, encoding: String.Encoding.utf8)
                    print("value:(value)")
                    */
                    /*在文件中插入数据*/
                    /*
                    let handle =  FileHandle.init(forWritingAtPath: path)
                    handle!.seekToEndOfFile()//在文件默认添加数据
                    handle!.write("this is append data".data(using: .utf8, allowLossyConversion: false)!)//追加数据
                    handle!.closeFile()//关闭文件
                   */
                   
                }
            }
    

     

  • 相关阅读:
    前端性能优化--图片处理(Css Sprites 与 base64)
    CSS规范--春风十里不如写好CSS
    前端性能优化--图片懒加载(lazyload image)
    struts2不同版本在核心filter在web.xml中的配置
    Web.xml配置详解之context-param
    web.xml的加载过程是context-param >> listener >> fileter >> servlet
    web.xml中Listener的作用
    web.xml中Filter的作用
    字符串反转
    fastjson是什么东东?
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/12039871.html
Copyright © 2011-2022 走看看