Getting Started with iOS network programming: Mbprogresshud Wait Indicator

Source: Internet
Author: User
Tags json


Third-party wait indicator, MBPROGRESSHUD is the waiting indicator frame provided by the third party. The following is the wait indicator style provided by Mbprogresshud, which can be divided into two main types of wait indicators: unknown end time and known end time, in which Mbprogresshud can add labels and detailed labels for the wait indicator









Mbprogresshud's download address is Https://github.com/matej/MBProgressHUD and we will download the source The MBProgressHUD.h and MBPROGRESSHUD.M in the file are copied into their projects, and the Mbprogresshud-dependent frameworks are: Foundation.framework, Uikit.framework and Coregraphics.framework, we need to add these frameworks to the project.



We add the Mbprogresshud wait indicator for the application, modify the Startrequest method code for the main view controller masterviewcontroller.m as follows, and note the bold section:


-(void) startRequest
     
{
     
    // Initialize MBProgressHUD
     
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo: self.view animated: YES];
     
    hud.mode = MBProgressHUDModeCustomView;
     
    hud.labelText = @ ”Loading”;
     
NSString * strURL = [[NSString alloc]
     
initWithFormat: @ ”http: //iosbook3/mynotes/webservice.php”];
     
NSURL * url = [NSURL URLWithString: [strURL URLEncodedString]];
     
NSString * post;
     
if (action == ACTION_QUERY) {// Query processing
     
post = [NSString stringWithFormat: @ "email =% @ & type =% @ & action =% @",
     
@ "<Your iosbook1.com user email>", @ "JSON", @ "query"];
     
} else if (action == ACTION_REMOVE) {// delete processing
     
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
     
NSMutableDictionary * dict = self.listData [indexPath.row];
     
post = [NSString stringWithFormat: @ "email =% @ & type =% @ & action =% @ & id =% @",
     
@ "<Your iosbook1.com user email>", @ "JSON", @ "remove", [dict objectForKey: @ "ID"]];
     
}
     
NSData * postData = [post dataUsingEncoding: NSUTF8StringEncoding];
     
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url];
     
[request setHTTPMethod: @ "POST"];
     
[request setHTTPBody: postData];
     
NSURLConnection * connection = [[NSURLConnection alloc]
     
initWithRequest: request delegate: self];
     
if (connection) {
     
_datas = [NSMutableData new];
     
}
     
}
     
-(void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error {
     
NSLog (@ ”% @”, [error localizedDescription]);
     
[MBProgressHUD hideHUDForView: self.view animated: YES];
     
}
     
      
     
-(void) connectionDidFinishLoading: (NSURLConnection *) connection {
     
NSLog (@ "Request completed ...");
     
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: _datas
     
options: NSJSONReadingAllowFragments error: nil];
     
if (action == ACTION_QUERY) {// Query processing
     
[self reloadView: dict];
     
} else if (action == ACTION_REMOVE) {// delete processing
     
NSString * message = @ "Operation succeeded.";
     
NSNumber * resultCodeObj = [dict objectForKey: @ "ResultCode"];
     
if ([resultCodeObj integerValue] <0) {
     
message = [resultCodeObj errorMessage];
     
}
     
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "prompt message"
     
message: message
     
delegate: nil
     
cancelButtonTitle: @ ”OK”
     
otherButtonTitles: nil];
     
[alertView show];
     
//look up again 
     
action = ACTION_QUERY;
     
[self startRequest];
     
}
     
 [MBProgressHUD hideHUDForView: self.view animated: YES];
     
} 

This article is from the "Dongsheng" blog, please be sure to keep this source http://2009315319.blog.51cto.com/701759/1174465


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.