Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How writeToFile in a .playground?

I have edited a .plist file. How I understood it: because of sandboxing it is only possible to read files inside the .playgrounds file in a Resources directory. But how is it possible to write the edited file to this folder?

// read file
let xmlPath: String = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist")!
var xmlDictionary: [String : AnyObject] = NSDictionary(contentsOfFile: xmlPath) as Dictionary<String, AnyObject>

// editing file
// ...

// saving file
let saveDict: NSDictionary = xmlDictionary as NSDictionary
let savePath: String = xmlPath.stringByDeletingLastPathComponent.stringByAppendingPathComponent("Saved.plist")
saveDict.writeToFile(savePath, atomically: false) // ~> false

Whether savePath nor xmlPath is working, returns false meaning no success.

It would be nice if I could replace the old (non-edited) version of the .plist file, but saving to a new file would also be ok.

like image 435
Binarian Avatar asked Mar 06 '15 10:03

Binarian


2 Answers

modifying files in the main bundle doesn't appear to be possible, see earlier question. What you can do is to save to the sandboxed application folders, the URLs for which can be retrieved like this:

 func applicationDirectory(directory:NSSearchPathDirectory) -> NSURL? {

    var appDirectory:String?
    var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(directory, NSSearchPathDomainMask.UserDomainMask, true);
    if paths.count > 0 {
        if let pathString = paths[0] as? String {
            appDirectory = pathString
        }
    }
    if let dD = appDirectory {
        return NSURL(string:dD)
    }
    return nil
}



func applicationTemporaryDirectory() -> NSURL? {

    if let tD = NSTemporaryDirectory() {
        return NSURL(string:tD)
    }

    return nil

}

applicationTemporaryDirectory()

applicationDirectory(NSSearchPathDirectory.DocumentDirectory)

The urls will display in the righthand side of the playground, then simply copy and paste into the Finder -> Go -> Go to Folder dialogue box.

GitHub Repo: For a full working example see this GitHub repo.

like image 68
sketchyTech Avatar answered Oct 20 '22 10:10

sketchyTech


You can.

Use your current plist as a template by looking at its source code.

enter image description here

Then assign it to a string

let newPlist: String = 
"""
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIconFile</key>
    <string></string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSMinimumSystemVersion</key>
    <string>$(MACOSX_DEPLOYMENT_TARGET)</string>
    <key>NSHumanReadableCopyright</key>
    <string>Copyright © 2020 YourName. All rights reserved.</string>
    <key>NSMainStoryboardFile</key>
    <string>Main</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
    <key>NSSupportsAutomaticTermination</key>
    <true/>
    <key>NSSupportsSuddenTermination</key>
    <true/>
    <key>TDAMERITRADE_API_KEY</key>
    <string></string>
    <key>ACCOUNT_ID</key>
    <string></string>
    <key>CFBundleURLSchemes</key>
    <string>YourName.YourProjName</string>
</dict>
</plist>
"""

Then call the write method on strings using filePath for the url.

let url: URL = URL(fileURLWithPath: "/Users/username/Desktop/iOS/YourProj/YourProj/info.plist")
newPlist.write(to: url, atomically: true, encoding: .utf8)
like image 28
ScottyBlades Avatar answered Oct 20 '22 11:10

ScottyBlades