Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this the right way to consume a webservice with AFNetworking
    text
    copied!<p>I am currently implementing the connection from the app to my webservice, at the moment I am trying to figure out what's the right way of doing this. I decided to use the AFNetworking library for the connection and handle the JSON output via AFNetworking too. I read here, that the best way to implement such an asynchronous is the usage of a callback delegate. Here are the links to my two classes. I would really enjoy some tipps and critic on my implementation and especially what to improve.</p> <p><a href="http://pastebin.com/AUfPhD58" rel="nofollow">ViewController</a></p> <p><a href="http://pastebin.com/FQtbNdG4" rel="nofollow">WebServiceClass</a></p> <p>Code:</p> <pre><code>#import "SLLoginViewController.h" @interface SLLoginViewController () @end @implementation SLLoginViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; sharedWebService = [SLWebService sharedWebService]; [sharedWebService setDelegate:self]; [sharedWebService login:@"test@hotmail.de" withPassword:@"12345"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)requestFinished:(NSString *)name withResult:(NSDictionary *)result andError:(NSError *)error { NSLog(@"%@", result); } @end </code></pre> <p>WebService</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "AFNetworking.h" @class SLWebService; @protocol SLWebServiceDelegate &lt;NSObject&gt; @required - (void)requestFinished:(NSString *)name withResult:(NSDictionary *)result andError:(NS Error *)error; @end @interface SLWebService : NSObject { NSURL *baseURL; AFHTTPClient *client; } @property id &lt;SLWebServiceDelegate&gt; delegate; + (id)sharedWebService; - (void)login:(NSString *)email withPassword:(NSString *)password; - (NSError *)generateError:(NSString *)description domain:(NSString *)domain; @end #import "SLWebService.h" @implementation SLWebService @synthesize delegate; + (id)sharedWebService { static SLWebService *sharedWebService = nil; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ sharedWebService = [[self alloc] init]; }); return sharedWebService; } -(id)init { self = [super init]; if(self) { baseURL = [[NSURL alloc] initWithString:@"http://www.domain.de/"]; client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; } return self; } -(void)login:(NSString *)email withPassword:(NSString *)password { NSDictionary *params = @{@"email":email, @"password":password}; NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"login.php" parameters:params]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSString *result = [JSON objectForKey:@"result"]; if ([result isEqualToString:@"fail"]) { [[self delegate] requestFinished:@"login" withResult:nil andError:[self generateError:[JSON objectForKey:@"reason"] domain:@"de.Searchlight.WebServiceError"]]; } else { NSDictionary *dic = @{@"firstname":[JSON objectForKey:@"firstname"], @"lastname":[JSON objectForKey:@"lastname"], @"gender":[JSON objectForKey:@"gender"]}; [[self delegate] requestFinished:@"login" withResult:dic andError:[self generateError:[JSON objectForKey:@"reason"] domain:@"de.Searchlight.WebServiceError"]]; } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *err, id JSON) { [[self delegate]requestFinished:@"login" withResult:nil andError:err]; }]; [operation start]; } -(NSError *)generateError:(NSString *)description domain:(NSString *)domain { NSError *error; NSDictionary *userInfo = @{NSLocalizedDescriptionKey : description}; error = [NSError errorWithDomain:domain code:200 userInfo:userInfo]; return error; } @end </code></pre>
 

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