网站的原生登录屏幕

时间:2014-05-28 13:16:25

标签: ios objective-c login nsurlconnection

我无法使用HTTP POST从我的应用登录网站。我已经使用POST来提交Google表单,我猜这将以基本相同的方式完成。我登录时遇到的问题是,无论输入什么凭据,它都会成功返回。如何从我的应用程序登录此站点并让它打开一个包含该用户站点的Web视图?

以下是我现在所拥有的:

- (IBAction)signIn
{
    if (emailAddress.text.length == 0)
    {
        UIAlertView *emailAlert = [[UIAlertView alloc]initWithTitle:@"" message:@"You must enter the email address associated with your account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [emailAlert show];
    }
    else if (password.text.length == 0)
    {
        UIAlertView *passwordAlert = [[UIAlertView alloc]initWithTitle:@"" message:@"You must enter your password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [passwordAlert show];
    }
    else
    {
        NSString *email = emailAddress.text;
        NSString *passwordString = password.text;

        NSString *encodedEmail = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)email,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 ));
        NSString *encodedPassword = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)passwordString,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 ));

        NSURL *url = [[NSURL alloc]initWithString:@"https://my.kyfb.com/"];
        NSMutableURLRequest * urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];

        [urlRequest setHTTPMethod:@"POST"];

        NSString *paramDataString = [NSString stringWithFormat:@"email=%@&password=%@", encodedEmail, encodedPassword];

        NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];

        [urlRequest setHTTPBody:paramData];

        NSURLConnection *urlConnection = [[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
        [urlConnection start];
    }
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSArray *trustedHosts = [[NSArray alloc]initWithObjects:@"https://my.kyfb.com/", nil];

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
        if ([trustedHosts containsObject:challenge.protectionSpace.host])
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"RESPONSE: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);

    UIAlertView *confirmSubmission = [[UIAlertView alloc]initWithTitle:@"Connection Succeeded!" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [confirmSubmission show];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"CONNECTION ERROR: %@", [error localizedDescription]);

    UIAlertView *submitFailed = [[UIAlertView alloc]initWithTitle:@"Connection Failed!" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [submitFailed show];
}

响应:

RESPONSE: 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head> 
    <title>my.KYFB</title>
    <meta http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8" />
    <meta name="ROBOTS" content="NONE,NOARCHIVE" />
    <meta name="GOOGLEBOT" content="NOARCHIVE" />
    <meta http-equiv="PRAGMA" content="NO-CACHE" />
    <meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
    <meta http-equiv="EXPIRES" content="-1" />
    <link rel="stylesheet" href="/css/custom-theme/jquery-ui.portal.ext.css" />
    <link rel="stylesheet" href="/css/login-reg.css" type="text/css" />
    <script type="text/javascript" src="/nodealert.js"></script>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/js/jquery-ui.portal.ext.js"></script>
    <script type="text/javascript" src="/js/jquery.ui.touch-punch.min.js"></script>
    <script type="text/javascript" src="/js/liveval.js"></script>
    <link rel="shortcut icon" href="/favicon.ico" />
    <script type="text/javascript">var checkmark=String.fromCharCode(10003);</script>
</head>
<body>
<div class="logo"></div>

<div class="container">
    <div class="ui-widget-content ui-corner-all" style="padding:6px;">
        <h3>No Turing Test</h3>
        <p>You appear to have skipped our turing test or the result has expired. Please try again.</p>
        <noscript> 
            <div class="ui-state-error ui-corner-all" style="margin:4px; padding:6px;">
            <p>Your browser does not support javascript or you have it disabled. Javascript is required for this site.</p>
            <p>You will not be able to pass our turing test without javascript.</p>
            </div>
        </noscript>
    </div>
</div>

<div class="footer">

    <div>
        <p>Kentucky Farm Bureau &bull; 1-800-206-6887<br />
        9201 Bunsen Parkway &bull; Louisville &bull; KY &bull; 40220-3792<br />
        <small>&copy; Copyright 2012 Kentucky Farm Bureau. All rights reserved. <a href="https://www.kyfb.com/privacy-policy/" target="_blank">Privacy Policy</a></small></p>
    </div>

</div>

</body>
</html>

0 个答案:

没有答案