FIRDynamicLink.m 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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:
  21. @"<%@: %p, url [%@], match type: %@, minimumAppVersion: %@, "
  22. "match message: %@>",
  23. NSStringFromClass([self class]), self, self.url,
  24. [[self class] stringWithMatchType:_matchType],
  25. self.minimumAppVersion ?: @"N/A", self.matchMessage];
  26. }
  27. - (instancetype)initWithParametersDictionary:(NSDictionary *)parameters {
  28. NSParameterAssert(parameters.count > 0);
  29. if (self = [super init]) {
  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. _matchType = [[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. _matchType = FIRDLMatchTypeUnique;
  41. }
  42. _matchMessage = parameters[kFIRDLParameterMatchMessage];
  43. }
  44. return self;
  45. }
  46. - (NSDictionary *)parametersDictionary {
  47. NSMutableDictionary *parametersDictionary = [NSMutableDictionary dictionary];
  48. parametersDictionary[kFIRDLParameterInviteId] = _inviteId;
  49. parametersDictionary[kFIRDLParameterDeepLinkIdentifier] = [_url absoluteString];
  50. parametersDictionary[kFIRDLParameterMatchType] = [[self class] stringWithMatchType:_matchType];
  51. parametersDictionary[kFIRDLParameterWeakMatchEndpoint] = _weakMatchEndpoint;
  52. parametersDictionary[kFIRDLParameterMinimumAppVersion] = _minimumAppVersion;
  53. parametersDictionary[kFIRDLParameterMatchMessage] = _matchMessage;
  54. return parametersDictionary;
  55. }
  56. - (FIRDynamicLinkMatchConfidence)matchConfidence {
  57. return (_matchType == FIRDLMatchTypeUnique) ? FIRDynamicLinkMatchConfidenceStrong
  58. : FIRDynamicLinkMatchConfidenceWeak;
  59. }
  60. + (NSString *)stringWithMatchType:(FIRDLMatchType)matchType {
  61. switch (matchType) {
  62. case FIRDLMatchTypeNone:
  63. return @"none";
  64. case FIRDLMatchTypeWeak:
  65. return @"weak";
  66. case FIRDLMatchTypeDefault:
  67. return @"default";
  68. case FIRDLMatchTypeUnique:
  69. return @"unique";
  70. }
  71. }
  72. + (FIRDLMatchType)matchTypeWithString:(NSString *)string {
  73. static NSDictionary *matchMap;
  74. static dispatch_once_t onceToken;
  75. dispatch_once(&onceToken, ^{
  76. matchMap = @{
  77. @"none" : @(FIRDLMatchTypeNone),
  78. @"weak" : @(FIRDLMatchTypeWeak),
  79. @"default" : @(FIRDLMatchTypeDefault),
  80. @"unique" : @(FIRDLMatchTypeUnique),
  81. };
  82. });
  83. return [matchMap[string] integerValue] ?: FIRDLMatchTypeNone;
  84. }
  85. @end