[iOS / Obj-C] Firebase DynamicLink 생성(2)

이제 λ°˜μ€ 성곡이닀. Firebase console μ—μ„œ μ„€μ • μ™„λ£Œν›„ λ„€μ΄ν‹°λΈŒ μ†ŒμŠ€λ₯Ό μ§„ν–‰ν•˜λŠ” 단계이닀.

μ°Έκ³ μ‚¬μ΄νŠΈ

https://firebase.google.com/docs/dynamic-links/ios/receive#objective-c_3

Firebase Quick Start Obj-C

https://github.com/firebase/quickstart-ios/blob/master/dynamiclinks/DynamicLinksExample/AppDelegate.m

κ΅¬ν˜„λ°©λ²•

  1. Associated Domains, URL Types μΆ”κ°€ Associated Domains 에 applinks:escjunior.page.link μΆ”κ°€ (Associated Domains 없을 경우 Capability μ˜† + λ²„νŠΌ λˆŒλŸ¬μ„œ μΆ”κ°€)

    Targets β†’ Info β†’ URL Types μ—μ„œ URL Schemes 등둝

    μ€‘μš”! μ•± Bundle ID 둜 μž…λ ₯ν•΄μ•Ό 함

  2. Podfile import FirebaseFirestore, FirebaseAuth, FirebaseDynamicLinks Podfile에 μž…λ ₯ν›„ terminal μ•±μ—μ„œ ν•΄λ‹Ή ν”„λ‘œμ νŠΈ 경둜 μ§„μž…ν›„ pod install 진행

  3. AppDelegate.m μ†ŒμŠ€ μΆ”κ°€

static NSString *const CUSTOM_URL_SCHEME = @"dlscheme";

// [START openurl]
- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<NSString *, id> *)options {
  return [self application:app
                  openURL:url
        sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
        annotation:(id)annotation {
  FIRDynamicLink *dynamicLink = [[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];

  // ***μ—¬κΈ°κ°€ dynamicLink μ‹€ν–‰λΆ€λΆ„
  if (dynamicLink) {
    if (dynamicLink.url) {
      // Handle the deep link. For example, show the deep-linked content,
      // apply a promotional offer to the user's account or show customized onboarding view.
      // [START_EXCLUDE]
      // In this sample, we just open an alert.
      [self handleDynamicLink:dynamicLink];
      // [END_EXCLUDE]
    } else {
      // Dynamic link has empty deep link. This situation will happens if
      // Firebase Dynamic Links iOS SDK tried to retrieve pending dynamic link,
      // but pending link is not available for this device/App combination.
      // At this point you may display default onboarding view.

    }
    return YES;
  }
  // [START_EXCLUDE silent]
  // Show the deep link that the app was called with.
  //[self showDeepLinkAlertViewWithMessage:[NSString stringWithFormat:@"openURL:\n%@", url]];
  // [END_EXCLUDE]
  return NO;
}
// [END openurl]

// [START continueuseractivity]
- (BOOL)application:(UIApplication *)application
continueUserActivity:(nonnull NSUserActivity *)userActivity
restorationHandler:
#if defined(__IPHONE_12_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0)
(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> *_Nullable))restorationHandler {
#else
    (nonnull void (^)(NSArray *_Nullable))restorationHandler {
#endif  // __IPHONE_12_0
  BOOL handled = [[FIRDynamicLinks dynamicLinks] handleUniversalLink:userActivity.webpageURL
                                                          completion:^(FIRDynamicLink * _Nullable dynamicLink,
                                                                      NSError * _Nullable error) {
                                                            // [START_EXCLUDE]
                                                            [self handleDynamicLink:dynamicLink];
                                                            // [END_EXCLUDE]
                                                          }];
  // [START_EXCLUDE silent]
  if (!handled) {
    // Show the deep link URL from userActivity.
    NSString *message = [NSString stringWithFormat:@"continueUserActivity webPageURL:\n%@",
                        userActivity.webpageURL];
    [self showDeepLinkAlertViewWithMessage:message];
  }
  // [END_EXCLUDE]
  return handled;
}
// [END continueuseractivity]

- (void)handleDynamicLink:(FIRDynamicLink *)dynamicLink {
  NSString *matchConfidence;
  if (dynamicLink.matchType == FIRDLMatchTypeWeak) {
    matchConfidence = @"Weak";
  } else {
    matchConfidence = @"Strong";
  }
  NSString *message = [NSString stringWithFormat:@"App URL: %@\n"
                      @"Match Confidence: %@\nMinimum App Version:%@",
                      dynamicLink.url, matchConfidence, dynamicLink.minimumAppVersion];
  //[self showDeepLinkAlertViewWithMessage:message];
    NSString *myString = dynamicLink.url.absoluteString;
    [[DataManager getInstance].webViewController loadURL:myString];
}

- (void)showDeepLinkAlertViewWithMessage:(NSString *)message {
  NSString *title = @"Deep-link Data";
  NSString *buttonTitle = @"OK";

  if ([UIAlertController class]) {
    UIAlertController *alert =
    [UIAlertController alertControllerWithTitle:title
                                        message:message
                                preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:buttonTitle
                                              style:UIAlertActionStyleDefault
                                            handler:nil]];
    UIViewController *rootViewController = self.window.rootViewController;
    [rootViewController presentViewController:alert animated:YES completion:nil];
  } else {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                      delegate:nil
                                              cancelButtonTitle:buttonTitle
                                              otherButtonTitles:nil];
    [alertView show];
  }
}

끝! 이제 1λ‹¨κ³„μ—μ„œ μ„€μ •ν•œ https://escjunior.page.link/Tbeh 링크 μ ‘μ†μ‹œ μ•± λ˜λŠ” ν•΄λ‹Ή URL이 λΈŒλΌμš°μ €μ—μ„œ μ—΄λ¦Ό!πŸ˜†πŸ˜†

Categories:

Updated:

Leave a comment