Skip to content

Commit ee442a9

Browse files
authored
fix(dynamic_links,ios): retry handling iOS universal link on network failure (#4354)
1 parent 2ca4764 commit ee442a9

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

packages/firebase_dynamic_links/ios/Classes/FLTFirebaseDynamicLinksPlugin.m

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,59 @@ - (BOOL)checkForDynamicLink:(NSURL *)url {
150150
- (BOOL)application:(UIApplication *)application
151151
openURL:(NSURL *)url
152152
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
153-
return [self checkForDynamicLink:url];
153+
[self checkForDynamicLink:url];
154+
// results of this are ORed and NO doesn't affect other delegate interceptors' result
155+
return NO;
154156
}
155157

156158
- (BOOL)application:(UIApplication *)application
157159
openURL:(NSURL *)url
158160
sourceApplication:(NSString *)sourceApplication
159161
annotation:(id)annotation {
160-
return [self checkForDynamicLink:url];
162+
[self checkForDynamicLink:url];
163+
// results of this are ORed and NO doesn't affect other delegate interceptors' result
164+
return NO;
161165
}
162166

163167
- (BOOL)application:(UIApplication *)application
164168
continueUserActivity:(NSUserActivity *)userActivity
165-
restorationHandler:(void (^)(NSArray *))restorationHandler {
166-
BOOL handled = [[FIRDynamicLinks dynamicLinks]
167-
handleUniversalLink:userActivity.webpageURL
168-
completion:^(FIRDynamicLink *_Nullable dynamicLink, NSError *_Nullable error) {
169-
[self onDeepLinkResult:dynamicLink error:error];
170-
}];
171-
return handled;
169+
restorationHandler:
170+
#if defined(__IPHONE_12_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0)
171+
(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> *_Nullable))restorationHandler {
172+
#else
173+
(nonnull void (^)(NSArray *_Nullable))restorationHandler {
174+
#endif // __IPHONE_12_0
175+
__block BOOL retried = NO;
176+
177+
id completion =
178+
^(FIRDynamicLink *_Nullable dynamicLink, NSError *_Nullable error) {
179+
if (!error && dynamicLink && dynamicLink.url) {
180+
[self onDeepLinkResult:dynamicLink error:nil];
181+
}
182+
}
183+
// Per Apple Tech Support, a network failure could occur when returning from background on
184+
// iOS 12. https://github.com/AFNetworking/AFNetworking/issues/4279#issuecomment-447108981 So
185+
// we'll retry the request once
186+
if (error && !retried && [NSPOSIXErrorDomain isEqualToString:error.domain] &&
187+
error.code == 53) {
188+
retried = YES;
189+
[[FIRDynamicLinks dynamicLinks] handleUniversalLink:userActivity.webpageURL
190+
completion:completion];
191+
}
192+
// We could send this to Dart and maybe have a onDynamicLinkError stream but there's also
193+
// a good chance the `userActivity.webpageURL` might not be for a Firebase dynamic link,
194+
// which needs consideration - so we'll log this for now, logging will get picked up by
195+
// Crashlytics automatically if its integrated.
196+
if (error)
197+
NSLog(@"FLTFirebaseDynamicLinks: Unknown error occurred when attempting to handle a universal "
198+
@"link: %@",
199+
error);
200+
};
201+
202+
[[FIRDynamicLinks dynamicLinks] handleUniversalLink:userActivity.webpageURL completion:completion];
203+
204+
// results of this are ORed and NO doesn't affect other delegate interceptors' result
205+
return NO;
172206
}
173207

174208
- (FIRDynamicLinkShortenerCompletion)createShortLinkCompletion:(FlutterResult)result {

0 commit comments

Comments
 (0)