A wrapper around AFNetworking with an auto loading HUD.
ILHTTPClient
is a thin wrapper around AFNetworking
that automatically fades in and out an MBProgressHUD
HUD for each request made. It subclasses AFHTTPClient
, and exposes several easy-to-use methods to make each of the four verbs in HTTP requests: GET, POST, DELETE and PUT.
Responses returned by the HTTP requests made are in NSString
plain text form, and can be converted to JSON objects (if the context is appropriate), using a built-in method -JSONValue
on the NSString
. Currently, only plain-text and JSON formats are supported.
A demo project ILHTTPClientDemo
is included to show how ILHTTPClient
can be integrated into a project.
-
Copy all files in the "Files to include" folder into your Xcode project. Be sure to check "Copy items into destination's group folder". These files include:
37x-Checkmark.png
[email protected]
ILHTTPClient.h
ILHTTPClient.m
MBProgressHUD+CustomAdditions.h
MBProgressHUD+CustomAdditions.m
-
Include the
AFNetworking
library into your project by downloading the latest version, and copying only theAFNetworking
folder into your project. -
Include the
MBProgressHUD
library into your project by downloading the latest version, and copying the 2 filesMBProgressHUD.h
andMBProgressHUD.m
into your project. -
Add the
SystemConfiguration
andMobileCoreServices
framework to your project by clicking on your project's name at the top of the sidebar in Xcode, then going into "Build Phases". In this tab, expand "Link Binaries With Libraries" and addSystemConfiguration.framework
. Do the same forMobileCoreServices.framework
.AFNetworking
requires these two frameworks. -
Add the line
#include "ILHTTPClient.h"
to the interface of the view controller that you wish to useILHTTPClient
in. If you intend to useILHTTPClient
in multiple view controllers, add the above include line to theYourAppName-Prefix.pch
file in the "Supporting Files" group. This way,ILHTTPClient
will be available to every file in your project without needing to keep adding a#include
. -
Also, in the
YourAppName-Prefix.pch
file, add:#import <SystemConfiguration/SystemConfiguration.h> #import <MobileCoreServices/MobileCoreServices.h>
-
In the
-viewDidLoad:
of the view controller, initialize either a local or global ivar by calling:ILHTTPClient *client = [ILHTTPClient clientWithBaseURL:<Base URL here> showingHUDInView:<View here>];
And assign the result of this method call to the ivar client. Note that the view given should be the one you want to display the HUD in (usually
self.view
). The client only needs to be initialized once per set of requests with the same base URL.
-
Use the following method.
params
is an NSDictionary containing the key-value pairs of any parameters needed to be passed into the request.loadingText
is the text to show in the HUD while the request is executing.sucessText
is the text to show in the HUD when the request completes successfully. If nil, the HUD will just fade away. If non-nil, a checkmark will be shown along with the text.success
andfailure
are the callback blocks.[client getPath:path parameters:params loadingText:@"Loading" successText:@"Completed!" success:^(AFHTTPRequestOperation *operation, NSString *response) { id JSON = [response JSONValue]; //If JSON is returned /* Do something with this data */ } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); /* Handle the error here */ }];
-
Use the following method. The parameters are all the same as for the GET request, except for
multiPartForm
. In this block, use the methods declared inAFHTTPClient.h
to compose theAFMultipartFormData
object. One such example is to use-appendPartWithFileData:name:
to compose the POST request body.[client postPath:path parameters:params loadingText:@"Submitting" successText:@"Submitted!" multiPartForm:^(id<AFMultipartFormData> formData) { /* Compose POST request body here */ } success:^(AFHTTPRequestOperation *operation, NSString *response) { id JSON = [response JSONValue]; //If JSON is returned /* Do something with this data */ } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); /* Handle the error here */ }];
The same technique as demonstrated above goes for the DELETE and PUT operations, except using the -deletePath:…
and putPath:…
methods respectively. There is a range of other methods that can be used to interact with the HTTP operation, but I won't go into detail here.
- ARC
- iOS 5.0 or later
- The
AFNetworking
library - The
MBProgressHUD
library - The
SystemConfiguration
framework - The
MobileCoreServcies
framework
Isaac Lim
isaacl.net
1.11
- Fixed reference to
self
within a block which could lead to a retain cycle.
1.1
- Edited methods to integrate the
successText
parameter. This ties in the additions I made toMBProgressHUD
inMBProgressHUD+CustomAdditions.h
. Note: This breaks the methods in v1.0. To fix, add thesuccessText
parameter to each request call.
1.0
- First publish to Github
ILHTTPClient is distributed under the terms and conditions of the MIT license.
Copyright (c) 2013 isaacl.net. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.