1

我正在使用 ASIHTTPRequest 让 iPhone 应用程序与我的 Rails 服务器通信。(我知道我可能不应该使用 ASIHTTPRequest,但我现在对它非常投入,而且它大部分时间都有效)。

当应用程序启动时,我向服务器发送一个请求,请求一个 JSON 文件。我指望 ASIHTTPRequest 来检查它是否以前存储过登录凭据并使用这些凭据。如果没有,则请求的委托实现 authenticationNeededForRequest:,它会显示我自己的自定义登录对话框。

当请求没有先前存储的凭据时,服务器会发送 401 响应。问题是在 ASIHTTPRequest 内部,一旦看到 401 响应,它就会记录:

[AUTH] Request <ASIHTTPRequest: 0x2031a800> received 401 challenge and must authenticate using (null)

这是...的结果

ASI_DEBUG_LOG(@"[AUTH] Request %@ received 401 challenge and must authenticate using %@%@",self,[self authenticationScheme],realm);

所以基本上,没有领域,身份验证方案为空。请求放弃并立即失败,而无需尝试任何其他获得身份验证的方法。

我在我的主视图中发起这样的请求:

/* When using a storyboard, this method gets called instead of the old initWithNibName:bundle:. */
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.delegate = self;

    NSURL* url = [NSURL URLWithString:@"http://myserver/file.json"];
        [ASIHTTPRequest setDefaultTimeOutSeconds:20];
        request = [[ASIHTTPRequest alloc] initWithURL:url];
        [request setDelegate:self];
        hasConnected = NO;

        newPicturesPaths = [[NSMutableArray alloc] init];
    }

    return self;
}

和 ...

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.

  /* We will try to access the server, if we don't have authentication,
   * we'll present the login view. */
   [request setAuthenticationScheme:(NSString*)kCFHTTPAuthenticationSchemeBasic];
   [request startAsynchronous];
   ...
}

如果我们以前没有保存用户名/密码(并且还保存用户名/密码供以后使用),关于如何实现我想要的行为并且只显示登录屏幕的任何建议?

编辑:我的逻辑基于this,但这似乎不是 ASIHTTPRequest 的实际行为方式。

4

0 回答 0