Downloading files from an Objective C or Swift server

How do I download audio files from my server to a custom phone that will be used in the application? When I go to a website link, the JSON code looks like this

[{"name":"Lets Party","path":"http:\/\/domain.us\/\/\/audios\/\/Lets Party.wav"},
{"name":"Let You Know","path":"http:\/\/domain.us\/\/\/audios\/\/Let You Know.wav"},
{"name":"OMG","path":"http:\/\/domain.us\/\/\/audios\/\/OMG.wav"}]

      

I tried to get JSON and convert it to Swift but it broke my application. But when I post a JSON blog, it works fine, no glitch. How do I do this in Swift or Objective-C?

EDIT: I get an error message.

Current code:

        let urlPath = "http://www.domain.us/"

        let url = NSURL(string: urlPath)

        let session = NSURLSession.sharedSession()

        let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in

            if error != nil {
                println(error)
            } else {
                let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
                println(jsonResult)
            }

        })

        task.resume()
    }

      

Error message:

2014-11-12 20:07:23.652 JSON Practice[6073:284290] CFNetwork SSLHandshake failed (-9847)
2014-11-12 20:07:23.874 JSON Practice[6073:284290] CFNetwork SSLHandshake failed (-9847)
2014-11-12 20:07:24.054 JSON Practice[6073:284290] CFNetwork SSLHandshake failed (-9847)
2014-11-12 20:07:24.055 JSON Practice[6073:284290] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9847)
Error Domain=NSURLErrorDomain Code=-1200 "The operation couldn’t be completed. (NSURLErrorDomain error -1200.)" UserInfo=0x7fc0ba656020 {NSErrorFailingURLStringKey=https://www.domain.us/, NSErrorFailingURLKey=https://www.makemeip.us/, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9847, NSUnderlyingError=0x7fc0ba52ebb0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1200.)"}

      

+3


source to share


1 answer


//
//  ViewController.swift
//  Test2
//
//  Created by adm on 11/14/14.
//  Copyright (c) 2014 Dabus.Tv. All rights reserved.
//

import UIKit
import Foundation
import AVFoundation

class 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]()
    }
}

      



+1


source







All Articles