如何将音频文件从我的服务器下载到用户的手机中以便在应用中使用?当我进入网站链接时,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.)"}
答案 0 :(得分:1)
//
// 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]()
}
}