1

我正在为我正在开发的应用程序创建一个登录页面,为了启动登录页面,我遵循了本教程(http://dipinkrishna.com/blog/2012/03/iphoneios-programming-login-screen- post-data-url-parses-json-response/)。我想知道为什么我创建的登录页面不起作用。我很确定这可能与将数据库连接到应用程序有关。关于我如何做到这一点的任何线索?我知道用户名和密码有效,因为我在网站上尝试过并且效果很好。关于为什么它在应用程序中不起作用的任何线索?谢谢您的帮助 !

这是我的代码片段:

视图控制器.m

- (IBAction)loginClicked:(id)sender {
    @try {

        if([[username text] isEqualToString:@""] || [[password text] isEqualToString:@""] ) {
            [self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
        } else {
            NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[username text],[password text]];
            NSLog(@"PostData: %@",post);

            NSURL *url=[NSURL URLWithString:@"http://thespot2g.com/login"];

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

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

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:url];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];

            //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

            NSError *error = [[NSError alloc] init];
            NSHTTPURLResponse *response = nil;
            NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

            NSLog(@"Response code: %d", [response statusCode]);
            if ([response statusCode] >=200 && [response statusCode] <300)
            {
                NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                NSLog(@"Response ==> %@", responseData);

                SBJsonParser *jsonParser = [SBJsonParser new];
                NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
                NSLog(@"%@",jsonData);
                NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
                NSLog(@"%d",success);
                if(success == 1)
                {
                    NSLog(@"Login SUCCESS");
                    [self alertStatus:@"Logged in Successfully." :@"Login Success!"];

                } else {

                    NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
                    [self alertStatus:error_msg :@"Login Failed!"];
                }

            } else {
                if (error) NSLog(@"Error: %@", error);
                [self alertStatus:@"Connection Failed" :@"Login Failed!"];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Login Failed." :@"Login Failed!"];
    }
}

- (IBAction)backgroundClick:(id)sender {
    [username resignFirstResponder];
    [password resignFirstResponder];

}

这是我无法使用正确的凭据登录后在控制台中得到的内容

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta http-equiv="content-language" content="en-US">
  <meta name="author" content="Innovatrix Labs, LLC">
  <meta name="copyright" content="The Spot2g, LLC">  
  <meta name="description" content="connecting nightlife enthusiasts with their favorite venues through a real time feed of drink specials, ent, waits, cover charges, and etc!">
  <meta name="keywords" content="The Spot2g, Spot2g, Bar Specials, Drink Specials, Awesome Venues, Innovatrix Labs">
  <meta name="city" content="State College, Williamsport">
  <meta name="state" content="Pennsylvania">
  <title>The Spot2g</title>
  <link rel="shortcut icon" href="http://thespot2g.com/favicon.ico"/>
  <link rel="stylesheet" type="text/css" href="http://thespot2g.com/styles/spot2g.css"/>
  <link rel="stylesheet" type="text/css" href="http://thespot2g.com/styles/bootstrap.css"/>
  <script src="http://thespot2g.com/scripts/jquery-1.8.2.min.js"></script>
  <script src="http://thespot2g.com/scripts/jquery.accordion.min.js"></script> 
  <script src="http://thespot2g.com/scripts/homepage.js"></script>
  <script src="http://thespot2g.com/scripts/loginajax.js"></script>
  <script src="http://thespot2g.com/scripts/jquery.timeago.js"></script>
  <script src="http://thespot2g.com/scripts/bootstrap.min.js"></script>
<script type="text/javascript">
// Google Analytics 
 var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-26344176-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

  </head>
<body>
 <div id="page-wrapper">
  <div id="header">
   <div class="header-wrap">
    <div class="header-wrap-2">     
     <div class="logo">
      <a href="http://thespot2g.com/home"><img src="http://thespot2g.com/images/logo.png" style="background-repeat:no-repeat;" alt="Spot2G" /></a>
     </div> <!-- logo -->
        <div id="navigation-top">
<div class="user-controls"> 
 <ul>
   <li><a href="http://thespot2g.com/login">Log In</a></li> | 
   <li><a href="http://thespot2g.com/account/register">Register</a></li>
  </ul>
</div><!--//user-controls -->

</div><!--//navigation-top -->
    </div><!--//header-wrap-2 -->
   </div><!--//header-wrap -->

  </div><!-- //#header -->
  <div class="content-wrapper">
  <div class="featured-special">
    <p> <a href="http://thespot2g.com/venues/id/38">Follow us on twitter: @TheSpot2g !! at TheSpot2g</a> </p> 
  </div>


  <div id="content">
    <div id="content-left">
    <div id="navigation-side">
    <div class="navigation">
     <ul>
      <li class="accordion-parent"><a href="#">Venues</a></li>
      <ul class="accordion-content" style="max-height:235px; overflow-x:hidden;">

        <li><a href="/venues/id/61">Arena Bar & Grill</a></li><li><a href="/venues/id/68">Autoport</a></li><li><a href="/venues/id/34">Bar Bleu</a></li><li><a href="/venues/id/71">Brewery</a></li><li><a href="/venues/id/46">Cafe 210</a></li><li><a href="/venues/id/59">Champs Sports Grill</a></li><li><a href="/venues/id/31">Chrome</a></li><li><a href="/venues/id/30">Darkhorse</a></li><li><a href="/venues/id/51">Gigi's</a></li><li><a href="/venues/id/47">Gingerbread Man</a></li><li><a href="/venues/id/26">Hi-way Pizza</a></li><li><a href="/venues/id/25">Home Delivery Pizza</a></li><li><a href="/venues/id/56">Indigo</a></li><li><a href="/venues/id/23">Inferno</a></li><li><a href="/venues/id/42">Kildare's</a></li><li><a href="/venues/id/70">Levels</a></li><li><a href="/venues/id/57">Lions Den</a></li><li><a href="/venues/id/19">Mad Mex</a></li><li><a href="/venues/id/43">Mt. Nittany Inn</a></li><li><a href="/venues/id/18">Otto's Pub and Brewery</a></li><li><a href="/venues/id/50">Phyrst</a></li><li><a href="/venues/id/64">Phyrst Upstairs</a></li><li><a href="/venues/id/16">Pickles</a></li><li><a href="/venues/id/60">Pletcher's Beer Distributor</a></li><li><a href="/venues/id/55">Rotelli's</a></li><li><a href="/venues/id/63">Shandygaff</a></li><li><a href="/venues/id/11">Sharkies</a></li><li><a href="/venues/id/15">The Rathskellar</a></li><li><a href="/venues/id/13">The Saloon</a></li><li><a href="/venues/id/38">TheSpot2g</a></li><li><a href="/venues/id/58">W.R. Hickey Beer Distributor</a></li><li><a href="/venues/id/62">Westside Stadium Bar and Grill</a></li><li><a href="/venues/id/8">Z Bar</a></li><li><a href="/venues/id/7">Zeno's</a></li>      </ul>
      <li class="accordion-parent"><a href="#">Whats Going On?</a></li>
        <ul class="accordion-content">
            <li><a href="/about"> What is The Spot2g? </a> </li>
            <li><a href="https://twitter.com/thespot2g" target="_blank"> Follow us on Twitter! </a></li>
            <li><a href="http://www.facebook.com/pages/The-Spot2g-LLC/160013764093417" target="_blank"> Like us on Facebook! </a> </li> 
        </ul>
     </ul>
    </div> <!-- navigation -->  
</div><!--//#navigation-side --> 
 <div class="content-box">
     <a href="http://www.blaisealexanderhyundaimazda.com/" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1361154289.jpg" width="230px"/></a>
 </div><!--//content-box -->


 <div class="content-box">
     <a href="http://www.wrhickey.com" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1363833306.jpg" width="230px"/></a>
 </div><!--//content-box -->


</div><!--//#content-left -->
  <div id="main-content">   
    <div id="form-login">
<form action="http://thespot2g.com/login" method="post" accept-charset="utf-8"><div class="form-title"><p>Login to the Spot2g</p></div>
<ul>
    <li>
        <label>Username</label>
        <div><input type="text" name="login-username" value="" id="login-username"  /></div>
    </li>
    <li>
        <label>Password</label>
        <div><input type="password" name="login-password" value="" id="login-password"  /></div>
    </li>
    <li id="login-submit">
        <div><input type="submit" name="login" value="Login"  /></div>
    </li>
    <li>
        <p>The Username field is required.</p>
<p>The Password field is required.</p>
    </li>
</ul>   
</form> 
</div><!--//#form-login -->
  </div><!-- //#main-content -->
    <div id="content-right">
 <div class="content-box">
  <h2> Featured Venue </h2>
        <a href="/venues/id/47"> <img src="/images/uploads/1361328262.jpg" width="230px"/> </a>
 </div><!--//content-box-->

 <div class="content-box">
     <a href="http://www.pletchersbeer.com" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1364503058.jpg" width="230px"/></a>
 </div><!--//content-box -->

 <div class="content-box">
     <a href="https://www.facebook.com/pages/Good-Evil-Tattoos/105461496181423?ref=ts&fref=ts" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1377425552.jpg" width="230px"/></a>
 </div><!--//content-box -->

 <div class="content-box">
     <a href="http://www.michaelrobertssilverscissors.com/" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1379012957.jpg" width="230px"/></a>
 </div><!--//content-box -->

</div><!--//#content-right -->
 </div><!--//#content -->
    </div><!--//#content-wrapper -->
    <div id="footer">  
        <p>&copy; 2012 <a href="http://thespot2g.com/">The Spot2g, LLC</a> All Rights Reserved.</p>
        <p>Powered by <a href="http://www.innovatrixlabs.com">Innovatrix Labs</a></p>
   </div><!-- //#footer -->
  </div> <!-- //#page-wrapper -->
 </body>
</html>

2013-10-28 23:55:24.281 Spot2G[1069:a0b] (null)
2013-10-28 23:55:24.282 Spot2G[1069:a0b] 0
4

1 回答 1

0

我不确定您的具体问题,但可以提供一些一般性建议:

使用 OAuth

为了避免用户在每次使用应用程序时都必须使用用户名和密码凭据登录,您可能需要考虑使用 OAuth 安全协议保护您的应用程序。这就是 Facebook、Twitter、Dropbox、Instagram 等的工作方式。

OAuth 协议的基础是:

  • 用户使用其用户名和密码凭据进行一次身份验证。
  • 身份验证提供程序将令牌分配给可用于后续请求的应用程序。
  • 这个令牌有一个到期日。用户也可以通过访问一个站点来撤销它,从而在设备丢失的情况下确保它的安全。

您可以在后端设置自己的 OAuth 提供者,并使用例如 GTMOAuth2 设置消费者。您还可以使用上述之一(Facebook、Dropbox、Instagram 等)作为单点登录提供程序。

单点登录:

  • 使用客户端 SDK(Facebook、LinkedIn 等)或通用 OAuth 库来获取访问令牌。
  • 将此访问令牌发送到后端。
  • 后端将尝试使用令牌进行身份验证。如果成功,分配一个会话 x 分钟。
于 2013-10-29T04:42:07.950 回答