2019年7月2日 星期二

iOS swift 5: Xcode Project SQLite Bundle


  解決方案來源:
   bundle to documents directory in iOS
 
 
  Step 1: 把 SQLite的資料庫檔案MyDatabase.db 拖拉到 Xcode Project中
  Step 2: 設定 Copy Bundle Resources 加入剛才拖拉到 Xcode Project中MyDatabase.db

 
  Step 3:
func copyfileToDocs()
{
        let bundlePath = Bundle.main.path(forResource: "MyDatabase", ofType: ".db")
        print(bundlePath!, "\n") //prints the correct path
        let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        let fileManager = FileManager.default
        let fullDestPath = NSURL(fileURLWithPath: destPath).appendingPathComponent("fj200ls.db")
        let fullDestPathString = fullDestPath?.path
        print(fileManager.fileExists(atPath: bundlePath!)) // prints true
        do
        {
            try fileManager.copyItem(atPath: bundlePath!, toPath: fullDestPathString!)
            print("DB Copied")
        }
        catch
        {
            print("\n")
            print(error)
        }
}



沒有留言:

張貼留言