FIRDynamicLink.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2018 Google
  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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import "FirebaseDynamicLinks/Sources/FIRDynamicLink+Private.h"
  19. #import "FirebaseDynamicLinks/Sources/Utilities/FDLUtilities.h"
  20. @implementation FIRDynamicLink
  21. NSString *const FDLUTMParamPrefix = @"utm_";
  22. - (NSString *)description {
  23. return [NSString stringWithFormat:@"<%@: %p, url [%@], match type: %@, minimumAppVersion: %@, "
  24. "match message: %@>",
  25. NSStringFromClass([self class]), self, self.url,
  26. [[self class] stringWithMatchType:_matchType],
  27. self.minimumAppVersion ?: @"N/A", self.matchMessage];
  28. }
  29. - (instancetype)initWithParametersDictionary:(NSDictionary<NSString *, id> *)parameters {
  30. NSParameterAssert(parameters.count > 0);
  31. if (self = [super init]) {
  32. _parametersDictionary = [parameters copy];
  33. _utmParametersDictionary = [[self class] extractUTMParams:parameters];
  34. NSString *urlString = parameters[kFIRDLParameterDeepLinkIdentifier];
  35. _url = [NSURL URLWithString:urlString];
  36. _inviteId = parameters[kFIRDLParameterInviteId];
  37. _weakMatchEndpoint = parameters[kFIRDLParameterWeakMatchEndpoint];
  38. _minimumAppVersion = parameters[kFIRDLParameterMinimumAppVersion];
  39. if (parameters[kFIRDLParameterMatchType]) {
  40. [self setMatchType:[[self class] matchTypeWithString:parameters[kFIRDLParameterMatchType]]];
  41. } else if (_url || _inviteId) {
  42. // If matchType not present assume unique match for compatibility with server side behavior
  43. // on iOS 8.
  44. [self setMatchType:FIRDLMatchTypeUnique];
  45. }
  46. _matchMessage = parameters[kFIRDLParameterMatchMessage];
  47. }
  48. return self;
  49. }
  50. #pragma mark - Properties
  51. - (void)setUrl:(NSURL *)url {
  52. _url = [url copy];
  53. [self setParametersDictionaryValue:[_url absoluteString]
  54. forKey:kFIRDLParameterDeepLinkIdentifier];
  55. }
  56. - (void)setMinimumAppVersion:(NSString *)minimumAppVersion {
  57. _minimumAppVersion = [minimumAppVersion copy];
  58. [self setParametersDictionaryValue:_minimumAppVersion forKey:kFIRDLParameterMinimumAppVersion];
  59. }
  60. - (void)setInviteId:(NSString *)inviteId {
  61. _inviteId = [inviteId copy];
  62. [self setParametersDictionaryValue:_inviteId forKey:kFIRDLParameterInviteId];
  63. }
  64. - (void)setWeakMatchEndpoint:(NSString *)weakMatchEndpoint {
  65. _weakMatchEndpoint = [weakMatchEndpoint copy];
  66. [self setParametersDictionaryValue:_weakMatchEndpoint forKey:kFIRDLParameterWeakMatchEndpoint];
  67. }
  68. - (void)setMatchType:(FIRDLMatchType)matchType {
  69. _matchType = matchType;
  70. [self setParametersDictionaryValue:[[self class] stringWithMatchType:_matchType]
  71. forKey:kFIRDLParameterMatchType];
  72. }
  73. - (void)setMatchMessage:(NSString *)matchMessage {
  74. _matchMessage = [matchMessage copy];
  75. [self setParametersDictionaryValue:_matchMessage forKey:kFIRDLParameterMatchMessage];
  76. }
  77. - (void)setParametersDictionaryValue:(id)value forKey:(NSString *)key {
  78. NSMutableDictionary<NSString *, id> *parametersDictionary =
  79. [self.parametersDictionary mutableCopy];
  80. if (value == nil) {
  81. [parametersDictionary removeObjectForKey:key];
  82. } else {
  83. parametersDictionary[key] = value;
  84. }
  85. _parametersDictionary = [parametersDictionary copy];
  86. }
  87. - (FIRDynamicLinkMatchConfidence)matchConfidence {
  88. return (_matchType == FIRDLMatchTypeUnique) ? FIRDynamicLinkMatchConfidenceStrong
  89. : FIRDynamicLinkMatchConfidenceWeak;
  90. }
  91. + (NSString *)stringWithMatchType:(FIRDLMatchType)matchType {
  92. switch (matchType) {
  93. case FIRDLMatchTypeNone:
  94. return @"none";
  95. case FIRDLMatchTypeWeak:
  96. return @"weak";
  97. case FIRDLMatchTypeDefault:
  98. return @"default";
  99. case FIRDLMatchTypeUnique:
  100. return @"unique";
  101. }
  102. }
  103. + (FIRDLMatchType)matchTypeWithString:(NSString *)string {
  104. static NSDictionary *matchMap;
  105. static dispatch_once_t onceToken;
  106. dispatch_once(&onceToken, ^{
  107. matchMap = @{
  108. @"none" : @(FIRDLMatchTypeNone),
  109. @"weak" : @(FIRDLMatchTypeWeak),
  110. @"default" : @(FIRDLMatchTypeDefault),
  111. @"unique" : @(FIRDLMatchTypeUnique),
  112. };
  113. });
  114. return [matchMap[string] integerValue] ?: FIRDLMatchTypeNone;
  115. }
  116. + (NSDictionary<NSString *, id> *)extractUTMParams:(NSDictionary<NSString *, id> *)parameters {
  117. NSMutableDictionary<NSString *, id> *utmParamsDictionary = [[NSMutableDictionary alloc] init];
  118. for (NSString *key in parameters) {
  119. if ([key hasPrefix:FDLUTMParamPrefix]) {
  120. [utmParamsDictionary setObject:[parameters valueForKey:key] forKey:key];
  121. }
  122. }
  123. return [[NSDictionary alloc] initWithDictionary:utmParamsDictionary];
  124. }
  125. @end
  126. #endif // TARGET_OS_IOS