ios异步登录,登录的是php写服务器,同理可以实现文件图片下载
2013-08-26 19:56
716 查看
在使用的viewController的.h文件里实现以下协议
<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
注:logincheck.php的内容为
<?php
$username = $_GET["us"];
$password = $_GET["ps"];
if($username =="he"&& $password =="he")
{
echo "login ok!!!";
}
else
{
echo "oh no ! you must try again!";
}
?>
//异步登录
- (IBAction)btnAsyLogin:(id)sender {
//创建url,因为此代码是在本地测试的,所以ip为127.0.0.1,服务器为Apache
NSString * s_url = [[NSString
alloc]initWithFormat:@"http://ip/logincheck.php?us=%@&ps=%@",m_tf_userName.text,m_tf_password.text];
NSURL *url = [[NSURL
alloc]initWithString:s_url];
//创建请求对象
m_data = [[NSMutableData
alloc]init];
NSURLRequest *request = [[NSURLRequest
alloc]initWithURL:url];
[[NSURLConnection
alloc]initWithRequest:request
delegate:self];
}
//接收数据,实现了NSURLConnectionDelegate协议的方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data
{
[m_data
appendData:data];
}
//数据接收完成,实现了NSURLConnectionDelegate协议的方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connectio
{
NSString *s_result = [[NSString
alloc]initWithData:m_data
encoding:NSUTF8StringEncoding];
//处理接收到的数据
//如果包含“ok”,则登录成功,反之则失败
if ([s_result rangeOfString:@"ok"].length>0)
{
UIAlertView *alert =[[UIAlertView
alloc]initWithTitle:@"login result"
message:@"login success!"
delegate:self
cancelButtonTitle:@"ok"
otherButtonTitles: nil];
[alert
show];
}
else
{
UIAlertView *alert =[[UIAlertView
alloc]initWithTitle:@"login result"
message:@"login faile!"
delegate:self
cancelButtonTitle:@"ok"
otherButtonTitles: nil];
[alert
show];
NSLog(@"login faile");
}
}
<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
注:logincheck.php的内容为
<?php
$username = $_GET["us"];
$password = $_GET["ps"];
if($username =="he"&& $password =="he")
{
echo "login ok!!!";
}
else
{
echo "oh no ! you must try again!";
}
?>
//异步登录
- (IBAction)btnAsyLogin:(id)sender {
//创建url,因为此代码是在本地测试的,所以ip为127.0.0.1,服务器为Apache
NSString * s_url = [[NSString
alloc]initWithFormat:@"http://ip/logincheck.php?us=%@&ps=%@",m_tf_userName.text,m_tf_password.text];
NSURL *url = [[NSURL
alloc]initWithString:s_url];
//创建请求对象
m_data = [[NSMutableData
alloc]init];
NSURLRequest *request = [[NSURLRequest
alloc]initWithURL:url];
[[NSURLConnection
alloc]initWithRequest:request
delegate:self];
}
//接收数据,实现了NSURLConnectionDelegate协议的方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data
{
[m_data
appendData:data];
}
//数据接收完成,实现了NSURLConnectionDelegate协议的方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connectio
{
NSString *s_result = [[NSString
alloc]initWithData:m_data
encoding:NSUTF8StringEncoding];
//处理接收到的数据
//如果包含“ok”,则登录成功,反之则失败
if ([s_result rangeOfString:@"ok"].length>0)
{
UIAlertView *alert =[[UIAlertView
alloc]initWithTitle:@"login result"
message:@"login success!"
delegate:self
cancelButtonTitle:@"ok"
otherButtonTitles: nil];
[alert
show];
}
else
{
UIAlertView *alert =[[UIAlertView
alloc]initWithTitle:@"login result"
message:@"login faile!"
delegate:self
cancelButtonTitle:@"ok"
otherButtonTitles: nil];
[alert
show];
NSLog(@"login faile");
}
}
相关文章推荐
- 使用PHP实现下载CSS文件中的图片
- IOS学习 info.plist 文件加两行代码 多线程NSThread/GCD 异步下载网络图片
- php实现图片文件与下载文件防盗链的方法
- php实现远程网络文件下载到服务器指定目录(方法一)
- 用php实现的下载css文件中的图片的代码
- php实现从服务器下载文件
- php从服务器下载文件(图片)
- iOS开发-使用AFN实现GET/POST请求,以及文件下载/文件(图片)上传
- 使用PhpMysql实现图片文件上传下载
- jsp中实现图片的下载(其他的文件也可以用)
- PHP服务器文件管理器开发小结(九):jQuery动态表单实现文件下载
- php实现远程网络文件下载到服务器指定目录(方法一)
- iOS实现七牛多图片、文件上传和下载
- php+ajax实现异步上传文件或图片功能
- php实现远程网络文件下载到服务器指定目录(方法一)
- php实现远程网络文件下载到服务器指定目录(方法二)
- php简单实现文件或图片强制下载的方法
- 用TreeView控件实现资源管理器(显示本地硬盘下所有文件夹和文件,并可以浏览图片)(示例代码下载)
- Android端通过Okhttp与PHP服务器进行交互并实现文件上传下载(二)