Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中运行get文件重定向_Ios_Json_Swift - Fatal编程技术网

Ios 如何在swift中运行get文件重定向

Ios 如何在swift中运行get文件重定向,ios,json,swift,Ios,Json,Swift,我正在使用swift访问youtubeinmp3 api,我被卡住了。我对这个很陌生,所以请温柔一点。使用api,我得到一个json响应,并使用返回的link属性下载文件。但是,我得到的链接如下所示: 该链接在Chrome等浏览器中运行良好,但swift应用程序下载未知文件,绝对不是mp3 我用这个来找到代码 有没有关于如何通过重定向进入mp3下载链接的想法? 先谢谢你 override func tableView(tableView: UITableView, didSelectRowAtI

我正在使用swift访问youtubeinmp3 api,我被卡住了。我对这个很陌生,所以请温柔一点。使用api,我得到一个json响应,并使用返回的link属性下载文件。但是,我得到的链接如下所示:

该链接在Chrome等浏览器中运行良好,但swift应用程序下载未知文件,绝对不是mp3

我用这个来找到代码

有没有关于如何通过重定向进入mp3下载链接的想法? 先谢谢你

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let videoURL1 = (videosArray[indexPath.row]).objectForKey("videoID") as! String
    //let videoURL = "https://www.youtube.com/watch?v=\(videoURL1)"
    let videoURL = "//www.youtubeinmp3.com/fetch/?video=https://www.youtube.com/watch?v=\(videoURL1)"
    let urlString = "http://www.youtubeinmp3.com/fetch/?format=JSON&video=\(videoURL)"
    let url = NSURL(string: "http://www.youtubeinmp3.com/fetch/?format=JSON&video=\(videoURL)")
    let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "GET"

    let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
        if (error == nil) {
            if let response = response as? NSHTTPURLResponse {
                print("response=\(response)")
                if response.statusCode == 200 {
                    if data != nil {
                        do {
                            let responseJSON =  try  NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary;
                            //let urlString = NSURL(string:"\(responseJSON["link"] as! String)")
                            //print("URLString: \(urlString)")
                            //let directDownloadURL = NSURL(string: urlString)
                            // Call your method loadFileAsync

                                // code here
                            let urlString = responseJSON["link"] as! String

                            //let finalURLString = urlString.stringByReplacingOccurrencesOfString("get", withString: "mp3")

                           print(urlString)

                            dispatch_async(dispatch_get_main_queue(), {

                                let identifier = NSUUID().UUIDString
                                let downloadItem = DownloadsTableViewCellItem(identifier: identifier, urlString: urlString, infoLabelTitle: selectedName, stateLabelTitle: "Press Download", progressLabelTitle: "", action: DownloadsTableViewCellAction.Download)
                                DownloadsViewController().addDownloadItem(downloadItem, withIdentifier: identifier)

                                SVProgressHUD.showSuccessWithStatus("Download Added")
                                    print("Download Task Added")

                            })

                        }
                        catch let JSONError as NSError {
                            print("\(JSONError)")
                        }
                        catch {
                            print("unknown error in JSON Parsing");
                        }

                    }
                }
            }
        }
        else {
            print("Failure: \(error!.localizedDescription)");
        }
    })
    task.resume()

}

你知道最终的url应该是什么吗?比如像
download.youtube…
?如果是这样,您可以使用
UIWebView
加载初始URL,然后查找结束URL,请执行签入
shouldStartLoadWithRequest:
。当你得到它,然后下载文件。你可以下载Alamofire源代码,看看他们是如何做到的。或者您也可以使用阿拉莫菲尔。@sbarow谢谢您的快速回复。你能给我举个例子,说明我将如何着手做那样的事情吗?