FIRDynamicLink.m 4.6 KB

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