IOS-use of simple network nsurlconnection

Source: Internet
Author: User
# Import <Foundation/Foundation. h> @ Class myconnection; // download protocol @ protocol myconnectiondelegate <nsobject> // download completed-(void) myconnectiondidfinish :( myconnection *) con data :( nsdata *) data; // download failed-(void) myconnectiondidfail :( myconnection *) con; @ end @ interface myconnection: nsobject <timeout >{ nsmutabledata * imagedata;}-(ID) initwithurl :( nsstring *) URL delegate :( ID) Delegate; @ property (nonatomic, assign) ID <myconnectiondelegate> delegate; @ property (nonatomic, retain) nsstring * URL; @ property (nonatomic, assign) int tag; @ end # import "myconnection. H "# import" nsstring + hashing. H "@ implementation myconnection @ synthesize url = _ URL; @ synthesize delegate = _ delegate; @ synthesize tag = _ Tag;-(ID) initwithurl :( nsstring *) URL delegate :( ID) delegate {If (Self = [Super init]) {self. delegate = delegate; self. url = URL; imagedata = [[nsmutabledata alloc] init]; nsurlrequest * request = [nsurlrequest requestwithurl: [nsurl failed: url] cachepolicy: nsurlrequestreloadignoringlocalcachedata timeoutinterval: 30.0]; [nsurlconnection connectionwithrequest: request delegate: Self];} return self;}-(void) connection :( nsurlconnection *) connection didreceiveresponse :( nsurlresponse *) response {[uiapplication sharedapplication]. response = yes;}-(void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) Data {[imagedata appenddata: Data];}-(void) connectiondidfinishloading :( nsurlconnection *) connection {[uiapplication sharedapplication]. networkactivityindicatorvisible = no; // cache // http: // 192.168.88.8/wer/1.png // MD5 base64 // var/user/24234234/tmp/kjhaksdf87asd7f8a7sd8f78as7df // http: // 192.168.88.8/2.png // base64 // aahttp: // www.baidu.com/1.rarzz // thrund: // Signature = nsstring * Path = nshomedirectory (); Path = [nsstring stringwithformat: @ "% @/tmp/% @", path, [self. URL md5hash]; [imagedata writetofile: path atomically: No]; // use the downloaded proxy [_ delegate myconnectiondidfinish: Self data: imagedata];}-(void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) error {[uiapplication sharedapplication]. networkactivityindicatorvisible = no; // use the failed download proxy [_ delegate myconnectiondidfail: Self];}-(void) dealloc {[imagedata release]; self. url = nil; [Super dealloc] ;}@ end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.