Downloading Files from server Objective C or Swift Downloading Files from server Objective C or Swift json json

Downloading Files from server Objective C or Swift


////  ViewController.swift//  Test2////  Created by adm on 11/14/14.//  Copyright (c) 2014 Dabus.Tv. All rights reserved.//import UIKitimport Foundationimport AVFoundationclass ViewController: UIViewController {    @IBOutlet weak var strFiles: UITextView!    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        if let url = NSURL(string: "http://www.anyurl.com/") {            if let loadedString = String(contentsOfURL: url) {                // file string was sucessfully loaded                // add the file path + / + filename + extension                let localUrl = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String) + "/" + "data" + ".db"  // Documents URL as String (local storage path)                // check local file path url                if let checkLocalUrl = NSURL(fileURLWithPath: localUrl) {                    let savedFile = loadedString.writeToURL(checkLocalUrl, atomically: true, encoding: NSUTF8StringEncoding, error: nil) // write to url savedFile will true or false                    if savedFile {                        // do whatever                        self.strFiles.text = "\(self.strFiles.text)path: \(checkLocalUrl)\n"                        self.strFiles.text = "\(self.strFiles.text)file was sucessfully saved\n"                    }                    // lets create an ARRAY with your string                    var arrayLines = ""                    if let myArrayParsed = loadedString.JSONParseArray() {                        for elem:AnyObject in myArrayParsed {                            let name = elem["name"] as String                            let path = elem["path"] as String                            arrayLines = "Name: \(name), Path: \(path)"                            self.strFiles.text = "\(self.strFiles.text)\(arrayLines)\n"                        }                    } else {                        // could not parse array                    }                } else {                    // could not check local url                }            } else {                // could not load string contents from url            }        } else {            // invalid url        }    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}extension String {    func JSONParseArray() -> [AnyObject]? {        if let data = self.dataUsingEncoding(NSUTF8StringEncoding) {            if let array = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil)  as? [AnyObject] {                return array            }        } else {            return nil        }        return [AnyObject]()    }}