IPhone uses http to download files and protocol usage

Source: Internet
Author: User

ArticleThere is a demo below. As I have just been familiar with the iPhone, I have learned a few things, and I would like to ask you for advice if there are any errors or deficiencies. Thank you.
Some of this ArticleCodeIt is crawling on the Internet. Refer to the article.
Http://blog.sina.com.cn/s/blog_5ccfd2d50100u04g.html iPhone file read/write operations
Http://blog.csdn.net/rhljiayou/article/details/7616365Download iPhone HTTP files

Now, let's start the journey of this article.
1. Create a new protocol file named showmsgprotocol. h.

1 # Import<Foundation/Foundation. h>2 3 @ ProtocolShowmsgprotocol <nsobject>4 5-(Void) Showmsg :( nsstring *) MSG;6 7 @ End

2. Add an httpdownhelper class.
First look at httpdownhelper. h file

 1   # Import <Uikit/uikit. h> 2   # Import <Foundation/Foundation. h>3   # Import   "  Showmsgprotocol. h  "  4   @ Interface  Httpdownhelper: nsobject  5   {  6       //  URL  7 Nsstring * Urlstring;  8       // Down data  9 Nsmutabledata * Datanote;  10 Nsobject <showmsgprotocol> * Showmsgdelegate;  11   }  12   13 @ Property (nonatomic, retain) nsobject <showmsgprotocol> * Showmsgdelegate;  14 @ Property (nonatomic, retain) nsstring * Urlstring;  15 @ Property (nonatomic, retain) nsmutabledata * Datanote;  16   17 -( Void ) Dodownfile :( nsstring * ) Filename;  18   19   @ End 

Check the httpdownhelper. M file.

 1   # Import   "  Httpdownhelper. h  "  2   3  @ Implementation  Httpdownhelper  4   5   @ Synthesize  Urlstring, datanote;  6   @ Synthesize  Showmsgdelegate;  7   8 -( Void ) Dodownfile :( nsstring * ) Filename {  9       10 Urlstring =[[Nsstring alloc] init];  11 Datanote = [[Nsmutabledata alloc] init];  12 Urlstring = @"  Http: // 120.172.58.15: 80/test.txt  "  ;  13 Nslog ( @"  % @  "  , Urlstring );  14 Nsurl * url =[Nsurl urlwithstring: urlstring];  15 Nsurlrequest * request = [[Nsurlrequest alloc] initwithurl: url];  16 Nsurlconnection * connect = [[nsurlconnection alloc] initwithrequest: Request Delegate  : Self];  17   [Connect release];  18   [Request release];  19   20   } 21   22   //  Accept data  23 -( Void ) Connection :( nsurlconnection *) connection didreceivedata :( nsdata * ) Data {  24   [Self. datanote appenddata: Data];  25   }  26    27    //  Data accepted  28 -( Void ) Connectiondidfinishloading :( nsurlconnection * ) Connection {  29        //  Obtain path  30        //  Which path does the nsdocumentdirectory parameter obtain?  31 Nsarray * paths = Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );  32        //  Remove the required path  33 Nsstring * documentdirectory = [paths objectatindex: 0  ];  34        //  Change to the directory with the operation  35 Nsstring * Path = [documentdirectory stringbyappendingpathcomponent: @"  Test.txt  "  ];  36 Nslog ( @"  Path =% @  " , PATH );  37        38        39 Nslog ( @"  Datanote: % @  "  , [[Nsstring alloc] initwithdata: datanote encoding: nsutf8stringencoding]);  40        41   [[Nsfilemanager defaultmanager] createfileatpath: path contents: datanote attributes: Nil];  42        //  Write the downloaded data to a file. 43   [Datanote writetofile: path atomically: Yes];  44        45 Nsdata * reader = [Nsdata datawithcontentsoffile: path];  46        47 Nslog ( @"  Length = % d  "  , [READER length]);  48        49        //  Read the content of the file just written 50 Nsstring * gdata2 = [[nsstring alloc] initwithdata: [READER subdatawithrange: nsmakerange ( 0  , [READER length])] encoding: nsutf8stringencoding];  51                 52 Nslog ( @"  Gdata2 ==% @  "  , Gdata2 );  53   [Showmsgdelegate showmsg: gdata2]; // Call Protocol  54       // Lbltext. Text = gdata2;  55        56   }  57    58 -( Void ) Connection :( nsurlconnection *) connection didfailwitherror :( nserror * ) Error {  59 Uialertview * erroralert = [[uialertview alloc] initwithtitle: [error localizeddescription] Message: [error localizedfailurereason] Delegate : Nil cancelbuttontitle: @"  OK "  Otherbuttontitles: nil, nil];  60   [Erroralert show];  61   [Error release];  62   }  63   @ End 

3. Call secondviewcontroller. H on the interface.

  1  # import  
  
    2  
   # import  
   "  
    showmsgprotocol. h  
    " 
    3  
   #import  
   "  
    httpdownhelper. h  
    " 
    4  
    5  
    @ interface  secondviewcontroller: uiviewcontroller 
    
     6 -(ibaction) btndown :( 
     id  
    ) sender;  
     7  
     8  
     @ end  
   
  

Secondviewcontroller. m

 # Import   "  Secondviewcontroller. h  "  @ Interface  Secondviewcontroller ()  @ End  @ Implementation  Secondviewcontroller -( Void ) Showmsg :( nsstring * ) MSG {nslog (  @"  Showmsg =% @  " , MSG );} -(Ibaction) btndown :( ID  ) Sender {httpdownhelper * Httphepler = [[Httpdownhelper alloc] init]; [httphelper dodownfile:  @"  Test.txt  "  ];
Httphelper.Showmsgdelegate = self;}
@ End

OK,Demo download: Httpdowndemo

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.