SetDefaultCredential does not work for UIWebView in iOS 7, but works fine in previous versions of iOS

I am currently using the following code to set the default credentials for a UIWebView. This works fine in iOS 6.1 and earlier. However, in iOS 7 Beta 6 it does not work at all.

The web pages I'm trying to download use Windows authentication. I can open them in Safari in iOS 7. However, when I run the code below and then open the URL in UIWebView, I get an empty white rectangle and nothing loads! As I said, this works fine in iOS 6.1 and earlier.

I also tried a second approach that involves using NSURLConnectionDelegate to pass credentials. This second approach also works fine in iOS 6.1 and earlier, but is broken in iOS 7.

Does anyone know why this is happening? Similar experience? Thoughts?

// 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]; 
+6
source share
2 answers

I had the exact same problem - NSURLConnection on a Sharepoint site configured for Windows authentication worked fine in iOS 6.1. In iOS 7 - regardless of whether I’ve configured and built the application on 6 or 7 - all authentications seem to be successful (getting the right cookie), but still 401 will respond; all subsequent requests sent with the cookie will also receive 401.

I solved the problem by dropping the didReceiveAuthenticationChallenge delegation protocol in favor of willSendRequestForAuthenticationChallenge. Implementing the second delegate protocol means that the first call is not called.

In your deletet, implement this delegate protocol:

 - (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]; } } 

After I implemented this on a whim, my NTLM iOS 7 authentication problems disappeared.

+4
source

Update: This issue is fixed in iOS 7.0.3

I answered this on the Apple Developer Forum, but now that iOS7 is coming out of beta, I will rearrange it here. Windows authentication is currently broken in iOS7. I expect it to be fixed soon, but until then you can work around this problem by accessing the authentication tasks in your UIViewController, which contains your UIWebView.

Essentially you

  • Do NSURLRequest and NSURLConnection by yourself
  • Handle connection: didReceiveAuthenticationChallenge:
  • In connection: didReceivedResponse manually loads your data into a UIWebView

Below I upload a PDF file, but this process works regardless of your content type.

 //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 
+4
source

Source: https://habr.com/ru/post/953417/


All Articles