如何从Xcode(objective c)向php发送和接收数据
我试图将用户名和密码从xcode发送到我制作的php文件中。由于我对xcode和php都是新手,而且对于我正在制作的简单应用程序来说,用户名和密码是预定义的123456。我在将用户名和密码发送到php站点并收到响应时遇到了很多问题。当我的代码进行通信并建立连接时,它不会读取php文件中回显的变量。相反,它获取页面的整个源代码。我的代码如下: 目标C代码: //我已经设置了一个警报视图,并设置了两个按钮continue=buttonIndex==0和cancel=buttonIndex==1如何从Xcode(objective c)向php发送和接收数据,php,ios,objective-c,xcode,nsurlrequest,Php,Ios,Objective C,Xcode,Nsurlrequest,我试图将用户名和密码从xcode发送到我制作的php文件中。由于我对xcode和php都是新手,而且对于我正在制作的简单应用程序来说,用户名和密码是预定义的123456。我在将用户名和密码发送到php站点并收到响应时遇到了很多问题。当我的代码进行通信并建立连接时,它不会读取php文件中回显的变量。相反,它获取页面的整个源代码。我的代码如下: 目标C代码: //我已经设置了一个警报视图,并设置了两个按钮continue=buttonIndex==0和cancel=buttonIndex==1
// The user continues from the alert view
if(buttonIndex==0)
{
NSString* username = @"123456";
NSString* pass = @"123456";
// I combine the two strings to send to the php page, and insert a : between the two strings
NSString* combined = [NSString stringWithFormat:@"%@:%@", username, pass];
NSString *post = combined;
// Now I attempt to post the data to the php page
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://www.sulaimanmajeed.com/login.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
}
}
}
现在,我还有其他方法需要在发布帖子时调用:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
// NSLog(loginStatus);
dataOutput.text = loginStatus;
[loginStatus release];
[connection release];
[webData release];
}
我的php代码非常简单:
<?php
$usernameEntered = $_GET['user']; $passwordEntered = $_GET['pass']; $full = 'Choose';
$username = array(
"123456", "234567", "345678", "456789" );
$password = array(
"123456", "234567", "345678", "456789" );
$accounts = array(
"Chequings-874576",
"Savings-793756",
"Visa-018375",
"Mastercard-375836" );
$b = 0; $elementsInArray = count($accounts);
$isTrue = "FALSE";
if (($usernameEntered== $username[0]) && ($passwordEntered == $password[0])) {
$isTrue = "TRUE"; } elseif ((usernameEntered == $username[1]) && ($passwordEntered == $password[1])) {
$isTrue = "TRUE"; } elseif ((usernameEntered == $username[2]) && ($passwordEntered == $password[2])) {
$isTrue = "TRUE"; } elseif ((usernameEntered == $username[3]) && ($passwordEntered == $password[3])) {
$isTrue = "TRUE"; } else {
$isTrue = "FALSE"; }
if( $isTrue == "TRUE" ) { for ($i=0; $i < $elementsInArray; $i++) { $b = $i; $full = $full . ':' . $accounts[$b] ; }
echo $full;
}
if( $isTrue == "FALSE" ) {
echo "error:error 2:test 1:test 2:test 3";
}
?>
因此,dataOutput.text应该返回一个真值,因为我提供的用户名和密码是默认的123456。但是,dataOutput.text仅返回网站的整个源代码。有人能帮我找出为什么它不能输出true,以及我的代码哪里出错了吗?我已经为此工作了两天了,我被难住了。先谢谢你
附言
我在dataOutput中收到的输出是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!-- HTML generated by Network Solutions Website Builder --><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Sulaiman Majeed login</title><meta name="robots" content="index, follow" /><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" /><meta name="description" content="Welcome to my website"><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="pragma" content="no-cache"/><meta http-equiv="expires" content="-1"/><script type="text/javascript" src="/scripts/user.js"></script><script type="text/javascript" src="/scripts/jquery.js"></script><script type="text/javascript" src="/scripts/simplegallery.js"></script><script type="text/javascript" src="/scripts/photo_album.js"></script><link type="text/css" href="/scripts/PrebuiltBlockStructure.css" media="screen" rel="stylesheet"><link rel="stylesheet" media="screen" href="/scripts/template.css" id="wb-template.css" /><link rel="stylesheet" media="all" href="/scripts/layout3.css" id="wb-layout3.css" /><link rel="stylesheet" media="all" href="/scripts/website.css" id="wb-website.css" /><link rel="stylesheet" media="print" href="/scripts/print.css" id="wb-print.css" /><link rel="stylesheet" media="all" href="/scripts/12custom.css" id="wb-custom.css" /><link rel="stylesheet" media="all" href="/scripts/icwebsiteelement.css" id="wb-icwebsiteelement.css" /><link rel="stylesheet" media="all" href="/share/scripts/PrebuiltBlockStructure.css" id="wb-/share/scripts/PrebuiltBlockStructure.css" /></head><body onload="initClientMenu();sizeColumns();"><!-- Start content section --><div id="wb-container-outer"> <div id="wb-container-inner"> <div id="wb-layout"> <div id="wb-layout-inner"> <div id="wb-layout-top-header"> <div id="wb-navigation-subtop-header"> </div> <div id="wb-top-subbasenav"></div> </div> <div id="wb-header-nav"> <div id="wb-navigation-top-header"> <div id="wb-navigation-bottom-header"> </div> </div> </div> <div id="wb-layout-header"> <div id="wb-layout-header-mask" class="wb_region"> <table id="wb_con__42340" class="wb_con" cellpadding="0" cellspacing="0" width="100%"> <tr> <td id="wb_col__45678" class="wb_col" width="50%" valign="top" style=""> </td><td id="wb_col__5673453" class="wb_col" width="50%" valign="top" style=""></td> </tr> </table> </div> </div> <div id="wb-layout-header-base"></div> <div id="wb-layout-top-2"> <div id="wb-navigation-subtop"> <div><a href="home.html" class="tertiary" target="_self"><h3>Home</h3></a></div> <div><a href="resume.html" class="tertiary" target="_self"><h3>Resume</h3></a></div> </div> <div id="wb-top-subbasenav"></div> </div> <div id="wb-container-contentwrapper"> <div id="wb-layout-sidebar"> <div id="wb-navigation"> <div id="wb-navigation-top"> <div id="wb-navigation-bottom"> <div><a href="home.html" class="tertiary" target="_self"><h3>Home</h3></a></div> <div><a href="resume.html" class="tertiary" target="_self"><h3>Resume</h3></a></div> </div> </div> </div> <div id="wb-basenav"></div> <div id="wb-navbasewrap" class="wb_region"> <div class="wb_dropzone_y"> </div> <table id="dz-tablewb_element_text__02773140" class="dz-table" cellspacing="0" cellpadding="0" width="100%"> <tr> <td align="left"> <div id="wb_element_text__02773140" class="ic-element"> <div></div> </div> </td> </tr> </table> </div> </div> <div id="wb-layout-main"> <div id="wb-layout-main-inner-1"> <div id="wb-layout-main-inner-2" class="wb_region wb-text-content"> <table id="wb_cl__12342346" cellpadding="0" cellspacing="0" class="" width="100%"> <tr> <td> <table id="wb_con__056756794678680" class="wb_con" cellpadding="0" cellspacing="0" width="100%"> <tr> <td id="wb_col__089563564560" class="wb_col" width="100%" valign="top" style=""> </td> </tr> </table> <table id="wb_con__1231213564678" class="wb_con" cellpadding="0" cellspacing="0" width="100%"> <tr> <td id="wb_col__335786456" class="wb_col" width="70%" valign="top" style=""> <table id="dz-tablewb_element_php__1358289049875_15" class="dz-table" cellspacing="0" cellpadding="0" width="100%"> <tr> <td align="left"> <div id="wb_element_php__1358289049875_15" class="ic-element"><php? ; ?></div> </td> </tr> </table> </td><td id="wb_col__234456134" class="wb_col" width="30%" valign="top" style=""> </td> </tr> </table> <table id="wb_con__5675679467868" class="wb_con" cellpadding="0" cellspacing="0" width="100%"> <tr> <td id="wb_col__8956356456" class="wb_col" width="100%" valign="top" style=""> </td> </tr> </table> </td> </tr> </table> </div> </div> </div> <div style="clear: both"></div> </div> <div id="wb-layout-footer"> <div id="wb-layout-footer-inner-1" class="wb_region"> <div class="wb_dropzone_y"> </div> </div> </div> </div> </div> </div> </div><table border="0" width="100%" cellpadding="0" cellspacing="0" id="other_footer"> <tr> <td align="left" valign="middle"><p style="font-size: 10px; font-style: normal; font-family: arial,verdana,sans-serif; font-style: normal;"><a style="text-decoration: none; font-size: 10px; font-style: normal; font-family: arial,verdana,sans-serif; font-style: normal;" title="Web hosting" href="http://ads.networksolutions.com/landing?code=P61C100S1N0B142A1D255E0000V100">Web Hosting</a> powered by Network Solutions®</p></td> </tr> </table> <!-- End content section --><!-- Start nsWebstats code. Copyright 2010 networksolutions.com More info available at http://networksolutions.com --><script type="text/javascript">var pageName="login"; document.write(unescape('%3Cscript src="'+(location.href.split(':')[0] =='https'?'https':'http')+'://stats.netsolads.com/jsconfig?pid=288656091%26mg=all%26lc=1%26pe=1"%3E%3C/script%3E'));</script><!--End nsWebStats tracking code. --></body></html>
我相信有更好的方法可以做到这一点,但您可以在UIWebView中加载php文件,然后运行脚本,然后,当您想要检索变量时,可以使用php打印它们,然后使用javascript检索它们,并使用其他人在这里提到的方法将变量从javascript传输到objective-c:如果这里有人对答案感兴趣,对于一些琐碎的数据发送来说,这有点过分了,我建议您看看ActiveMQ。您可以设置服务器,并且您的iOS设备可以订阅服务器上的队列,以识别其他客户端是否在队列中发送数据
关于您是否使用wordpress插件或其他可能干扰php文件的php文件运行php类
<script type="text/javascript">var pageName="token"; document.write(unescape('%3Cscript src="'+(location.href.split(':')[0] =='https'?'https':'http')+'://stats.netsolads.com/jsconfig?pid=288656091%26mg=all%26lc=1%26pe=1"%3E%3C/script%3E'));</script><!--End nsWebStats tracking code. -->
如果粘贴的是PHP代码,则会出现严重的语法问题。首先,您只需让脚本无误地运行即可。他说的。。。以$password[0]而不是password0开始,以$result\u数组[0]而不是$result\u数组0。。。。和$username[0]而不是username0。。。还有一些,请先调试一下。我一直在寻找一种将变量从javascript转换到objective c的方法。如果我有一个有效的方法,我可以避免整个混乱。我正在开发的应用程序已经实现了一个服务器,它使用javascript与之通信。然而,我尝试了几种方法来跨两种语言传输变量,但每种方法都失败了。因此,我尝试使用php。谢谢你的帮助!你看到我贴的链接了吗?它向您展示了如何通过EvaluatingJavaScriptFromStringYes将变量从javascript传输到objective cLook up stringByEvaluatingJavaScriptFromStringYes我看到了链接。我想我可以将其加载到web视图中,并使用该代码。不过,我确实认为以本地方式运行它更简单。当我说我一直在寻找一种将变量从javascript转换到objective-c的方法时,我的意思是我正在运行一个混合javascript/objective-c应用程序,我需要一个objective-c javascript桥。这很难实现,因此我避免使用任何会使我的应用程序更复杂的javascript。我的php代码中有很多错误。一旦我修复了这些,程序就正常工作了。谢谢