iphone - 使用 HTTP Post 方法如何在 iphone 中制作登录页面

标签 iphone ios objective-c

我已经创建了一个登录 View 。每次我登录时都会显示登录成功消息。即使我输入了错误的用户名和密码。我创建的登录页面是静态的。提到的链接是示例 Web 服务链接。这是我现在正在使用的方法:请给我任何想法。提前致谢。

loginPage.m

-

(IBAction)login:(id)sender

{

NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"];

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


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


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


[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"HTTP://URL"]]];


[request setHTTPMethod:@"POST"];


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


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


[request setHTTPBody:postData];


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


if (conn)

{

NSLog(@"connection successful");

}

else

{


NSLog(@"Failed");


}

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

[textField resignFirstResponder];

return YES;

}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

[receivedData setLength:0];

// NSURL *theURL=[response URL];

}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection

{

if(receivedData)

{

NSLog(@"success",[receivedData length]);

}

else

{

NSLog(@"Success",[receivedData length]);

}

}

最佳答案

NSString *string= [NSString stringWithFormat:@"your Url.php?&Username=%@&Password=%@",username,password];
        NSLog(@"%@",string);
        NSURL *url = [NSURL URLWithString:string];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];

        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
        NSLog(@"responseData: %@", responseData);
        NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"responseData: %@", str);
        NSString *str1 = @"1";
        if ([str isEqualToString:str1 ])
        {

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Successfully" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Try Again" message:@"" delegate:self cancelButtonTitle:@"Try Later" otherButtonTitles:@"Call", nil];
            alert.tag = 1;
            [alert show];
        }

不需要使用 JSON 你可以在没有 JSON 的情况下以简单的方式做到这一点!!!

关于iphone - 使用 HTTP Post 方法如何在 iphone 中制作登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18573323/

相关文章:

iphone - 在 facebook ios6 中未进入 if(granted ) 循环 "Sign in with Facebook"

iphone - Objective-c 调用 C 函数

objective-c - 以编程方式在 iOS 中忘记/删除存储的 WiFi 网络

ios - 列出重新加载动画故障

iphone - 我的新版本iOS应用程序仅在从应用程序商店加载时加载时崩溃

ios - 如何使用 CMDeviceMotion 检测我的设备移动的速度?

ios - Flurry 分析 "Table Failed to Load"错误

ios - 使用 ARC 将模态视图 Controller 的委托(delegate)设置为 nil

ios - UITextField 键击和复制/粘贴之间的区别

ios - 为collectionview添加背景图片