2013-09-08 7 views
6

現在、次のコードを使用してUIWebViewのデフォルトの資格情報を設定しています。これは、iOS 6.1以前では正常に動作します。しかし、iOS 7 Beta 6ではまったく動作しません。setDefaultCredentialはiOS 7のUIWebViewでは機能しませんが、以前のiOSバージョンでは正常に動作します

ロードしようとしているWebページにWindows認証が使用されています。私はiOS 7のSafariでそれらを開くことができます。しかし、私は以下のコードを実行し、UIWebViewでURLを開くと、空の白い矩形を取得し、何もロードすることはありません!私が言ったように、これはiOS 6.1以前では完全に機能します。

また、資格情報を引き渡すためにNSURLConnectionDelegateを使用するという2番目の方法も試しました。この2番目のアプローチは、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]; 
+2

AppleデベロッパーフォーラムでiOS7について質問してみましたか? –

+0

はい - これまでの返信はありません – jbro91837

+0

MPMoviePlayerで同様の問題が発生しました –

答えて

4

Windows認証用に設定されたSharepointサイトへのNSURLConnectionは、iOS 6.1でも正常に機能していました。 iOS 7では、6または7のアプリを対象にして作成したかどうかにかかわらず、すべての認証が成功するように見えますが(適切なCookieを受け取りましたが)、まだ401で応答します。 Cookieとともに送信された後続のリクエストはすべて401を受信します。

私はdidReceiveAuthenticationChallengeデリゲートプロトコルをダンプしてwillSendRequestForAuthenticationChallengeを優先してこの問題を解決しました。 2番目のデリゲートプロトコルを実装することは、最初のものが呼び出されないことを意味します。デリゲートで

は、このデリゲートプロトコル実装:私は気まぐれで、これを実装した後

- (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認証の問題が消えました。

+0

私は認証が必要な複数の異なる方法があるため、類似していますがもっと複​​雑です。認証はiOS 6または7で動作していましたが、この単純なブロックは、アプリが特定の量のNSURLConnectionをヒットしたときに便利でした。私のケースでは、私は大量のWebサービスを消費しており、一部は動作していて、一部はdidReceiveDataで401を取得していませんでした。奇妙なバグ。一定の接続数に達するとセッションが閉鎖され、サーバーは資格情報を失います。 – whyoz

+0

NSURLPersistenceForSessionを使用している間にこれをwillSendRequestForAuthenticationChallengeトンに入れなければならないと、プールに大量の接続プールが発生します。もし使用していれば、NSURLPersistenceNoneこれを複数回行う – whyoz

4

更新:この問題は、私はアップルの開発者フォーラムでこれを答えたが、今iOS7ベータの外であることを、私は再投稿よiOSの7.0.3

で修正される表示されますここに。現在のところ、iOS7ではWindows認証が壊れています。 UIWebViewが含まれているUIViewControllerで認証の問題を処理することで、これまで問題を回避することができます。関連して

  • :didReceiveAuthenticationChallenge:

    基本的に、あなたは

    1. NSURLRequestとNSURLConnection自分
    2. 接続ハンドルを作るdidReceivedResponse手動I下の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 
    
  • +1

    Appleはテストでこれらの不具合を修正しました(2回目です)。それは、Microsoftエンタープライズ環境でiPadを使用できなくする。 – Karlth

    +0

    このソリューションをありがとうございます!しかし、私はそれがリダイレクトでうまくいかないと思いますか? – NLemay

    +1

    @NLemayリダイレクトは問題ではありません。あなたがそれらを期待するならば、あなたはただそれらを扱うために準備する必要があります。 'connection:didReceiveResponse'と' connection:willSendRequest:redirectResponse'メソッド[here](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/Reference/Reference.html)を参照してください。 – jpolete

    関連する問題