@@ -38,7 +38,7 @@ @interface PinningURLSessionDelegate ()
3838
3939// the ApproovService that is able to interpret the trust decisions against the
4040// dynamic pins
41- @property ApproovService *approovService;
41+ @property ( nonatomic , strong , nullable ) ApproovService *approovService;
4242
4343// the original delegate to which non-authentication calls are passed
4444@property (nullable ) id <NSURLSessionDataDelegate > originalDelegate;
@@ -54,7 +54,7 @@ @implementation PinningURLSessionDelegate
5454 * information
5555 */
5656+ (instancetype )createWithDelegate : (id <NSURLSessionDataDelegate > _Nullable)delegate
57- approovService : (ApproovService *)approovService {
57+ approovService : (ApproovService *_Nullable )approovService {
5858 return [[self alloc ] initWithDelegate: delegate approovService: approovService];
5959}
6060
@@ -66,7 +66,7 @@ + (instancetype)createWithDelegate:(id<NSURLSessionDataDelegate> _Nullable)deleg
6666 * information
6767 */
6868- (instancetype )initWithDelegate : (id <NSURLSessionDataDelegate > _Nullable)delegate
69- approovService : (ApproovService *)approovService {
69+ approovService : (ApproovService *_Nullable )approovService {
7070 self = [super init ];
7171 if (self) {
7272 _approovService = approovService;
@@ -77,6 +77,19 @@ - (instancetype)initWithDelegate:(id<NSURLSessionDataDelegate> _Nullable)delegat
7777 return self;
7878}
7979
80+ /* *
81+ * Resolves the current ApproovService. Delegates created during early
82+ * swizzling may be initialized before the native module exists, so pinning
83+ * must recover the live service at challenge time.
84+ */
85+ - (ApproovService *_Nullable)currentApproovService {
86+ ApproovService *service = _approovService ?: [ApproovService sharedService ];
87+ if (_approovService == nil && service != nil ) {
88+ _approovService = service;
89+ }
90+ return service;
91+ }
92+
8093/* *
8194 * Forwards authentication challenges to the wrapped delegate when available.
8295 * Task-level callback is preferred when a task is available, then
@@ -156,9 +169,23 @@ - (void)URLSession:(NSURLSession *)session
156169 host);
157170 if ([challenge.protectionSpace.authenticationMethod
158171 isEqualToString: NSURLAuthenticationMethodServerTrust ]) {
172+ ApproovService *service = [self currentApproovService ];
173+ if (service == nil ) {
174+ ApproovLogW (@" ApproovService unavailable for task server-trust "
175+ @" challenge on %@ , forwarding without pin verification" ,
176+ host);
177+ [self forwardChallengeToOriginalDelegateForSession: session
178+ task: dataTask
179+ challenge: challenge
180+ completionHandler: completionHandler
181+ challengeType: @" server-trust "
182+ @" (service "
183+ @" unavailable)" ];
184+ return ;
185+ }
186+
159187 ApproovTrustDecision trustDecision =
160- [_approovService verifyPins: challenge.protectionSpace.serverTrust
161- forHost: host];
188+ [service verifyPins: challenge.protectionSpace.serverTrust forHost: host];
162189
163190 // Notify interceptor that pinning was invoked
164191 if (self.authChallengeCallback ) {
@@ -212,9 +239,23 @@ - (void)URLSession:(NSURLSession *)session
212239 authMethod, host);
213240 if ([challenge.protectionSpace.authenticationMethod
214241 isEqualToString: NSURLAuthenticationMethodServerTrust ]) {
242+ ApproovService *service = [self currentApproovService ];
243+ if (service == nil ) {
244+ ApproovLogW (@" ApproovService unavailable for session server-trust "
245+ @" challenge on %@ , forwarding without pin verification" ,
246+ host);
247+ [self forwardChallengeToOriginalDelegateForSession: session
248+ task: nil
249+ challenge: challenge
250+ completionHandler: completionHandler
251+ challengeType: @" server-trust "
252+ @" (service "
253+ @" unavailable)" ];
254+ return ;
255+ }
256+
215257 ApproovTrustDecision trustDecision =
216- [_approovService verifyPins: challenge.protectionSpace.serverTrust
217- forHost: host];
258+ [service verifyPins: challenge.protectionSpace.serverTrust forHost: host];
218259
219260 // Notify interceptor that pinning was invoked
220261 if (self.authChallengeCallback ) {
0 commit comments