Sto usando AFNetworking e sto creando una richiesta di posta per la quale richiedo feedback di JSON. Il codice qui sotto funziona comunque ho due domande principali; dove posso rilasciare ActivityIndicator Manager? La seconda domanda è questo codice corretto, essendo nuovo mi confondo con i blocchi quindi voglio davvero sapere se sto facendo la cosa giusta per prestazioni ottimali, anche se funziona.AFNetworking Post Request con json feedback
NSURL *url = [NSURL URLWithString:@"mysite/user/signup"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
AFNetworkActivityIndicatorManager * newactivity = [[AFNetworkActivityIndicatorManager alloc] init];
newactivity.enabled = YES;
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
usernamestring, @"login[username]",
emailstring, @"login[email]",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"mysite/user/signup"parameters:params];
[httpClient release];
AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id json) {
NSString *status = [json valueForKey:@"status"];
if ([status isEqualToString:@"success"]) {
[username resignFirstResponder];
[email resignFirstResponder];
[self.navigationController dismissModalViewControllerAnimated:NO];
}
else {
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful"
message:@"Please try again"
delegate:NULL
cancelButtonTitle:@"OK"
otherButtonTitles:NULL];
[alert show];
[alert release];
}
}
failure:^(NSHTTPURLResponse *response, NSError *error) {
NSLog(@"%@", error);
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful"
message:@"There was a problem connecting to the network!"
delegate:NULL
cancelButtonTitle:@"OK"
otherButtonTitles:NULL];
[alert show];
[alert release];
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
NSLog(@"check");
}
La ringrazio molto per il vostro aiuto in anticipo :)
Da dove viene il 'AFJSONRequestOperation operationWithRequest: Successo: finitura:' metodo vieni? Non lo vedo nell'API. –
@reakinator In realtà dice '+ JSONRequestOperationWithRequest: success: failure:' Vedere l'esempio [qui] (https://github.com/AFNetworking/AFNetworking#readme). – borisdiakur