iOS Nsurlcache, ASI settings cache and usage steps

Source: Internet
Author: User

1 a Nsurlcache2 steps to use the cache3 //get a global cache object4Nsurlcache *cache =[Nsurlcache Sharedurlcache];5 6 //Set Cache capacity7Cache.memorycapacity =1024x768*1024x768;8Cache.diskcapacity = -*1024x768*1024x768;9 Ten //set the requested cache policy OneRequest.cachepolicy =Nsurlrequestreturncachedataelseload; A  - Second, ASI - 1. How to use the cache the 1>Caching Individual requests - //1. Get a global cache object (determines where the cache is stored and where to store it) -Asidownloadcache *cache =[Asidownloadcache Sharedcache]; - //Set the default cache load policy +Cache.defaultcachepolicy =Asidonotreadfromcachecachepolicy; -  + //2. Set the cache object for the request object (which cache object to use) ARequest.downloadcache =Cache; at  - //3. Set the cache load policy for the request object -Request.cachepolicy =Asionlyloadifnotcachedcachepolicy; - //send a request if there is no cache -  - //4. Set the cache storage policy for the request object (length of storage) inRequest.cachestoragepolicy =Asicachepermanentlycachestoragepolicy; - //Permanent Storage to  +Note the priority of the cache load policy: request.cachepolicy >Cache.defaultcachepolicy -  the 2>Cache all requests * //1. Get a global cache object (determines where the cache is stored and where to store it) $Asidownloadcache *cache =[Asidownloadcache Sharedcache];Panax Notoginseng //Set the default cache load policy -Cache.defaultcachepolicy =Asionlyloadifnotcachedcachepolicy; the  + //2. Setting the global cache object A [ASIHTTPRequest Setdefaultcache:cache]; the  + 2. Send a request - 1>Sync Request $ [Request startsynchronous]; $  - 2>Asynchronous Request - [Request startasynchronous]; the  - 3. Get\postWuyi 1>GET Request theASIHTTPRequest *request =[ASIHTTPRequest Requestwithurl:url]; -  Wu 2>POST Request -Asiformdatarequest *request =[Asiformdatarequest Requestwithurl:url]; About //add normal parameter (non-file parameter) $[Request Setpostvalue:@"Zhangsan"Forkey:@"username"]; -[Request Setpostvalue:@"123"Forkey:@"pwd"]; -  - 4. File Download A //the path to where files are stored (where files are downloaded) +Request.downloaddestinationpath =filepath; the //set up Download agent (listen for download progress) -Request.downloadprogressdelegate =Self.circleview; $ //Support Breakpoint Download theRequest.allowresumeforfiledownloads =YES; the  the 5. File Upload the //Add File parameter (file: path to upload files) -[Request Setfile:file Forkey:@"file"]; in[Request Setfile:file Withfilename:@"123.txt"Andcontenttype:@"Text/plain"Forkey:@"file"]; the[Request Setdata:data Withfilename:@"Minion.png"Andcontenttype:@"Image/png"Forkey:@"file"]; the  About //set up upload agent (listen for upload progress) theRequest.uploadprogressdelegate =Self.circleview; the  the 6. The process of listening for requests + 1>Proxy Method - //Set up proxy theRequest.Delegate=Self ;Bayi //Compliance Agreement the asihttprequestdelegate the //implementing proxy methods in the Protocol -- (void) requeststarted: (ASIHTTPRequest *) request; -- (voidRequest: (ASIHTTPRequest *) Request Didreceivedata: (NSData *) Data the- (void) requestfinished: (ASIHTTPRequest *) request; the- (void) requestfailed: (ASIHTTPRequest *) request; the  the 2>SEL - //Set up proxy theRequest.Delegate=Self ; the //Set method Name the[Request Setdidstartselector: @selector (start)];//starts sending the request, the proxy's Start method is invoked94 // .... the  the 3>Block the[Request setstartedblock:^{98NSLog (@"setstartedblock----"); About }]; - 101[Request setdatareceivedblock:^ (NSData *data) {102NSLog (@"setdatareceivedblock----");103 }];104  the[Request setcompletionblock:^{106NSLog (@"setcompletionblock----");107 }];108 109[Self setfailedblock:^{ theNSLog (@"setfailedblock----");111 }]; the 113 7get the server's response through the request object the 1>Get response header information the@property (Atomic, retain) nsdictionary *responseheaders; the 117 2>Get the response Body (entity content)118-(NSData *) responsedata;//returns binary data directly to the server119-(NSString *) responsestring;//Convert binary data to strings (easy debugging)

iOS Nsurlcache, ASI settings cache and usage steps

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.