6

我目前正在使用以下代码为 UIWebView 设置默认凭据。这在 iOS 6.1 及更早版本中运行良好。但是,在 iOS 7 Beta 6 中它根本不起作用。

我尝试加载的网页使用 Windows 身份验证。我可以在 iOS 7 的 Safari 中打开它们。但是,当我运行下面的代码,然后在 UIWebView 中打开 URL 时,我得到一个空的白色矩形并且什么都没有加载!就像我说的,这在 iOS 6.1 及更早版本中完美运行。

我还尝试了第二种方法,涉及使用 NSURLConnectionDelegate 来传递凭据。第二种方法在 iOS 6.1 和更早版本中也可以正常工作,但在 iOS 7 中被破坏了。

有谁知道为什么会这样?类似的经历?想法?

// Authenticate
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"myusername"
                                                         password:@"mypassword"
                                                      persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                         initWithHost:@"mysite.com"
                                         port:80
                                         protocol:@"http"
                                         realm:nil
                                         authenticationMethod:NSURLAuthenticationMethodDefault];

[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
4

2 回答 2

4

更新:这个问题似乎在 iOS 7.0.3 中得到修复

我在 Apple 开发者论坛上回答了这个问题,但是现在 iOS7 已经过测试版了,我会在这里重新发布。目前 Windows 身份验证在 iOS7 中被破坏。我希望很快就会有一个修复,但在那之前你可以通过在包含你的 UIWebView 的 UIViewController 中处理身份验证挑战来解决这个问题。

本质上你

  1. 自己创建一个 NSURLRequest 和 NSURLConnection
  2. 处理连接:didReceiveAuthenticationChallenge:
  3. 在连接中:didReceivedResponse 手动将您的数据加载到 UIWebView

下面我正在加载 PDF,但无论您的内容类型如何,该过程都相同。

//Make sure you implement NSURLConnectionDelegate and NSURLConnectionDataDelegate in your header

@interface MyViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) NSURLConnection *conn;
@property (strong, nonatomic) NSMutableData *pdfData;
@end

@implementation MyViewController 


//... all of your init and other standard UIViewController methods here...


//Method that loads UIWebview. You'll probably call this in viewDidLoad or somewhere similar... 
- (void) loadWebView {

    //Make Request manually with an NSURLConnection... 
    NSString *url = //Get your url
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    self.conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

//#pragma mark - NSURLConnectionDelegate

//Handle authentication challenge (NSURLConnectionDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if([challenge previousFailureCount] == 0) {

        NSString *username = //Get username
        NSString *password = //Get password

        //Use credentials to authenticate
        NSURLCredential *cred = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistencePermanent];
        [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];

    } else {

        //Cancel authentication & connection
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        [self.conn cancel];
        self.conn = nil;
    }
}

//#pragma mark - NSURLConnectionDataDelegate

//Received response (NSURLConnectionDataDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    //Init data
    self.pdfData = [NSMutableData data];
}

//Collect data as it comes in (NSURLConnectionDataDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.pdfData appendData:data];
}

//Handle connection failure (NSURLConnectionDataDelegate)
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    //Clean up... 
    [self.conn cancel];
    self.conn = nil;
    self.pdfData = nil;

    //TODO: Notify user and offer next step...
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    //Finally, load data into UIWebview here (I'm loading a PDF)...
    [self.webView loadData:self.pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
}


@end
于 2013-09-20T14:37:12.387 回答
4

我遇到了同样的问题 - 为 Windows 身份验证配置的 Sharepoint 站点的 NSURLConnection 在 iOS 6.1 中运行良好。在 iOS 7 中——无论我是针对 6 还是 7 构建应用程序——所有身份验证似乎都会成功(接收到正确的 cookie),但仍会以 401 响应;使用 cookie 发送的所有后续请求也将收到 401。

我通过转储 didReceiveAuthenticationChallenge 委托协议以支持 willSendRequestForAuthenticationChallenge 解决了这个问题。实现第二个委托协议意味着第一个永远不会被调用。

在您的委托中,实现此委托协议:

- (void)connection:(NSURLConnection *)sender willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
   if ([challenge previousFailureCount] > 0]) {
       [[challenge sender] cancelAuthenticationChallenge:challenge];
   }else{
      NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLPersistenceForSession];
   [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
   }
}

在我一时兴起实现了这个之后,我的 iOS 7 NTLM 身份验证问题就消失了。

于 2013-10-15T04:00:36.570 回答