我正在尝试在我的应用中设置登录屏幕,以便用户登录网站。我正在尝试使用AFNetworking,但没有太多运气。我之前从未使用过AFNetworking,所以我仍然想弄明白。
这是我尝试过的:
#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
现在它正在崩溃NSDictionary *params = @{@"username":emailAddress.text,@"password":password.text};
根据建议,我决定不使用AFNetworking。我按下“登录”按钮时发生的情况现在是:
- (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!");
}
}
这是我在尝试登录时获得的内容。
如果有任何关于如何改进这方面的建议,我会全力以赴。