IOS development-use MBProgressHUD to increase user experience
For a mobile client application, the user experience is very important. For example, if a long loading time causes the interface to die, MBProgressHUD can help you solve this problem. MBProgressHUD can give users a prompt during time-consuming operations such as webpage loading and downloading. The purpose of the HUD is to display the header. Today we will try to use the powerful third-party library MBProgressHUD (thanks ). MBProgressHUD At Github: https://github.com/jdg/MBProgressHUD. It contains many sample codes, which can be used as needed. However, I personally think it is a little complicated to directly use MBProgressHUD. However, I just found other great gods on the Internet (I don't know who it is, thank you) to encapsulate MBProgressHUD, which is more convenient to use, I also wrote some examples to upload to: https://github.com/chenyufeng1991/UseMBProgressHUD.
The steps for using MBProgressHUD are as follows:
(1) first, I downloaded the encapsulated MBProgressHUD library from the Internet. There are 5 files in total, and you can copy them directly from my code.
.
(2) At the same time, I made a network request and used AFNetworking to complete the GET request for the number attribution (this request may take a long time for the server to test MBProgressHUD ). We strongly recommend that you learn Cocoapods and AFnetworking. For Cocoapods and AFNetworking, refer to "iOS development-network request case summary (AFNetworking) and iOS package management tool Cocoapods installation and use.
I put a button on the interface and click the button for network requests. The Code is as follows: (Note the note ...)
It is mainly used in four methods: [MBProgressHUD showMessage], [MBProgressHUD hideHUD], [MBProgressHUD showSuccess], and [MBProgressHUD showError.
# Import ViewController. h # import MBProgressHUD + MJ. h @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(IBAction) buttonPressed :( id) sender {// The MBProgressHUD; [MBProgressHUD showMessage: @ loading...] is displayed at the beginning of the network request. AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; manager. responseSerializer = [AFHTTPResponseSerializer serializer]; // if you change it to POST, you can perform the POST request. // put the parameters to be passed directly into the URL, instead of in the dictionary. [manager GET: @ http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo? MobileCode = 18888888888 & userId = parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSString * string = [[NSString alloc] initWithData: responseObject encoding: encoding]; NSLog (@ success: % @, string); // if the network request is successful, the above MBProgressHUD "loading" is hidden; [MBProgressHUD hideHUD]; // The message "loading successful" is displayed. [MBProgressHUD showSuccess: @ loading successful];} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ failed: % @, error); // network request failed. Hide the above MBProgressHUD; [MBProgressHUD hideHUD]; // The prompt "loading failed" is displayed; [MBProgressHUD showError: @ loading failed] ;}] ;}@ end
(3) run the program with the following effect:
[Loading process]
.
[The request will be successful in a few seconds]
11.
[Network Disconnection will cause loading failure]
.
You can also modify the parameters and text as needed. It is best to view the source code, so that you can use it more freely.