ios - 不使用 JSON 的简单 RestKit 登录

标签 ios authentication restkit

这两天我一直在寻找 RestKit API 的简单代码,以便在不使用 JSON 的情况下登录网站。这是我到目前为止编写的代码:

- (void)login
{
    [RKClient clientWithBaseURLString:@"http://Mywebsite.com/login.php"];
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:@"user" forKey:@"username"];
    [params setObject:@"pass" forKey:@"password"];
    [params setObject:@"login" forKey:@"type"];
    [[RKClient sharedClient] post:@"/login" params:params delegate:self];

}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
    NSLog(@"HTTP status code:     %d", response.statusCode);
    NSLog(@"HTTP status message:  %@", [response localizedStatusCodeString]);
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    NSRange range = [[error localizedDescription] rangeOfString:@"-1012"];
    if (range.length > 0){
        //Do whatever here to handle authentication failures
    }
    RKLogError(@"Hit error: %@", error);
}

这是我收到的日志:

2012-09-08 21:44:14.633 RestKitTest[889:fb03] I restkit:RKLog.m:33 RestKit initialized...
2012-09-08 21:44:15.010 RestKitTest[889:fb03] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x6c5a560 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=NO reachabilityFlags=-R -----l->
2012-09-08 21:44:21.021 RestKitTest[889:fb03] I restkit.network:RKRequest.m:676 Status Code: 404
2012-09-08 21:44:21.036 RestKitTest[889:fb03] HTTP status code:     404
2012-09-08 21:44:21.090 RestKitTest[889:fb03] HTTP status message:  not found

如果我知道如何解决这个问题,我们将不胜感激。

最佳答案

我会说它是 404,因为你将 baseURL 设置为 http://Mywebsite.com/login.php 但你发布了作为 post:@"/login"。您真的要访问 http://Mywebsite.com/login.php/login 吗?

baseURL 应设置为所有后端调用的公共(public)前缀,在您的情况下为 http://Mywebsite.com,资源本身应发布为 post:@"/login.php”。资源名称和 baseURL 在发送请求之前连接在一起。

关于ios - 不使用 JSON 的简单 RestKit 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12336175/

相关文章:

iphone - 在每次启动时更改应用程序的启动图像

ios - 我如何阻止某人使用我们的应用程序?

ios - Restkit 映射时添加自定义值

android - 一种将最小化的 flutter 应用程序带到前台的方法

ios - 点击 UINavigationBar 标题进行更改

ios - 实例成员 'layer' 不能用于类型 'CustomPrettyView'

iphone - 如何验证用户是否已经登录,为了验证我使用这一行([facebook isSessionValid]==NO)但总是返回NO

azure - Azure Web API 的身份验证 token

ios - RestKit 在 PUT 请求时抛出 NSInternalInconsistencyException

ios - 使用 RESTKit 将 url 参数映射到对象