TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jul 2015 |
Maintained by Jamie Evans.
Depends on: | |
NSJSONSerialization-NSNullRemoval | ~> 1.0 |
Reachability | ~> 3.2 |
To run the example project, clone the repo, and run pod install
from the Example directory first.
To use the library, start by importing the client
#import <NetworkSimple/NSClient.h>
To use the client, you'll have to start out with an instance
NSClient *client = [NSClient clientWithScheme:@"http" andHost:@"randomuser.me"];
To make a simple URL request, you can perform the following
[client sendRequestWithEndpoint:@""
httpMethodType:NSHTTPMethodTypeGet
requestType:NSRequestTypeURL
dataObject:nil
requestMutationBlock:nil
andCallback:^(NSUInteger statusCode, id responseObject, NSError *error)
{
if(statusCode == 200)
{
// Received users!
// responseObject should contain an NSArray of user objects
}
else if(error)
{
// Display alert with error message
}
}];
For a more complicated request, lets send a POST request with JSON body data. We'll also set an HTTP header in the requestMutationBlock - this is always fired last in the request generation.
[client sendRequestWithEndpoint:@""
httpMethodType:NSHTTPMethodTypePost
requestType:NSRequestTypeJSON
dataObject:@{@"firstName" : @"Jamie",
@"lastName" : @"Evans",
@"phoneNumber" : @"555-555-5555"}
requestMutationBlock:^(NSMutableURLRequest *request)
{
[request setValue:@"cbeiqu829fPamfr12adkjln" forHTTPHeaderField:@"client_token"];
[request setTimeoutInterval:10.0f];
}
andCallback:^(NSUInteger statusCode, id responseObject, NSError *error)
{
if(statusCode == 200)
{
// Created a user successfully!
}
else if(error)
{
// Failed to create a user :(
}
}];
We can also update the default timeout intervals for our requests
[client setRequestTimeout:10.0f];
Or set the request caching policy
[client setCachingPolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
And if you're using multipart requests, you'll want to set the boundary string
[client setBoundaryString:@"myBoundaryStringForThisApplication"];
All of these properties can be overridden in the requestMutationBlock.
Jamie Evans, [email protected]
NetworkSimple is available under the MIT license. See the LICENSE file for more info.