dimanche 23 août 2015

How to copy files from a directory to iphone document directory

What I am trying to do is, in app delegate, I want to write a code that will copy a sqlite database if it is not exists in iphone's document directory. For that I am using the following code-

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let containerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()

//Create database if not exists
let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
let databaseStr = "LocalDatabase.sqlite"
let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)
let fileManager: NSFileManager = NSFileManager.defaultManager()
if !fileManager.fileExistsAtPath(dbPath) {
    let databaseInApp: String? = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent(databaseStr)
    fileManager.copyItemAtPath(databaseInApp!, toPath: dbPath, error: nil)
}

return true
}

It's creating the database in the directory that is ok. But I am not getting ant tables there in database. That means new file is created instead of copying. I am sure there are 9 tables in that database which I want to copy.

Structure of files are as given in the screenshot-

enter image description here

Where I am wrong that I did not understand. Please tell me if anybody is able to catch the problem. One more thing When I was running the application in the simulator with the /Users/Adelantelabs/Documents/Sidemenu.swift/SlideOutNavigation/Localdatabase.sqlite Then it was working perfectly but did not work in iphone when I run it.

Aucun commentaire:

Enregistrer un commentaire