SceneDelegate.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "SceneDelegate.h"
  17. #import <FirebaseDynamicLinks/FIRDynamicLinks.h>
  18. @interface SceneDelegate ()
  19. @end
  20. @implementation SceneDelegate
  21. - (void)scene:(UIScene *)scene
  22. willConnectToSession:(UISceneSession *)session
  23. options:(UISceneConnectionOptions *)connectionOptions {
  24. // Use this method to optionally configure and attach the UIWindow `window` to the provided
  25. // UIWindowScene `scene`. If using a storyboard, the `window` property will automatically be
  26. // initialized and attached to the scene. This delegate does not imply the connecting scene or
  27. // session are new (see `application:configurationForConnectingSceneSession` instead).
  28. if (connectionOptions.userActivities && connectionOptions.userActivities.count > 0) {
  29. NSUserActivity *userActivity = connectionOptions.userActivities.allObjects.firstObject;
  30. [self handleDynamicLinkFromActivity:userActivity];
  31. }
  32. }
  33. - (void)sceneDidDisconnect:(UIScene *)scene {
  34. // Called as the scene is being released by the system.
  35. // This occurs shortly after the scene enters the background, or when its session is discarded.
  36. // Release any resources associated with this scene that can be re-created the next time the scene
  37. // connects. The scene may re-connect later, as its session was not neccessarily discarded (see
  38. // `application:didDiscardSceneSessions` instead).
  39. }
  40. - (void)sceneDidBecomeActive:(UIScene *)scene {
  41. // Called when the scene has moved from an inactive state to an active state.
  42. // Use this method to restart any tasks that were paused (or not yet started) when the scene was
  43. // inactive.
  44. }
  45. - (void)sceneWillResignActive:(UIScene *)scene {
  46. // Called when the scene will move from an active state to an inactive state.
  47. // This may occur due to temporary interruptions (ex. an incoming phone call).
  48. }
  49. - (void)sceneWillEnterForeground:(UIScene *)scene {
  50. // Called as the scene transitions from the background to the foreground.
  51. // Use this method to undo the changes made on entering the background.
  52. }
  53. - (void)sceneDidEnterBackground:(UIScene *)scene {
  54. // Called as the scene transitions from the foreground to the background.
  55. // Use this method to save data, release shared resources, and store enough scene-specific state
  56. // information to restore the scene back to its current state.
  57. }
  58. - (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity {
  59. [self handleDynamicLinkFromActivity:userActivity];
  60. }
  61. - (void)handleDynamicLinkFromActivity:(NSUserActivity *)userActivity {
  62. if (!userActivity) {
  63. return;
  64. }
  65. BOOL handled = [[FIRDynamicLinks dynamicLinks]
  66. handleUniversalLink:userActivity.webpageURL
  67. completion:^(FIRDynamicLink *_Nullable dynamicLink, NSError *_Nullable error) {
  68. [self _showDynamicLinkInfo:dynamicLink];
  69. }];
  70. if (!handled) {
  71. // Show the deep link URL from userActivity.
  72. NSLog(@"Unhandled link %@", userActivity.webpageURL);
  73. }
  74. }
  75. - (void)_showDynamicLinkInfo:(FIRDynamicLink *)dynamicLink {
  76. NSLog(@"Got dynamic link %@", dynamicLink);
  77. UIAlertController *alertVC = [UIAlertController
  78. alertControllerWithTitle:@"Got Dynamic Link!"
  79. message:[NSString stringWithFormat:@"URL [%@], matchType [%ld], "
  80. @"minimumAppVersion [%@], utmParams [%@]",
  81. dynamicLink.url,
  82. (unsigned long)dynamicLink.matchType,
  83. dynamicLink.minimumAppVersion,
  84. dynamicLink.utmParametersDictionary]
  85. preferredStyle:UIAlertControllerStyleAlert];
  86. [alertVC addAction:[UIAlertAction actionWithTitle:@"Dismiss"
  87. style:UIAlertActionStyleCancel
  88. handler:NULL]];
  89. [self.window.rootViewController presentViewController:alertVC animated:YES completion:NULL];
  90. }
  91. @end