Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>AFNetworking is a great choice! I'd advise to use blocks, keeping the login request &amp; result handling together. It would also simplify and shorten the code.</p> <p>Then, like the AFNetworking's <a href="https://github.com/AFNetworking/AFNetworking/wiki/Getting-Started-with-AFNetworking" rel="nofollow">Getting Started</a> at the end suggests, it's a good idea to subclass <code>AFHTTPCLIENT</code>.</p> <p>General remark: As the marvellous <a href="http://www.amazon.de/Objective-C-Phrasebook-Developers-Library-Chisnall/dp/0321813758/ref=sr_1_3?ie=UTF8&amp;qid=1357947485&amp;sr=8-3" rel="nofollow">Objective-C Phrasebook</a> suggests, a singleton can be created without GCD using the language's <code>+initialize</code>. And the shared variable generally is a static, instead of an instance variable.</p> <p>The first part would look like:</p> <pre><code>@class SLWebService; @interface SLWebService : AFHTTPClient + (SlWebService)sharedWebService; - (void)login:(NSString*)email password:(NSString*)password success:(void (^)(AFHTTPRequestOperation* operation, NSDictionary* response))success failure:(void (^)(AFHTTPRequestOperation* operation, NSError* error))failure; @end #import "SLWebService.h" @implementation SLWebService static SLWebService* sharedWebService; + (void)initialize { if ([SLWebService class] == self) { sharedWebService = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.domain.de/"]]; // Let AFNetworking do conversion to/from JSON. [sharedWebService setParameterEncoding:AFJSONParameterEncoding]; [sharedWebService setDefaultHeader:@"Accept" value:@"application/json"]; [sharedWebService registerHTTPOperationClass:[AFJSONRequestOperation class]]; } } + (id)allocWithZone:(NSZone*)zone { if (sharedWebService &amp;&amp; [SLWebService class] == self) { [NSException raise:NSGenericException format:@"Duplicate SLWebService singleton creation"]; } return [super allocWithZone:zone]; } + (SLWebService*)sharedWebService { return sharedWebService; } ... </code></pre> <p>As a final off-topic remark: I don't like Apple's/Xcode's suggestion to use <code>-initWithNibName::</code> for view controllers, because this keeps the XIB name outside the class. You almost never need to initialise with a different XIB, therefore I'd rather specify the XIB inside '-init'. Like so:</p> <pre><code>- (id)init { if (self = [super initWithNibName:@"SLLoginView" bundle:nil]) { } return self; } </code></pre> <p>Cheers, hope this helps!</p>
 

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