Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>AFNetworking is awesome. Uses blocks and GCD. I have written a NetworkClient class that makes calling it fairly simple. Feel free to use it or customize it, but I don't warranty anything :)</p> <p>The API Key is something that can be removed if this doesn't fit your needs. I added it as a way to add security to all my network requests. My web services check the validity of the passed in APIKey before returning anything.</p> <p>Also, you can be fairly flexible in how you want things done. You could do sync or async requests, requests that don't warn the user on failure, etc.</p> <p>Basic Example usage:</p> <pre><code>NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"GetAllParkingLots", @"Command", nil]; [NetworkClient processURLRequestWithURL:nil andParams:params block:^(id obj) { // I check to see what kind of object is passed back, in this case I was expecting an Array if ([obj isKindOfClass:[NSArray class]]) { // Do something with the Array [self processNetworkRequestWithArray:(NSArray *)obj]; } }]; </code></pre> <p>NetworkClient.h:</p> <pre><code>// // NetworkClient.h // // Created by LJ Wilson on 2/18/12. // Copyright (c) 2012 LJ Wilson All rights reserved. // #import &lt;Foundation/Foundation.h&gt; extern NSString * const APIKey; @interface NetworkClient : NSObject +(void)processURLRequestWithURL:(NSString *)url andParams:(NSDictionary *)params block:(void (^)(id obj))block; +(void)processURLRequestWithURL:(NSString *)url andParams:(NSDictionary *)params syncRequest:(BOOL)syncRequest block:(void (^)(id obj))block; +(void)processURLRequestWithURL:(NSString *)url andParams:(NSDictionary *)params syncRequest:(BOOL)syncRequest alertUserOnFailure:(BOOL)alertUserOnFailure block:(void (^)(id obj))block; +(void)handleNetworkErrorWithError:(NSError *)error; +(void)handleNoAccessWithReason:(NSString *)reason; @end </code></pre> <p>NetworkClient.m:</p> <pre><code>// // NetworkClient.m // // Created by LJ Wilson on 2/18/12. // Copyright (c) 2012 LJ Wilson All rights reserved. // #import "NetworkClient.h" #import "AFHTTPClient.h" #import "AFHTTPRequestOperation.h" #import "SBJson.h" #warning Set APIKey or set to nil NSString * const APIKey = @"YourAPIKEYGoesHere"; @implementation NetworkClient +(void)processURLRequestWithURL:(NSString *)url andParams:(NSDictionary *)params block:(void (^)(id obj))block { [self processURLRequestWithURL:url andParams:params syncRequest:NO alertUserOnFailure:NO block:^(id obj) { block(obj); }]; } +(void)processURLRequestWithURL:(NSString *)url andParams:(NSDictionary *)params syncRequest:(BOOL)syncRequest block:(void (^)(id obj))block { [self processURLRequestWithURL:url andParams:params syncRequest:syncRequest alertUserOnFailure:NO block:^(id obj) { block(obj); }]; } +(void)processURLRequestWithURL:(NSString *)url andParams:(NSDictionary *)params syncRequest:(BOOL)syncRequest alertUserOnFailure:(BOOL)alertUserOnFailure block:(void (^)(id obj))block { #warning Fix default url // Default url goes here, pass in a nil to use it if (url == nil) { url = @"YourDefaultURLGoesHere"; } NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:params]; [dict setValue:APIKey forKey:@"APIKey"]; NSDictionary *newParams = [[NSDictionary alloc] initWithDictionary:dict]; NSURL *requestURL; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:requestURL]; NSMutableURLRequest *theRequest = [httpClient requestWithMethod:@"POST" path:url parameters:newParams]; __block NSString *responseString = @""; AFHTTPRequestOperation *_operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest]; __weak AFHTTPRequestOperation *operation = _operation; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { responseString = [operation responseString]; id retObj = [responseString JSONValue]; // Check for invalid response (No Access) if ([retObj isKindOfClass:[NSDictionary class]]) { if ([[(NSDictionary *)retObj valueForKey:@"Message"] isEqualToString:@"No Access"]) { block(nil); [self handleNoAccessWithReason:[(NSDictionary *)retObj valueForKey:@"Reason"]]; } } else if ([retObj isKindOfClass:[NSArray class]]) { if ([(NSArray *)retObj count] &gt; 0) { NSDictionary *dict = [(NSArray *)retObj objectAtIndex:0]; if ([[dict valueForKey:@"Message"] isEqualToString:@"No Access"]) { block(nil); [self handleNoAccessWithReason:[(NSDictionary *)retObj valueForKey:@"Reason"]]; } } } block(retObj); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failed with error = %@", [NSString stringWithFormat:@"[Error]:%@",error]); block(nil); if (alertUserOnFailure) { // Let the user know something went wrong [self handleNetworkErrorWithError:operation.error]; } }]; [operation start]; if (syncRequest) { // Process the request syncronously [operation waitUntilFinished]; } } +(void)handleNetworkErrorWithError:(NSError *)error { NSString *errorString = [NSString stringWithFormat:@"[Error]:%@",error]; // Standard UIAlert Syntax UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [myAlert show]; } +(void)handleNoAccessWithReason:(NSString *)reason { // Standard UIAlert Syntax UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"No Access" message:reason delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [myAlert show]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload