iosswiftmainbundle

Pre-create Array on Mac, add it to main bundle to access in iOS App


In my app I'm using selfcreated arrays of points to mask images with CGPath.

So it looks like this

let pnt1 = CGPointMake(0, 33)
    let pnt2 = CGPointMake(33, 66)
    let pnt3 = CGPointMake(47, 71)
    let pnt4 = CGPointMake(66, 65)
    let pnt5 = CGPointMake(79, 69)
    let pnt6 = CGPointMake(90, 67)
    let pnt7 = CGPointMake(116, 36)
    let pnt8 = CGPointMake(93, 8)
    let pnt9 = CGPointMake(59, 0)
    let pnt10 = CGPointMake(37, 0)
    var pntz = NSMutableArray()
    pntz.addObject(NSValue(CGPoint: pnt1))
    pntz.addObject(NSValue(CGPoint: pnt2))
    pntz.addObject(NSValue(CGPoint: pnt3))
    pntz.addObject(NSValue(CGPoint: pnt4))
    pntz.addObject(NSValue(CGPoint: pnt5))
    pntz.addObject(NSValue(CGPoint: pnt6))
    pntz.addObject(NSValue(CGPoint: pnt7))
    pntz.addObject(NSValue(CGPoint: pnt8))
    pntz.addObject(NSValue(CGPoint: pnt9))
    pntz.addObject(NSValue(CGPoint: pnt10))

What i want is that i could write all my arrays to one file and then after my app has launched i'd load that file to app and use these arrays. I want to do it because these arrays are being created every 4-5 secs and i guess it's not good.


Solution

  • Basically here is what you need to do:

    1. Archive your array to file on MAC; and do it in Swift code!
    2. Add this file to your app's main bundle on MAC
    3. Unarchive file from the app's main bundle on iOS

    The tricky part is that you need to do all this automatically.

    1. Archive your array to file on MAC and do it in Swift code!

    Create Swift script that generates archive with array. First parameter of the script will accept App main bundle folder location(it will be passed at the second step). Add CreatePointsArrayArchive.swift file to your project, and do not add it to any targets.

    Contents of CreatePointsArrayArchive.swift:

    import Foundation
    import Cocoa
    
    println("MY APP: Generating points array archive...")
    
    let archiveFileName = "MyPointsArray.archive"
    let resourcesFolderPath = Process.arguments[1] // access first element because 0 contains this script file name
    
    // YOUR CODE:
    let pnt1 = CGPointMake(0, 33)
    let pnt2 = CGPointMake(33, 66)
    let pnt3 = CGPointMake(47, 71)
    let pnt4 = CGPointMake(66, 65)
    let pnt5 = CGPointMake(79, 69)
    let pnt6 = CGPointMake(90, 67)
    let pnt7 = CGPointMake(116, 36)
    let pnt8 = CGPointMake(93, 8)
    let pnt9 = CGPointMake(59, 0)
    let pnt10 = CGPointMake(37, 0)
    var pntz = NSMutableArray()
    pntz.addObject(NSValue(point: pnt1))
    pntz.addObject(NSValue(point: pnt2))
    pntz.addObject(NSValue(point: pnt3))
    pntz.addObject(NSValue(point: pnt4))
    pntz.addObject(NSValue(point: pnt5))
    pntz.addObject(NSValue(point: pnt6))
    pntz.addObject(NSValue(point: pnt7))
    pntz.addObject(NSValue(point: pnt8))
    pntz.addObject(NSValue(point: pnt9))
    pntz.addObject(NSValue(point: pnt10))
    
    
    // Archiving array to file
    if NSKeyedArchiver.archiveRootObject(pntz, toFile: archiveFileName) {
        println("    Array points archive generated SUCCESSFULY wit hfile name '\(archiveFileName)'.")
    } else {
        println("    FAILED to generate array points archive.")
    }
    
    // Copy file to app budle
    println("MY APP: Copying '\(archiveFileName)' to app main bundle at path '\(resourcesFolderPath)'...")
    var err: NSError?
    if NSFileManager.defaultManager().copyItemAtPath(archiveFileName, toPath: resourcesFolderPath + archiveFileName, error: &err) {
        println("    '\(archiveFileName)' file added to app main bundle.")
    } else {
        println("    FAILED to add '\(archiveFileName)' to app main bundle.")
    }
    

    ... so afteer 1-st step there is CreatePointsArrayArchive.swift file at the root folder of the project; with the code that listed above.

    2. Add this file to your app's main bundle on MAC

    You need to perform this as a build step. Here is what you need to do:

    1. Open you project build target and Run Script phase:

      enter image description here

    2. Type following line in script text area:

      xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) CreatePointsArrayArchive.swift ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/
      

      ... screenshot:

      enter image description here

    3. Unarchive file from the app's main bundle on iOS

    To access you archive in iOS App use code bellow:

    // NOTE: MyPointsArray.archive is name of the file that was defined in CreatePointsArrayArchive.swift file
    let pointsArchivePath = NSBundle.mainBundle().pathForResource("MyPointsArray", ofType: "archive")
    let pointsArr = NSKeyedUnarchiver.unarchiveObjectWithFile(archivePath) as NSArray