iPhone 认证流程

标签 iphone ios authentication

我无法找到有关此主题的信息。请在这里帮助我。

我需要通过 POST 或 GET 方法将参数传递到我的网络服务器并获得回复。

基本上,如果使用 GET 方法,我想做一些类似 server.com/?user=john&password=smith 的事情,并接收使用我的 php< 完成的动态生成的 HTML 代码/strong> 脚本。所有这一切都无需在我的应用程序上使用网络浏览器。

通常是如何完成的?

最佳答案

您需要查看 NSMutableURLRequestNSURLConnection

例如,您可以像这样使用它们来向服务器发出 GET 请求:

- (void)loginUser:(NSString *)username withPassword:(NSString *)password {

    // GET
    NSString *serverURL = [NSString stringWithFormat:@"http://yourserver.com/login.php?user=%@&pass=%@", username, password];

    NSURL *url = [NSURL URLWithString:serverURL];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];

    if (connection) {
        connectionData = [[NSMutableData alloc] init];
    }
}

这将向您的服务器发送一个异步 GET 请求,其中的查询字符串包含用户名和密码。

如果您想使用 POST 请求发送用户名和密码,该方法将如下所示:

- (void)loginUser:(NSString *)username withPassword:(NSString *)password {

        // POST
        NSString *myRequestString = [NSString stringWithFormat:@"user=%@&pass=%@",username,password];
        NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];

        NSURL *url = [NSURL URLWithString:@"http://yourserver.com/login.php"];
        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
        [req setHTTPMethod: @"POST"];
        [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
        [req setHTTPBody: myRequestData];

        NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];

        if (connection) {
            connectionData = [[NSMutableData alloc] init];
        }
    }

为了从服务器获取响应,您需要实现 NSURLConnection 委托(delegate)方法,例如:

#pragma mark -
#pragma mark NSURLConnection delegate methods
#pragma mark -

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    // Called if you have an .htaccess auth. on server
    NSURLCredential *newCredential;
    newCredential = [NSURLCredential credentialWithUser:@"your_username" password:@"your_password" persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    [connectionData setLength: 0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [connectionData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [connectionData release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {  


    NSString *content = [[NSString alloc]  initWithBytes:[connectionData bytes]
                                                  length:[connectionData length] encoding: NSUTF8StringEncoding];

    // This will be your server's HTML response
    NSLog(@"response: %@",content);

    [content release];
    [connectionData release];
}

引用文献:

NSMutableURLRequest Class Reference

NSURLConnection Class Reference

希望这有帮助:)

关于iPhone 认证流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9616620/

相关文章:

ios - 带有 alpha 的图像绘制为黑色背景,其他图像绘制为白色...?

iphone - 雅虎 API 集成?

iphone - 有没有关于如何在 iPhone 上处理 OFX 的库或示例?

iphone - 多个按钮的弧 : Setting references to nil,

objective-c - UIViewAutoresizingNone : Resize after rotation

iphone - 离线正向地理编码

ios - View 高度的布局不明确

php - 在 CakePHP 中,覆盖 AppController 的 redirect() 以在 ajax 请求期间通过 json 响应 url

java - 测试网页前的 Wicket Auth/Roles 用户认证

安卓用户登录