从服务器目标C或Swift下载文件

我怎样才能从我的服务器下载audio文件到用户的手机在应用程序中使用? 当我去到网站链接,JSON代码就像这样

[{"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"}] 

我试图得到的JSON和转换它在Swift中,但是崩溃了我的应用程序。 但是当我把博客JSON,它完美的作品,没有崩溃。 我怎么能在Swift或Objective-C中做到这一点?

编辑:我现在得到一个错误消息。

当前代码:

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

错误信息:

 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.)"} 

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