iOS development--network programming Swift & (vii) nsurlsession detailed
Last Update:2015-06-07
Source: Internet
Author: User
<span id="Label3"></p><p style="text-align: center;"><p style="text-align: center;"><span style="font-size: 18pt;">Nsurlsession detailed</span></p></p><p><p></p></p><p><p></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">MARK:-* * Load data using Nsurlsessiondatatask *</span> /</span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">func sessionloaddata ()</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a Nsurl object</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>var url:nsurl! = Nsurl (<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span>:<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">http://m.weather.com.cn/data/101010100.html</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create Request Object</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>var request:nsurlrequest =<span style="color: #000000;"><span style="color: #000000;">nsurlrequest (url:url)</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008080;"><span style="color: #008080;">Ten</span></span>Let session =<span style="color: #000000;"><span style="color: #000000;">nsurlsession.sharedsession ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>var datatask = session.datataskwithrequest (request, completionhandler: {var data:nsdata?, var response:nsurlresponse? , Var error:nserror?) -Void<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">return error message condition</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(error! =<span style="color: #000000;"><span style="color: #000000;">Nil)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>println (error?)<span style="color: #000000;"><span style="color: #000000;">. Code)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>println (error?)<span style="color: #000000;"><span style="color: #000000;">. Description)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>var jsonstring = NSString (data:data!<span style="color: #000000;"><span style="color: #000000;">, Encoding:nsutf8stringencoding)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #000000;"><span style="color: #000000;">println (jsonstring)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>})<span style="color: #0000ff;"><span style="color: #0000ff;"></span> as</span><span style="color: #000000;"><span style="color: #000000;">Nsurlsessiontask</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">to <span style="color: #008000;">start a task by using the Resume method</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Datatask.resume ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">MARK:-*/* use Nsurlsessiondatatask to download files----simple download, do not need to know the download progress *</span> /</span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">func sessionsimpledownload ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;"></span></span><span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span>var url = nsurl (<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span>:<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">http://www.iphonetrain.com/shop/data/ios/201404/source/2809080296130100.jpg</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Request</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>var request = Nsurlrequest (url:url!<span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Connection</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>var session =<span style="color: #000000;"><span style="color: #000000;">nsurlsession.sharedsession ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Download Task</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>var downloadtask = session.downloadtaskwithrequest (request, completionhandler: {(var location:nsurl?,var response: nsurlresponse, var error:nserror?) -Void<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Output download File The original storage directory</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;">location <span style="color: #800000;">: \ (location)</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Location Conversion</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>var Locationpath = location?<span style="color: #000000;"><span style="color: #000000;">. Path</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;">Wuyi</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">copy it to our own directory.</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Let documents:string = nshomedirectory () +<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/documents/1.png</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> wu</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">creating a File Manager</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>var Filemanager:nsfilemanager =<span style="color: #000000;"><span style="color: #000000;">Nsfilemanager.defaultmanager ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span>Filemanager.moveitematpath (locationpath!<span style="color: #000000;"><span style="color: #000000;">, topath:documents, Error:nil)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;">location <span style="color: #800000;">: \ (documents)</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> })</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">to <span style="color: #008000;">start a task by using the Resume method</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">Downloadtask.resume ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">MARK:-*/* use Nsurlsessiondatatask to download files----get progress *</span> /</span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a download mode-simple interest implementation</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Func currentsession ()<span style="color: #000000;"><span style="color: #000000;">nsurlsession{</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span>var predicate:dispatch_once_t =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>var currentsession:nsurlsession? =<span style="color: #000000;"><span style="color: #000000;">Nil</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span>Dispatch_once (&<span style="color: #000000;"><span style="color: #000000;">predicate,{</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> about</span>var config =<span style="color: #000000;"><span style="color: #000000;">nsurlsessionconfiguration.defaultsessionconfiguration ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span>Currentsession = Nsurlsession (configuration:config,<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">: self, Delegatequeue:nil)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> )</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>currentsession!<span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">Bayi</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">func sessionseniordownload ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;"></span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>var url = nsurl (<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span>:<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">http://www.iphonetrain.com/shop/data/ios/201404/source/2809080296130100.jpg</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Request</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>var request = Nsurlrequest (url:url!<span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Connection</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>var session = Currentsession ()<span style="color: #0000ff;"><span style="color: #0000ff;"></span> as</span><span style="color: #000000;"><span style="color: #000000;">nsurlsession</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Download Task</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>var downloadtask =<span style="color: #000000;"><span style="color: #000000;">session.downloadtaskwithrequest (request)</span></span><span style="color: #008080;"><span style="color: #008080;">94</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">to <span style="color: #008000;">start a task by using the Resume method</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">Downloadtask.resume ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">98</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">MARK:-nsurlsessiondownloaddelegate</span></span><span style="color: #008080;"><span style="color: #008080;">101</span></span> <span style="color: #000000;"><span style="color: #000000;">func urlsession (session:nsurlsession, downloadtask:nsurlsessiondownloadtask, didfinishdownloadingtourl Location:nsurl)</span></span><span style="color: #008080;"><span style="color: #008080;">102</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">103</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">End of Download</span></span><span style="color: #008080;"><span style="color: #008080;">104</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">End of Download</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;">106</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">107</span></span> <span style="color: #000000;"><span style="color: #000000;">func urlsession (session:nsurlsession, downloadtask:nsurlsessiondownloadtask, didwritedata byteswritten:int64, t otalbyteswritten:int64, Totalbytesexpectedtowrite:int64)</span></span><span style="color: #008080;"><span style="color: #008080;">108</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">109</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get Progress</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>var written:cgfloat =<span style="color: #000000;"><span style="color: #000000;">(cgfloat) (byteswritten)</span></span><span style="color: #008080;"><span style="color: #008080;">111</span></span>var total:cgfloat =<span style="color: #000000;"><span style="color: #000000;">(cgfloat) (totalbytesexpectedtowrite)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;">113</span></span>var pro:cgfloat = written/<span style="color: #000000;"><span style="color: #000000;"></span> total</span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">down by progress: \ (pro)</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">func urlsession (session:nsurlsession, downloadtask:nsurlsessiondownloadtask, didresumeatoffset fileoffset:int , Expectedtotalbytes:int64)</span></span><span style="color: #008080;"><span style="color: #008080;">117</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">118</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">download offset, mainly used to pause the resume</span></span><span style="color: #008080;"><span style="color: #008080;">119</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;">121</span></span> <span style="color: #008080;"><span style="color: #008080;">122</span></span> <span style="color: #008080;"><span style="color: #008080;">123</span></span> <span style="color: #008080;"><span style="color: #008080;">124</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">MARK:-* * Upload files using Nsurlsessiondatatask *</span> /</span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">func sessionupload ()</span></span><span style="color: #008080;"><span style="color: #008080;">126</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">127</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">location of the uploaded address</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>var url = nsurl (<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span>:<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">http://www.iphonetrain.com/shop/data/</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">129</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Request</span></span><span style="color: #008080;"><span style="color: #008080;">131</span></span>var request = Nsurlrequest (url:url!<span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;">133</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Connection</span></span><span style="color: #008080;"><span style="color: #008080;">134</span></span>var session =<span style="color: #000000;"><span style="color: #000000;">nsurlsession.sharedsession ()</span></span><span style="color: #008080;"><span style="color: #008080;">135</span></span> <span style="color: #008080;"><span style="color: #008080;">136</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Uploading data streams</span></span><span style="color: #008080;"><span style="color: #008080;">137</span></span>Let documents:string = nshomedirectory () +<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/documents/1.png</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;">138</span></span>var imgdata =<span style="color: #000000;"><span style="color: #000000;">NSData (contentsoffile:documents)</span></span><span style="color: #008080;"><span style="color: #008080;">139</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span>var uploadtask = session.uploadtaskwithrequest (request, fromdata:imgdata, completionhandler: {var data:nsdata?, var re sponse:nsurlresponse, var error:nserror?) -Void<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #008080;"><span style="color: #008080;">141</span></span> <span style="color: #008080;"><span style="color: #008080;">142</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">after the upload is complete, judge</span></span><span style="color: #008080;"><span style="color: #008080;">143</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Upload Complete</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">144</span></span> <span style="color: #000000;"><span style="color: #000000;"> })</span></span><span style="color: #008080;"><span style="color: #008080;">145</span></span> <span style="color: #008080;"><span style="color: #008080;">146</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">to <span style="color: #008000;">start a task by using the Resume method</span></span><span style="color: #008080;"><span style="color: #008080;">147</span></span> <span style="color: #000000;"><span style="color: #000000;">Uploadtask.resume ()</span></span><span style="color: #008080;"><span style="color: #008080;">148</span></span>}</pre><p><p></p></p><p><p>iOS development--network programming Swift & (vii) nsurlsession detailed</p></p></span>