FIRDynamicLink.m 4.6 KB

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