Last active
December 23, 2015 12:52
-
-
Save dutran90/d82097885b9b13a438f3 to your computer and use it in GitHub Desktop.
AFNetworking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//GET | |
+(void) checkLoginWithUsername:(NSString*)username andPassword:(NSString*)password InBackground:(void(^)(BOOL success)) completionHandler{ | |
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; | |
NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:username, @"email", password, @"password", nil]; | |
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]]; | |
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"admin" password:@"admin"]; | |
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; | |
[manager GET:@"http://lottofy.gleblu.com/ws/login.php" parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
if ([[responseObject objectForKey:@"success"] doubleValue] == 0) { | |
completionHandler(NO); | |
}else{ | |
completionHandler(YES); | |
} | |
NSLog(@"JSON: %@", responseObject); | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"Error: %@", error); | |
}]; | |
} | |
//POST in Form | |
+(void)forgotPassword:(NSString *)email password:(NSString *)password InBackgroundWithBlock:(void(^)(long, NSString*, NSError*))completionHandler{ | |
NSString *urlString = [ApiString getAPIUrl:@"forgot_password.php"]; | |
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; | |
[parameters setObject:email forKey:@"email"]; | |
[parameters setObject:password forKey:@"password"]; | |
AFHTTPRequestOperationManager *afRequest = [AFHTTPRequestOperationManager manager]; | |
afRequest.requestSerializer = [AFJSONRequestSerializer serializer]; | |
afRequest.responseSerializer.acceptableContentTypes = [afRequest.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; | |
[afRequest.requestSerializer setAuthorizationHeaderFieldWithUsername:API_Username password:API_Password]; | |
[afRequest POST:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { | |
} success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSLog(@"Response object = %@", responseObject); | |
long success = [responseObject[@"success"] longValue]; | |
NSString *message = responseObject[@"message"]; | |
completionHandler(success,message,nil); | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"error = %@", error); | |
completionHandler(0,nil,error); | |
}]; | |
} | |
//UPLOAD image | |
+(void)uploadImageWithData:(UIImage *)image key:(id)key excuteFilePath:(NSString *)path compresionQuality:(float)quality success:(void (^)(NSString*, NSString *, id))success failure:(void (^)(NSString *, id))failure | |
{ | |
@autoreleasepool { | |
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970]; | |
NSString *strTimeStamp = [NSString stringWithFormat:@"%f",timeStamp]; | |
NSString *fileName = [NSString stringWithFormat:@"%@.png",strTimeStamp]; | |
/* | |
turning the image into a NSData object | |
getting the image back out of the UIImageView | |
setting the quality to 90 | |
*/ | |
NSData *imageData = UIImageJPEGRepresentation(image, 0.7); | |
// setting up the URL to post to | |
// setting up the request object now | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; | |
[request setURL:[NSURL URLWithString:path]]; | |
[request setHTTPMethod:@"POST"]; | |
/* | |
add some header info now | |
we always need a boundary when we post a file | |
also we need to set the content type | |
You might want to generate a random boundary.. this is just the same | |
as my output from wireshark on a valid html post | |
*/ | |
NSString *boundary = [NSString stringWithFormat:@"%@",@"---------------------------14737809831466499882746641449"]; | |
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; | |
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; | |
/* | |
now lets create the body of the post | |
*/ | |
NSMutableData *body = [NSMutableData data]; | |
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data;name=\"uploaded_file\"; filename=\"%@\"\r\n",fileName]; | |
[body appendData:[[NSString stringWithFormat:@"%@",contentDisposition] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[body appendData:[[NSString stringWithFormat:@"%@",@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[body appendData:[NSData dataWithData:imageData]]; | |
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
// setting the body of the post to the reqeust | |
[request setHTTPBody:body]; | |
// now lets make the connection to the web | |
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; | |
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; | |
if([returnString isEqualToString:@"error"]){ | |
failure (returnString, key); | |
}else { | |
success(fileName, returnString, key); | |
} | |
#if DEBUG | |
NSLog(@"Retrurn %@", returnString); | |
#endif | |
} | |
} | |
--> Use: [UserObject uploadImageWithData:image key:nil excuteFilePath:[ApiString getAPIUrl:@"upload_image.php"] compresionQuality:1.0 success:^(NSString* fileName, NSString *aa, id bb) {}]; | |
//LOAD IMAGE + CACHE | |
#import "UIImageView+AFNetworking.h" | |
UIImage *placeHolderProfilePicture = [UIImage imageNamed:@"ic_userprofile_default.png"]; | |
[self.imvProfile setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:postObj.user_picture]] placeholderImage:placeHolderProfilePicture success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { | |
[UIView transitionWithView:self.imvProfile | |
duration:1.0f | |
options:UIViewAnimationOptionTransitionCrossDissolve | |
animations:^{ | |
self.imvProfile.image = image; | |
} completion:NULL]; | |
// | |
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { | |
// | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment