【问题标题】:How to use web server with iOS app? sample [closed]如何在 iOS 应用程序中使用 Web 服务器?样品[关闭]
【发布时间】:2014-02-04 15:04:16
【问题描述】:

我写了类似的问题,我得到了关于“如何将 Web 服务器与 iOS 应用程序一起使用?”的答案。但我需要样本,因为对我来说有一些样本是如何工作的更简单,例如,你能帮我处理那个样本吗:

如果我想要 /register 新用户,我如何在我的服务器上实现请求,我将收到服务器的 JSON 应答,我需要将它发送到变量 'data' 中的 POST ?例如服务器是http://myserver.com:8080

{ 
 email:
 password:
}  //this is register form but  how named this field?

我会收到这样的答复:

{
result:[OK|fail]
 message: [error if fail]
 data: [if OK]
}

我需要示例如何发送和接收 /register 请求。对不起我的英语,它会更好:)

更新我也无法理解如何在 Objective-C 中使用这种形式?

【问题讨论】:

    标签: ios json web ssh request


    【解决方案1】:

    所以,在这里你有我的寺庙来发送登录请求,我已经实现了 AFNetworking(https://github.com/AFNetworking/AFNetworking) 和 NSJSONSerialization 来解析 Json 响应。很难给你一个示例代码,但我认为如果你自定义这段代码,一切都应该可以正常工作。

            - (void)sentLoginReqWithLogin:(NSString *)email andPassword:(NSString *)pass {
    
                NSDictionary *params = @{@"email" : email,
                                         @"password" : pass
                                         };
    
    
                AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST_URL]];
                NSMutableURLRequest *request = [httpClient requestWithMethod:POST
                                                                        path:LOGIN_URL
                                                                  parameters:params];
    
    
                AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    
                [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    
                    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
                    NSDictionary *response = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:operation.responseData options:kNilOptions error:nil];
    
        //HERE YOU HAVE A RESPONSE (your server answer)
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {         
                    NSLog(@"Error: %@", error);      
                }];
    
    
                [operation start];
    
    }
    

    【讨论】:

    • 好的,我会试试的,如果我需要添加 O-Auth-token,你能补充一些吗?我会在 NSDictionary *params 中插入它?
    • 是的,看到这个提示:NSDictionary *params = @{@"authenticity_token" : @"token_string", @"email" : log, @"password" : pass };
    • 好的,谢谢,您建议使用 AFNetworking 然后使用其他库吗?
    • 如何在变量“data”中发送发布请求?我使用它,但得到了 null 中数据的响应: NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/login" parameters:params];
    • 您正确发送 POST 请求,可能参数创建方式错误,您的后端需要什么结构的参数。我想这只是示例:{ email: password: } //this is register form but how named this field?
    【解决方案2】:
    // Post data to server
    NSURL * url = [NSURL URLWithString:@"http://myserver.com/register/"]];
    
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString * bodyString = [NSString stringWithFormat:@"&username=%@&password=%ld", "username", "password"];
    [request setHTTPBody:[bodyString dataUsingEncoding:NSASCIIStringEncoding]];
    
    NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    
    
    // On server, do this (assuming PHP)
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];
    
    // Do w/e checks you need to do against the username and password, be sure to hash and salt the password
    // Store user information in database
    

    【讨论】:

    • 有服务器的回答吗?
    • @user3196922 你需要实现 NSURLConnection 委托方法来获取响应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    相关资源
    最近更新 更多