|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#import <UIKit/UIKit.h> |
| 12 | + |
| 13 | +#import "BFAppLinkResolving.h" |
| 14 | +#import "BFAppLinkResolvingPrivate.h" |
| 15 | +#import "BFAppLink.h" |
| 16 | +#import "BFAppLinkTarget.h" |
| 17 | +#import "BFTask.h" |
| 18 | +#import "BFTaskCompletionSource.h" |
| 19 | + |
| 20 | +NSString *const BFAppLinkResolverPreferHeader = @"Prefer-Html-Meta-Tags"; |
| 21 | +NSString *const BFAppLinkResolverMetaTagPrefix = @"al"; |
| 22 | + |
| 23 | +NSString *const BFAppLinkResolverRedirectDataKey = @"data"; |
| 24 | +NSString *const BFAppLinkResolverRedirectResponseKey = @"response"; |
| 25 | + |
| 26 | +static NSString *const BFAppLinkResolverIOSURLKey = @"url"; |
| 27 | +static NSString *const BFAppLinkResolverIOSAppStoreIdKey = @"app_store_id"; |
| 28 | +static NSString *const BFAppLinkResolverIOSAppNameKey = @"app_name"; |
| 29 | +static NSString *const BFAppLinkResolverDictionaryValueKey = @"_value"; |
| 30 | +static NSString *const BFAppLinkResolverWebKey = @"web"; |
| 31 | +static NSString *const BFAppLinkResolverIOSKey = @"ios"; |
| 32 | +static NSString *const BFAppLinkResolverIPhoneKey = @"iphone"; |
| 33 | +static NSString *const BFAppLinkResolverIPadKey = @"ipad"; |
| 34 | +static NSString *const BFAppLinkResolverWebURLKey = @"url"; |
| 35 | +static NSString *const BFAppLinkResolverShouldFallbackKey = @"should_fallback"; |
| 36 | + |
| 37 | +NSDictionary *BFAppLinkResolverParseALData(NSArray *dataArray) { |
| 38 | + NSMutableDictionary *al = [NSMutableDictionary dictionary]; |
| 39 | + for (NSDictionary *tag in dataArray) { |
| 40 | + NSString *name = tag[@"property"]; |
| 41 | + if (![name isKindOfClass:[NSString class]]) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + NSArray *nameComponents = [name componentsSeparatedByString:@":"]; |
| 45 | + if (![nameComponents[0] isEqualToString:BFAppLinkResolverMetaTagPrefix]) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + NSMutableDictionary *root = al; |
| 49 | + for (int i = 1; i < nameComponents.count; i++) { |
| 50 | + NSMutableArray *children = root[nameComponents[i]]; |
| 51 | + if (!children) { |
| 52 | + children = [NSMutableArray array]; |
| 53 | + root[nameComponents[i]] = children; |
| 54 | + } |
| 55 | + NSMutableDictionary *child = children.lastObject; |
| 56 | + if (!child || i == nameComponents.count - 1) { |
| 57 | + child = [NSMutableDictionary dictionary]; |
| 58 | + [children addObject:child]; |
| 59 | + } |
| 60 | + root = child; |
| 61 | + } |
| 62 | + if (tag[@"content"]) { |
| 63 | + root[BFAppLinkResolverDictionaryValueKey] = tag[@"content"]; |
| 64 | + } |
| 65 | + } |
| 66 | + return al; |
| 67 | +} |
| 68 | + |
| 69 | +BFAppLink *BFAppLinkResolverAppLinkFromALData(NSDictionary *appLinkDict, NSURL *destination) { |
| 70 | + NSMutableArray *linkTargets = [NSMutableArray array]; |
| 71 | + |
| 72 | + NSArray *platformData = nil; |
| 73 | + switch (UI_USER_INTERFACE_IDIOM()) { |
| 74 | + case UIUserInterfaceIdiomPad: |
| 75 | + platformData = @[ appLinkDict[BFAppLinkResolverIPadKey] ?: @{}, |
| 76 | + appLinkDict[BFAppLinkResolverIOSKey] ?: @{} ]; |
| 77 | + break; |
| 78 | + case UIUserInterfaceIdiomPhone: |
| 79 | + platformData = @[ appLinkDict[BFAppLinkResolverIPhoneKey] ?: @{}, |
| 80 | + appLinkDict[BFAppLinkResolverIOSKey] ?: @{} ]; |
| 81 | + break; |
| 82 | +#ifdef __TVOS_9_0 |
| 83 | + case UIUserInterfaceIdiomTV: |
| 84 | +#endif |
| 85 | + case UIUserInterfaceIdiomUnspecified: |
| 86 | + default: |
| 87 | + // Future-proofing. Other User Interface idioms should only hit ios. |
| 88 | + platformData = @[ appLinkDict[BFAppLinkResolverIOSKey] ?: @{} ]; |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + for (NSArray *platformObjects in platformData) { |
| 93 | + for (NSDictionary *platformDict in platformObjects) { |
| 94 | + // The schema requires a single url/app store id/app name, |
| 95 | + // but we could find multiple of them. We'll make a best effort |
| 96 | + // to interpret this data. |
| 97 | + NSArray *urls = platformDict[BFAppLinkResolverIOSURLKey]; |
| 98 | + NSArray *appStoreIds = platformDict[BFAppLinkResolverIOSAppStoreIdKey]; |
| 99 | + NSArray *appNames = platformDict[BFAppLinkResolverIOSAppNameKey]; |
| 100 | + |
| 101 | + NSUInteger maxCount = MAX(urls.count, MAX(appStoreIds.count, appNames.count)); |
| 102 | + |
| 103 | + for (NSUInteger i = 0; i < maxCount; i++) { |
| 104 | + NSString *urlString = urls[i][BFAppLinkResolverDictionaryValueKey]; |
| 105 | + NSURL *url = urlString ? [NSURL URLWithString:urlString] : nil; |
| 106 | + NSString *appStoreId = appStoreIds[i][BFAppLinkResolverDictionaryValueKey]; |
| 107 | + NSString *appName = appNames[i][BFAppLinkResolverDictionaryValueKey]; |
| 108 | + BFAppLinkTarget *target = [BFAppLinkTarget appLinkTargetWithURL:url |
| 109 | + appStoreId:appStoreId |
| 110 | + appName:appName]; |
| 111 | + [linkTargets addObject:target]; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + NSDictionary *webDict = appLinkDict[BFAppLinkResolverWebKey][0]; |
| 117 | + NSString *webUrlString = webDict[BFAppLinkResolverWebURLKey][0][BFAppLinkResolverDictionaryValueKey]; |
| 118 | + NSString *shouldFallbackString = webDict[BFAppLinkResolverShouldFallbackKey][0][BFAppLinkResolverDictionaryValueKey]; |
| 119 | + |
| 120 | + NSURL *webUrl = destination; |
| 121 | + |
| 122 | + if (shouldFallbackString && |
| 123 | + [@[ @"no", @"false", @"0" ] containsObject:[shouldFallbackString lowercaseString]]) { |
| 124 | + webUrl = nil; |
| 125 | + } |
| 126 | + if (webUrl && webUrlString) { |
| 127 | + webUrl = [NSURL URLWithString:webUrlString]; |
| 128 | + } |
| 129 | + |
| 130 | + return [BFAppLink appLinkWithSourceURL:destination |
| 131 | + targets:linkTargets |
| 132 | + webURL:webUrl]; |
| 133 | +} |
| 134 | + |
| 135 | +BFTask *BFFollowRedirects(NSURL *url) { |
| 136 | + // This task will be resolved with either the redirect NSURL |
| 137 | + // or a dictionary with the response data to be returned. |
| 138 | + BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource]; |
| 139 | + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; |
| 140 | + [request setValue:BFAppLinkResolverMetaTagPrefix forHTTPHeaderField:BFAppLinkResolverPreferHeader]; |
| 141 | + |
| 142 | + void (^completion)(NSURLResponse *response, NSData *data, NSError *error) = ^(NSURLResponse *response, NSData *data, NSError *error) { |
| 143 | + if (error) { |
| 144 | + [tcs setError:error]; |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + if ([response isKindOfClass:[NSHTTPURLResponse class]]) { |
| 149 | + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; |
| 150 | + |
| 151 | + // NSURLConnection usually follows redirects automatically, but the |
| 152 | + // documentation is unclear what the default is. This helps it along. |
| 153 | + if (httpResponse.statusCode >= 300 && httpResponse.statusCode < 400) { |
| 154 | + NSString *redirectString = httpResponse.allHeaderFields[@"Location"]; |
| 155 | + NSURL *redirectURL = [NSURL URLWithString:redirectString]; |
| 156 | + [tcs setResult:redirectURL]; |
| 157 | + return; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + [tcs setResult:@{ BFAppLinkResolverRedirectResponseKey : response, BFAppLinkResolverRedirectDataKey : data }]; |
| 162 | + }; |
| 163 | + |
| 164 | +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0 || __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_9 |
| 165 | + NSURLSession *session = [NSURLSession sharedSession]; |
| 166 | + [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { |
| 167 | + completion(response, data, error); |
| 168 | + }] resume]; |
| 169 | +#else |
| 170 | + [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:completion]; |
| 171 | +#endif |
| 172 | + |
| 173 | + return [tcs.task continueWithSuccessBlock:^id(BFTask *task) { |
| 174 | + // If we redirected, just keep recursing. |
| 175 | + if ([task.result isKindOfClass:[NSURL class]]) { |
| 176 | + return BFFollowRedirects(task.result); |
| 177 | + } |
| 178 | + return task; |
| 179 | + }]; |
| 180 | +} |
| 181 | + |
0 commit comments