I have a question. Maybe 2 questions.
First one relates to this piece of code:
-(void)isLoggedIn {
NSString *urlString = [NSString stringWithFormat:@"%@%@%@", kBaseURL, kAPI, kUserIsLogged];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
I would like that this method is not a void, i would like it to be returning a BOOL or something that I can use like: User is logged in > Ok do something somewhere in the app; User is not logged in > Ok prompt the user to a screen where he can introduce his credentials and log in.
Second question is related to this piece of code:
-(void)detail {
NSString *urlString = [NSString stringWithFormat:@"%@%@%@", kBaseURL, kAPI, kUser];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
I would like that this method returns me a NSArray with the parsed JSON, which i can use wherever i want to.
This methods are contained into a Model called NetworkModel (NSObject subclass). I'm just wondering why i should do everything inside the blocks. I should have pieces interacting with network all over the place, and not organized in one unique place/class. Is there a way to do that. I don't want to have 5 lines of requests every ViewController. I want to define methods i can call from any ViewController (obviously importing the header files where needed).
Any suggestions? Thank you in advice!