FIRDynamicLink.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "DynamicLinks/FIRDynamicLink+Private.h"
  17. #import "DynamicLinks/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 *)parameters {
  27. NSParameterAssert(parameters.count > 0);
  28. if (self = [super init]) {
  29. NSString *urlString = parameters[kFIRDLParameterDeepLinkIdentifier];
  30. _url = [NSURL URLWithString:urlString];
  31. _inviteId = parameters[kFIRDLParameterInviteId];
  32. _weakMatchEndpoint = parameters[kFIRDLParameterWeakMatchEndpoint];
  33. _minimumAppVersion = parameters[kFIRDLParameterMinimumAppVersion];
  34. if (parameters[kFIRDLParameterMatchType]) {
  35. _matchType = [[self class] matchTypeWithString:parameters[kFIRDLParameterMatchType]];
  36. } else if (_url || _inviteId) {
  37. // If matchType not present assume unique match for compatibility with server side behavior
  38. // on iOS 8.
  39. _matchType = FIRDLMatchTypeUnique;
  40. }
  41. _matchMessage = parameters[kFIRDLParameterMatchMessage];
  42. }
  43. return self;
  44. }
  45. - (NSDictionary *)parametersDictionary {
  46. NSMutableDictionary *parametersDictionary = [NSMutableDictionary dictionary];
  47. parametersDictionary[kFIRDLParameterInviteId] = _inviteId;
  48. parametersDictionary[kFIRDLParameterDeepLinkIdentifier] = [_url absoluteString];
  49. parametersDictionary[kFIRDLParameterMatchType] = [[self class] stringWithMatchType:_matchType];
  50. parametersDictionary[kFIRDLParameterWeakMatchEndpoint] = _weakMatchEndpoint;
  51. parametersDictionary[kFIRDLParameterMinimumAppVersion] = _minimumAppVersion;
  52. parametersDictionary[kFIRDLParameterMatchMessage] = _matchMessage;
  53. return parametersDictionary;
  54. }
  55. - (FIRDynamicLinkMatchConfidence)matchConfidence {
  56. return (_matchType == FIRDLMatchTypeUnique) ? FIRDynamicLinkMatchConfidenceStrong
  57. : FIRDynamicLinkMatchConfidenceWeak;
  58. }
  59. + (NSString *)stringWithMatchType:(FIRDLMatchType)matchType {
  60. switch (matchType) {
  61. case FIRDLMatchTypeNone:
  62. return @"none";
  63. case FIRDLMatchTypeWeak:
  64. return @"weak";
  65. case FIRDLMatchTypeDefault:
  66. return @"default";
  67. case FIRDLMatchTypeUnique:
  68. return @"unique";
  69. }
  70. }
  71. + (FIRDLMatchType)matchTypeWithString:(NSString *)string {
  72. static NSDictionary *matchMap;
  73. static dispatch_once_t onceToken;
  74. dispatch_once(&onceToken, ^{
  75. matchMap = @{
  76. @"none" : @(FIRDLMatchTypeNone),
  77. @"weak" : @(FIRDLMatchTypeWeak),
  78. @"default" : @(FIRDLMatchTypeDefault),
  79. @"unique" : @(FIRDLMatchTypeUnique),
  80. };
  81. });
  82. return [matchMap[string] integerValue] ?: FIRDLMatchTypeNone;
  83. }
  84. @end