0

I am trying to set up a login screen in my app for users to log into a website. I'm trying to use AFNetworking but not having much luck. I've never used AFNetworking before so I'm still trying to figure it out.

Here is what I've tried:

#import "KFBLoginScreen.h"
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFURLResponseSerialization.h"
#import "AFURLRequestSerialization.h"

@interface KFBLoginScreen ()
{
    AFHTTPRequestOperationManager *manager;
}

@end

@implementation KFBLoginScreen
@synthesize emailAddress, password, login, forgotPassword, createAccount, baseURL;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    baseURL = [NSURL URLWithString:@"http://www.my.kyfb.com"];

    emailAddress.delegate = self;
    password.delegate = self;

    manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:baseURL];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

- (IBAction)signIn
{
    // Login information from UITextFields
    NSDictionary *params = @{@"username":emailAddress.text,@"password":password.text};

    [manager POST:@"/login" parameters:params
    success:^(AFHTTPRequestOperation *operation, id success)
    {
        NSLog(@"Success!");
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        NSLog(@"Failure!");
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Right now it's crashing on NSDictionary *params = @{@"username":emailAddress.text,@"password":password.text};

From a suggestion, I decided to not use AFNetworking. My method for what happens when the "Login" button is pressed is now:

   - (IBAction)signIn
{
    NSString *email = emailAddress.text;
    NSString *passwordString = password.text;

    NSString *encodedEmail = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)email,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 ));
    NSString *encodedPassword = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)passwordString,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 ));

    NSString *post = [NSString stringWithFormat:@"email=%@&password=%@", encodedEmail, encodedPassword];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"https://www.my.kyfb.com/"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLResponse *response;
    NSError *error;

    NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;

    if (error)
    {
        NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);

        UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"" message:error.localizedDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
    }
    else
    {
        NSLog(@"Login Successful!");
    }
}

Here is what I'm getting when trying to log in. enter image description here

If there are any suggestions on how to improve upon this, I'm all ears.

raginggoat
  • 3,570
  • 10
  • 48
  • 108
  • 1
    Any reason why you're using AFNetworking instead of the native API calls? There was a decent reason to use it in the past, but the APIs now are robust enough that I'm not sure the dependency on a 3rd party library is worth it… – Axeva Feb 11 '14 at 13:36
  • Examples of a POST command: [iOS: how to perform an HTTP POST request?](http://stackoverflow.com/questions/5537297/ios-how-to-perform-an-http-post-request) – Axeva Feb 11 '14 at 13:37
  • It's mostly because when I was researching how to accomplish what I'm wanting, everyone was using AFNetworking. – raginggoat Feb 11 '14 at 13:38
  • @Axeva, I've taken your suggestion and updated my post. – raginggoat Feb 11 '14 at 15:31
  • Great, so is your code working now? Are you hitting errors, or just looking for advice on how to improve the code? – Axeva Feb 11 '14 at 15:47
  • It's working. I'm curious how it could be improved and I'm guessing I could open a web view with the url for the logged in account where "Login Successful" and open it like any other web view? – raginggoat Feb 11 '14 at 16:16
  • @Axeva, see my updated info. – raginggoat Feb 12 '14 at 16:12

0 Answers0