代码之家  ›  专栏  ›  技术社区  ›  dombesz

NSURLConnection缓存清除和重新连接问题

  •  1
  • dombesz  · 技术社区  · 15 年前

    给出了一个与我的ipad应用程序通信的rails应用程序。我正在使用http身份验证的异步连接。我想测试一下证书,如果他们是好的。问题是,如果我输入了好的凭据,然后又换成了错误的凭据,连接仍然可以接受。只有当我重新打开应用程序时才会拒绝。可能是缓存问题,我试图清除ipad上的缓存。

    连接初始化。

     NSURLRequest *request =
    [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0];
    [NSURLConnection connectionWithRequest:request delegate:self];
    

    还实现了willcacheresponse方法

    - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
    return nil;
    }
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   Chengjiong    15 年前

    你是否碰巧缓存了凭据缓存?如果没有,你可以忽略以下几点:)

    我不知道您选择哪种身份验证模式,这里我有一个在我的iphone应用程序中使用Windows身份验证模式的示例。当服务器端挑战客户端时,我的应用程序将发回一个NSURLCredential对象:

    NSURLCredential* credential = [NSURLCredential credentialWithUser:login password:password persistence:NSURLCredentialPersistenceForSession];
    

    typedef enum {
     NSURLCredentialPersistenceNone,       // Credential won't be stored.
     NSURLCredentialPersistenceForSession, // Credential will be stored only for this session.
     NSURLCredentialPersistencePermanent   // Credential will be stored in the user’s keychain and shared with other applications.
    } NSURLCredentialPersistence;  
    

    如您所见,如果您使用NSURLCredentialPersistenceForSession,则凭据将被缓存,并且当服务器质询客户端时,将永远不会调用回调连接:didReceiveAuthenticationChallenge:,除非您在NSURLCredentialStorage中清除它。所以这就使得你的凭证变更没用了。必要时,可以使用nsurlCredentialPersistenceOne禁用缓存或清除缓存。

    推荐文章