您的位置:首页 > 移动开发 > IOS开发

ios 版得用户登录编写

2013-10-17 13:19 309 查看
#import

@interface ViewController :
UIViewController

{

UITextField
*m_tf_user;//用户名

UITextField
*m_tf_pass;//用户密码

NSMutableData *m_data;
//存储返回得数据

}

@property(strong,nonatomic)IBOutlet UITextField *m_tf_user; //连接

@property(strong,nonatomic)IBOutlet UITextField
*m_tf_pass;

@property NSMutableData
*m_data;

-(IBAction)dologin:(id)sender;//同步联网登陆

-(IBAction)dologin02:(id)sender;//异步联网登陆

@end

----------------------------

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize
m_tf_user;
@synthesize m_tf_pass;
@synthesize m_data;
- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional
setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{

[super didReceiveMemoryWarning];
//
Dispose of any resources that can be recreated.
}
-(IBAction)dologin:(id)sender{
//
1.创建url

NSString *s_url=[[NSString alloc]initWithFormat:@"http://10.2.145.69/logincheck.php?us=%@&ps=%@",m_tf_user.text,m_tf_pass.text];

NSLog(@"%@",s_url);
NSURL
*url=[[NSURL alloc]initWithString:s_url];

//2.创建请求对象

NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];

//3.发送请求返回数据

NSError *err;

NSData *data=[NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:&err];

//4.处理接受到的数据
if
(data.length>0) {

NSString *s_result=[[NSString
alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",s_result);

if ([s_result
rangeOfString:@"ok"].length>0)

{

UIAlertView * alert=[[UIAlertView
alloc]initWithTitle:@"登录结果"
message:@"登陆成功"delegate:nil
cancelButtonTitle:@"知道了"
otherButtonTitles:nil, nil];

[alert show];

}

else{

UIAlertView * alert=[[UIAlertView
alloc]initWithTitle:@"登录结果"
message:@"登陆失败"delegate:nil
cancelButtonTitle:@"知道了"
otherButtonTitles:nil, nil];

[alert show];

}

}

else
{

UIAlertView *
alert=[[UIAlertView alloc]initWithTitle:@"登录结果"
message:@"登陆失败"delegate:nil
cancelButtonTitle:@"知道了"
otherButtonTitles:nil, nil];

[alert show];
}
}
-(IBAction)dologin02:(id)sender{
//
1.创建url

NSString *s_url=[[NSString alloc]initWithFormat:@"http://10.2.145.69/logincheck.php?us=%@&ps=%@",m_tf_user.text,m_tf_pass.text];

NSLog(@"%@",s_url);
NSURL
*url=[[NSURL alloc]initWithString:s_url];

//2.创建请求对象

NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];

//3.发送数据请求

m_data=[[NSMutableData alloc]init];

[[NSURLConnection alloc]initWithRequest:request delegate:self];

}

//接收数据
- (void)connection:(NSURLConnection
*)connection didReceiveData:(NSData *)data{
[
m_data appendData:data];

}

//数据接受完成
-
(void)connectionDidFinishLoading:(NSURLConnection *)connection{

printf("data recive OK");

NSString *s_result=[[NSString alloc]initWithData:m_data
encoding:NSUTF8StringEncoding];

UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"登录结果"
message:s_result delegate:nil cancelButtonTitle:@"知道了"
otherButtonTitles:nil, nil];

[alert show];
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: