// JDURLConnection.h
typedef void (^successBlock)(NSDictionary *dict);
typedef void (^errorBlock)(NSString *error);
@interface JDURLConnection : NSObject
+(void) urlRequestWithURL:(NSString *)urlStr
usingMethod:(NSString *)verb
andParameters:(NSDictionary *) param
completionBlock:(successBlock)completed
errorBlock:(errorBlock)errored;
@end
// JDURLConnection.m
+ (void) showMessageString:(NSString *)message{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"App Name!" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
#pragma mark - RequestMethods
+ (NSMutableURLRequest *) URLRequestFrom:(NSURL *)url verb:(NSString *)verb parameters:(NSDictionary*)parameters{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:30];
[request setHTTPMethod:verb];
if (parameters != nil) {
NSData *jsonData = [[NSString stringWithFormat:@"data=%@",[parameters JSONRepresentation]] dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:jsonData];
}
return request;
}
+(void) urlRequestWithURL:(NSString *)urlStr usingMethod:(NSString *)verb andParameters:(NSDictionary *) param completionBlock:(successBlock)completed errorBlock:(errorBlock)errored{
// [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [self URLRequestFrom:url verb:verb parameters:param];
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// [SVProgressHUD dismiss];
if ([data length] > 0 && error == nil){
id responseData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",responseData);
NSDictionary *responseDict = nil;
if ([responseData isKindOfClass:[NSDictionary class]])
responseDict = (NSDictionary *)responseData;
else if ([responseData isKindOfClass:[NSArray class]])
responseDict = [NSDictionary dictionaryWithObject:responseData forKey:@"result"];
else
responseDict = [NSDictionary dictionaryWithObject:@"" forKey:@"error"];
completed(responseDict);
}
else if ([data length] == 0 && error == nil){
[self performSelectorOnMainThread:@selector(showMessageString:) withObject:@"Error! unable to receive the data from Server." waitUntilDone:YES];
errored(@"Error! unable to receive the data from Server.");
}
else if (error != nil){
[self performSelectorOnMainThread:@selector(showMessageString:) withObject:[error localizedDescription] waitUntilDone:YES];
errored([error localizedDescription]);
}
}];
}
これを次のように実装します。
NSArray *keys = [NSArray arrayWithObjects:@"username",@"password", nil];
NSArray *values = [NSArray arrayWithObjects:self.tfUserName.text,self.tfUserPass.text,nil];
NSDictionary *param = [NSDictionary dictionaryWithObjects:values forKeys:keys];
[JDURLConnection urlRequestWithURL:[NSString stringWithFormat:@"http://yourwebURL/API/user-login"] usingMethod:@"POST" andParameters:param completionBlock:^(NSDictionary *dict)
{
if ([dict objectForKey:@"result"]) {
NSLog(@"%@",dict);
}
}
errorBlock:^(NSString *error)
{
NSLog(@"%@",error);
// errored(error);
}];