在应用内登录在线帐户

时间:2014-09-18 13:49:59

标签: ios xcode afnetworking-2

我很难理解如何创建一个允许用户在网站上登录其在线帐户的应用程序。我的例子是登录:https://account.tfl.gov.uk/oyster/

现在我已经阅读了很多关于AFNetworking,HTTP POST请求等的内容,但我对这一切与我上面的示例网站登录的相关性(如果有的话)非常困惑。

关于登录方式的任何建议都会很棒,因为我完全迷失了。

1 个答案:

答案 0 :(得分:0)

使用以下方式:

NSString *post = [NSString stringWithFormat:@"Username=%@&Password=%@",@"username",@"password"];  //may be change as per your requirement

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

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

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yourDoamin.com/test/xyz.aspx"]]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:postData];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

if(connection) {
    NSLog(@"Connected");
} else {
    NSLog(@"Connection Error");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
    // here check response data in your case login password true or false
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   // handle connection error
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}