No Description

FIRInstanceIDURLQueryItem.m 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2019 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 "FIRInstanceIDURLQueryItem.h"
  17. @implementation FIRInstanceIDURLQueryItem
  18. + (instancetype)queryItemWithName:(NSString *)name value:(NSString *)value {
  19. return [[[self class] alloc] initWithName:name value:value];
  20. }
  21. - (instancetype)initWithName:(NSString *)name value:(NSString *)value {
  22. self = [super init];
  23. if (self) {
  24. _name = [name copy];
  25. _value = [value copy];
  26. }
  27. return self;
  28. }
  29. @end
  30. NSString *FIRInstanceIDQueryFromQueryItems(NSArray<FIRInstanceIDURLQueryItem *> *queryItems) {
  31. if ([NSURLQueryItem class]) {
  32. // We are iOS 8.0 and above. Convert to NSURLQueryItems and get query that way
  33. // to take advantage of any automatic encoding
  34. NSMutableArray<NSURLQueryItem *> *urlItems =
  35. [NSMutableArray arrayWithCapacity:queryItems.count];
  36. for (FIRInstanceIDURLQueryItem *queryItem in queryItems) {
  37. [urlItems addObject:[NSURLQueryItem queryItemWithName:queryItem.name value:queryItem.value]];
  38. }
  39. NSURLComponents *components = [[NSURLComponents alloc] init];
  40. components.queryItems = urlItems;
  41. return components.query;
  42. } else {
  43. // We are on iOS 7.0. Manually create the query string
  44. NSMutableArray<NSString *> *pairs = [NSMutableArray arrayWithCapacity:queryItems.count];
  45. for (FIRInstanceIDURLQueryItem *queryItem in queryItems) {
  46. [pairs addObject:[NSString stringWithFormat:@"%@=%@", queryItem.name, queryItem.value]];
  47. }
  48. return [pairs componentsJoinedByString:@"&"];
  49. }
  50. }