如何创建登录应用程序?

时间:2011-10-24 07:07:37

标签: iphone objective-c ios

我正在创建一个登录页面,用户可以使用相同的用户名和密码登录。

我必须使用PHP将用户提供的用户名广告密码发布到服务器。 PHP页面将验证服务器的详细信息并将答案发回给我,我必须得到答案并允许用户登录。

我已尝试过以下代码,但它无效:

NSString *firstname = Firstname.text;
NSString *lastname = Lastname.text;
NSString *post = [NSString stringWithFormat:@"fname=%@&lname=%@",firstname,lastname];
NSString *hostStr =@"http://yoursite.com/iphone/validateiphone.php";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
NSString *serverOutput = [[NSString alloc]initWithData:dataURL encoding:NSASCIIStringEncoding];

if ([serverOutput isEqualToString:@"YES"])
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Congrats" message:@"You are authorised" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
    [alert show];
    [alert release];    
}
else
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Invalid Username and password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
    [alert show];
    [alert release];
}
Firstname.text=nil;
Lastname.text=nil;

2 个答案:

答案 0 :(得分:2)

dataWithContentsOfURL没有按照您的想法行事。它不会执行HTTP请求等。它只会通过URL访问本地文件系统中的文件。因此,您实际上并未使用代码执行HTTP请求。

NSURLConnection是在iOS上执行HTTP的规范API。

NSURL *url = [NSURL URLWithString:@"http://yoursite.com/iphone/validateiphone.php"];
NSString *post = [NSString stringWithFormat:@"fname=%@&lname=%@",firstname,lastname];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[NSURLConnection connectionWithRequest:req delegate:self];

然后,您需要实现各种NSURLConnectionDelegate方法,以便在请求失败,请求完成时等时收到通知。

或者,在线程中使用同步请求。不要在主线程上执行同步请求,它会阻止你的UI。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // ....
    NSHTTPURLResponse *response = nil
    NSError *error = nil;
    NSData *responseBody = [NSURLConnection sendSynchronousRequest:req 
                                                 returningResponse:&response 
                                                             error:&error];            

});

答案 1 :(得分:1)

我真的认为你可能需要查看你的网址。只需使用Mac / PC上的浏览器验证您是否正在拨打正确的URL。 你的字符串追加给出以下

http://yoursite.com/iphone/validateiphone.phpfname="firstName"&lname="lastName"

其中“firstName”和“lastName”是您传入的参数。我认为这是一个不正确的php请求,并且您缺少查询参数或“?”。你可能需要附加一个“?”在那里的某个地方。所以也许

http://yoursite.com/iphone/validateiphone.php?fname="firstName"&lname="lastName"

然后确保您正在注销dataURL&的值。 serverOutput以确保您得到您期望的结果。

编辑:

根据您的评论,您可能无法理解我想传达的内容。测试您正在使用的URL实际上是通过将其粘贴到浏览器地址栏并查看源(如果页面上没有显示任何内容)来工作的。 其次,您应该将代码更改为如下所示,这样您就可以看到调试输出窗口中发生了什么。

 NSLog( @"URL sending Request to: %@", hostStr ); //Log the address you are setting as the url to ensure that it is correct. (You should be able to copy this into your browser and get a result if it is correct.
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
     NSLog( @"dataURL value:%@", dataURL): //this will let yo know if there is anything in, also insert a breakpoint here and verify that dataURL is not equal to 0x0 (or nil).
    NSString *serverOutput = [[NSString alloc]initWithData:dataURL encoding:NSASCIIStringEncoding];
     NSLog(@"serverOutput as NSString: %@", serverOutput);//again breakpoint this and make sure serverOutput is not 0x0.