//
// ViewController.swift
// Test_HomeWork
//
// Created by apple on 14-6-24.
// Copyright (c) 2014年 apple. All rights reserved.
//
import UIKit
class ViewController: UIViewController,NSURLConnectionDataDelegate{
var mData: NSMutableData!
var length: CLongLong = 0
@IBOutlet var pro : UIProgressView
@IBOutlet var textView : UITextField
@IBAction func didTap(sender : AnyObject) {
var url = NSURL(string: "\(textView.text)")
var request = NSURLRequest(URL: url)
var connection = NSURLConnection(request: request, delegate: self)
connection.start()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
println("connection failed: \(error)")
}
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
var httpResponse = response as NSHTTPURLResponse
println("length: \(httpResponse.expectedContentLength)")
length = httpResponse.expectedContentLength
println("filename: \(httpResponse.suggestedFilename)")
if httpResponse.statusCode == 200 {
println("\(httpResponse)")
mData = NSMutableData()
}
else {
println("失败")
}
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
mData.appendData(data)
var progress = Double(mData.length) / Double(length)
println("receive data: \(progress * 100)%")
pro.progress = CGFloat(progress)
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
mData.writeToFile("/Users/apple/Desktop/Reeder204.dmg", atomically: true)
println("下载完成")
}
@IBAction func tap(sender : AnyObject) {
self.view.endEditing(true)
}
}
6.24 Swift语言实现下载功能
最新推荐文章于 2024-07-24 10:49:10 发布