swift - iOS: Download .TXT file give strange result -
i'm trying download .txt file has 5mb size in 1 website
i tried download file directly , give correct size
i tried download via android downloadmanager.request , give correct size
except, in ios, while tried download via nsurlconnection, without 1 second, show successful download 5mb size however, when check in network receiving show used 25kb?
ps. if change download same domain .zip file, download properly
problem link: http://www.bluewindsolution.com/speed_test/downloads/download_file.txt
working link: http://www.bluewindsolution.com/speed_test/downloads/download_file.zip
here code
@ibaction func buttonclick(sender: anyobject) { let timestamp = nsstring(format: "%.0f", nsdate().timeintervalsince1970) let url:nsurl = nsurl(string: "http://www.bluewindsolution.com/speed_test/downloads/download_file.txt?time=\(timestamp)")! var request:nsurlrequest = nsurlrequest(url: url, cachepolicy: nsurlrequestcachepolicy.reloadignoringlocalcachedata, timeoutinterval: 30.0) startdate = nsdate() nsurlconnection(request: request, delegate: self) } func connectiondidfinishdownloading(connection: nsurlconnection, destinationurl: nsurl) { println("finishdownload \(destinationurl)") println("timetotal: \(timetotal)") println("speed: \(speed) mbps") println("filesize: \(filesize)") // it's show 5mb network receive 25kbps ? it's not cache because it's happen @ first time also. connection.cancel() } func connection(connection: nsurlconnection, didwritedata byteswritten: int64, totalbyteswritten: int64, expectedtotalbytes: int64) { let currentdate:nsdate = nsdate() let tt = currentdate.timeintervalsincedate(startdate) timetotal = tt // total time speed = double(totalbyteswritten)/1024/1024*8/tt // speed mbps filesize = double(totalbyteswritten)/1024/1024 // mb // use result real time //println("timetotal: \(totaltime)") //println("speed: \() mbps") //println("filesize: \()") }
output result
result of network receive in emulator
seems problem method connectiondidfinishdownloading
. please use below delegate methods , it's working
var mdata = nsmutabledata() func connection(connection: nsurlconnection, didreceivedata data: nsdata) { mdata.appenddata(data) println("loading") } func connectiondidfinishloading(connection: nsurlconnection) { let documentspath = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)[0] as! nsstring let filepath = documentspath.stringbyappendingpathcomponent("text.txt") mdata.writetofile(filepath, atomically: true) println("\(filepath)") } func connection(connection: nsurlconnection, didfailwitherror error: nserror) { println("\(error)") }
Comments
Post a Comment