32

自己署名証明書を持つ開発サーバーと一緒に運用されているアプリがあります。

バックグラウンドでのダウンロードをテストしようとしてNSURLSessionいますが、うまくいかないようです- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler

使用すると、次を使用NSURLConnectionしてバイパスできます。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

    NSLog(@"canAuthenticateAgainstProtectionSpace %@", [protectionSpace authenticationMethod]);

    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);

    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

しかし、これを動作させる方法がわかりませんNSURLSession>:(

これは私が現在持っているものです(動作しません):

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
    NSLog(@"NSURLSession did receive challenge.");

    completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}

NSURLSessionまた、ホストの任意の証明書を許可するカテゴリを作成しようとしました。

#import "NSURLRequest+IgnoreSSL.h"

@implementation NSURLRequest (IgnoreSSL)

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host {
    return YES;
}

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host {}

@end

これも役に立たないようです。


編集

このメソッドを更新して、次を返すようにしました。

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {

    //Creates credentials for logged in user (username/pass)
    NSURLCredential *cred = [[AuthController sharedController] userCredentials];

    completionHandler(NSURLSessionAuthChallengeUseCredential, cred);

}

まだ何もしません。

4

2 に答える 2

1

問題の具体的な解決策を提案するのに十分な情報がありません。

主な要件は次のとおりです。

  • バックグラウンド タスクが必要なため、 で適切なNSSessionオブジェクトを作成したことを確認してくださいbackgroundSessionConfiguration:バックグラウンド セッションを取得するには、このクラス ファクトリ メソッドの使用が必須です。

  • 別のプロセスでバックグラウンドで実行されているリクエストの場合、アップロードタスクとダウンロードタスクのみがサポートされます。元のコードでは、データタスクを使用していることに注意してください。

  • App Delegate にデリゲート メソッドが正しく実装されていることを確認してください。application:handleEventsForBackgroundURLSession:completionHandler:アプリが実行されていない場合、およびセッションが独自のプロセスで実行されて資格情報が必要な場合、iOS はバックグラウンドでアプリを再起動し、バックグラウンド セッションはこのデリゲート メソッドを呼び出します。https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleEventsForBackgroundURLSession:completionHandlerも参照してください。

最初の例で試したように、サーバーの信頼評価を無効にするとうまくいくはずです。これは開発のみに使用してください。

( https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html#//apple_ref/doc/uid/TP40013509-SW44 )も参照してください

于 2013-12-06T07:51:44.917 に答える