NSURLConnection 和 NSHTTPCookie

发布于 2024-11-26 10:22:48 字数 348 浏览 1 评论 0原文

场景:验证用户身份。抓取cookie,为其他请求设置cookie。美好的。我无法立即弄清楚身份验证失败时如何处理而不将下一个视图推送到屏幕上。如果我的响应是 {errors:{password:do not match}} 我是否也应该解析此响应并相应地创建一个条件?我应该简单地检查响应代码吗?我看得出来,我让这件事变得很困难。我在以下位置添加了下一个视图:

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection

因此,当连接完成时,它自然会推送下一个视图。如果身份验证也失败,我想阻止它到达这里。也许我不应该从这里推动我的下一个观点。那去哪儿呢?

Scenario: Authenticating a user. Grab the cookie, set the cookie for other requests. Fine. What I can't immediately figure out is how to handle when the authentication fails without push my next view onto the screen. if my response is {errors:{password:does not match}} should i parse this response too and create a condition accordingly? Should I simply check the response code? I'm making this to difficult, I can tell. I added my next view in:

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection

so, when the connection finishes, it naturally pushes the next view. I'd like to prevent it from ever getting here if the authentication fails too. Maybe I shouldn't push my next view from here. Where then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙气飘飘 2024-12-03 10:22:48

我检查重定向路径:这会显示身份验证是否正常。

- (NSURLRequest*)connection:(NSURLConnection*)connection willSendRequest:(NSURLRequest*)request redirectResponse:(NSURLResponse*)redirectResponse
{

    NSLog(@"willSendRequest (from %@ to %@)", redirectResponse.URL, request.URL);
    NSString *from =   [NSString stringWithFormat:@"%@",redirectResponse.URL];
    NSString *to =   [NSString stringWithFormat:@"%@",request.URL];
    if([from isEqualToString:serverSignInPath] )
      //logged=true
    else
     //logged=false
}

I check the redirect path: This shows me if the authentication went fine or not.

- (NSURLRequest*)connection:(NSURLConnection*)connection willSendRequest:(NSURLRequest*)request redirectResponse:(NSURLResponse*)redirectResponse
{

    NSLog(@"willSendRequest (from %@ to %@)", redirectResponse.URL, request.URL);
    NSString *from =   [NSString stringWithFormat:@"%@",redirectResponse.URL];
    NSString *to =   [NSString stringWithFormat:@"%@",request.URL];
    if([from isEqualToString:serverSignInPath] )
      //logged=true
    else
     //logged=false
}
如若梦似彩虹 2024-12-03 10:22:48
NSURLResponse *HTTPResponse = (NSURLResponse *)response;
NSInteger statusCode = [HTTPResponse statusCode];

if (404 == statusCode || 500 == statusCode) {
    [connection cancel];
    [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setNetworkActivityIndicatorVisible:NO];
}

这样,它就永远不会到达connectionDidFinishLoading来推送下一个视图。

Rails 响应应该如下所示。

render :json => @foo.to_json, :status => 404
NSURLResponse *HTTPResponse = (NSURLResponse *)response;
NSInteger statusCode = [HTTPResponse statusCode];

if (404 == statusCode || 500 == statusCode) {
    [connection cancel];
    [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setNetworkActivityIndicatorVisible:NO];
}

This way, it never gets to connectionDidFinishLoading to push the next view.

Rails response should look like this.

render :json => @foo.to_json, :status => 404
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文