Skip to content

Commit 8efd408

Browse files
author
Conrad Kramer
committed
Added XML App Link Resolver
1 parent 3eb2c00 commit 8efd408

13 files changed

+410
-197
lines changed

Diff for: Bolts.podspec

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Pod::Spec.new do |s|
4040

4141
ss.ios.source_files = 'Bolts/iOS/**/*.[hm]'
4242
ss.ios.public_header_files = 'Bolts/iOS/*.h'
43+
ss.ios.libraries = 'xml2'
44+
ss.ios.xcconfig = { 'HEADER_SEARCH_PATHS' => '$(SDKROOT)/usr/include/libxml2' }
4345
ss.osx.source_files = ''
4446
ss.watchos.source_files = ''
4547
ss.tvos.source_files = ''

Diff for: Bolts.xcodeproj/project.pbxproj

+24
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@
172172
8E8C8EFB17F23E5F00E3F1C7 /* TaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8E9C3D1C17DE9F6500427E62 /* TaskTests.m */; };
173173
8E8C8F2917F241FF00E3F1C7 /* TaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8E9C3D1C17DE9F6500427E62 /* TaskTests.m */; };
174174
8EDDA63017E17DDC00655F8A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E9C3CEC17DE9DE000427E62 /* Foundation.framework */; };
175+
D0A9104F1A86BF8500BF399F /* BFXMLAppLinkResolver.h in Headers */ = {isa = PBXBuildFile; fileRef = D0A9104D1A86BF8500BF399F /* BFXMLAppLinkResolver.h */; settings = {ATTRIBUTES = (Public, ); }; };
176+
D0A910541A86C83E00BF399F /* BFAppLinkResolvingPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = D0A910531A86C83E00BF399F /* BFAppLinkResolvingPrivate.h */; settings = {ATTRIBUTES = (Private, ); }; };
177+
D0B41D791C3EF97A00510849 /* BFAppLinkResolving.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A910511A86C4AF00BF399F /* BFAppLinkResolving.m */; };
178+
D0B41D7A1C3EF97B00510849 /* BFAppLinkResolving.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A910511A86C4AF00BF399F /* BFAppLinkResolving.m */; };
179+
D0B41D7B1C3EF98200510849 /* BFXMLAppLinkResolver.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A9104E1A86BF8500BF399F /* BFXMLAppLinkResolver.m */; };
180+
D0B41D7C1C3EF98300510849 /* BFXMLAppLinkResolver.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A9104E1A86BF8500BF399F /* BFXMLAppLinkResolver.m */; };
181+
D0B41D7D1C3EFB6300510849 /* BFAppLinkResolvingPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = D0A910531A86C83E00BF399F /* BFAppLinkResolvingPrivate.h */; settings = {ATTRIBUTES = (Private, ); }; };
182+
D0B41D7E1C3EFB6F00510849 /* BFXMLAppLinkResolver.h in Headers */ = {isa = PBXBuildFile; fileRef = D0A9104D1A86BF8500BF399F /* BFXMLAppLinkResolver.h */; settings = {ATTRIBUTES = (Public, ); }; };
175183
F5AFC9EC1BA752750076E927 /* BFTaskCompletionSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 8103FA5319900A84000BAE3F /* BFTaskCompletionSource.m */; };
176184
F5AFC9ED1BA752750076E927 /* BFTask.m in Sources */ = {isa = PBXBuildFile; fileRef = 8103FA5119900A84000BAE3F /* BFTask.m */; };
177185
F5AFC9EE1BA752750076E927 /* Bolts.m in Sources */ = {isa = PBXBuildFile; fileRef = 8103FA5519900A84000BAE3F /* Bolts.m */; };
@@ -313,6 +321,10 @@
313321
8E9C3D1C17DE9F6500427E62 /* TaskTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TaskTests.m; sourceTree = "<group>"; };
314322
B242FAB819A567660097ECAE /* BFMeasurementEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFMeasurementEvent.h; sourceTree = "<group>"; };
315323
B242FAB919A567660097ECAE /* BFMeasurementEvent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFMeasurementEvent.m; sourceTree = "<group>"; };
324+
D0A9104D1A86BF8500BF399F /* BFXMLAppLinkResolver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFXMLAppLinkResolver.h; sourceTree = "<group>"; };
325+
D0A9104E1A86BF8500BF399F /* BFXMLAppLinkResolver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFXMLAppLinkResolver.m; sourceTree = "<group>"; };
326+
D0A910511A86C4AF00BF399F /* BFAppLinkResolving.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFAppLinkResolving.m; sourceTree = "<group>"; };
327+
D0A910531A86C83E00BF399F /* BFAppLinkResolvingPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFAppLinkResolvingPrivate.h; sourceTree = "<group>"; };
316328
F5AFCA021BA752750076E927 /* Bolts.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Bolts.framework; sourceTree = BUILT_PRODUCTS_DIR; };
317329
F5AFCA131BA752770076E927 /* BoltsTests-tvOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "BoltsTests-tvOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
318330
F5AFCA151BA752AF0076E927 /* Bolts-tvOS.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Bolts-tvOS.xcconfig"; sourceTree = "<group>"; };
@@ -469,8 +481,12 @@
469481
8103FA5A19900A84000BAE3F /* BFAppLinkNavigation.h */,
470482
8103FA5B19900A84000BAE3F /* BFAppLinkNavigation.m */,
471483
8103FA5C19900A84000BAE3F /* BFAppLinkResolving.h */,
484+
D0A910531A86C83E00BF399F /* BFAppLinkResolvingPrivate.h */,
485+
D0A910511A86C4AF00BF399F /* BFAppLinkResolving.m */,
472486
8103FA6619900A84000BAE3F /* BFWebViewAppLinkResolver.h */,
473487
8103FA6719900A84000BAE3F /* BFWebViewAppLinkResolver.m */,
488+
D0A9104D1A86BF8500BF399F /* BFXMLAppLinkResolver.h */,
489+
D0A9104E1A86BF8500BF399F /* BFXMLAppLinkResolver.m */,
474490
8103FA5D19900A84000BAE3F /* BFAppLinkReturnToRefererController.h */,
475491
8103FA5E19900A84000BAE3F /* BFAppLinkReturnToRefererController.m */,
476492
8103FA5F19900A84000BAE3F /* BFAppLinkReturnToRefererView.h */,
@@ -654,6 +670,7 @@
654670
buildActionMask = 2147483647;
655671
files = (
656672
1D5D7DBA1BE3CE8200FD67C7 /* BFWebViewAppLinkResolver.h in Headers */,
673+
D0A9104F1A86BF8500BF399F /* BFXMLAppLinkResolver.h in Headers */,
657674
81CD062B1CEED28A00497F47 /* BFTask+Exceptions.h in Headers */,
658675
81CF830D1D0B559800633946 /* BFAppLinkReturnToRefererView_Internal.h in Headers */,
659676
81CF83111D0B559800633946 /* BFURL_Internal.h in Headers */,
@@ -673,6 +690,7 @@
673690
1D5D7DCC1BE3CE8200FD67C7 /* Bolts.h in Headers */,
674691
1D5D7DCD1BE3CE8200FD67C7 /* BFCancellationToken.h in Headers */,
675692
1D5D7DCE1BE3CE8200FD67C7 /* BFAppLink.h in Headers */,
693+
D0A910541A86C83E00BF399F /* BFAppLinkResolvingPrivate.h in Headers */,
676694
1D5D7DCF1BE3CE8200FD67C7 /* BFAppLinkReturnToRefererController.h in Headers */,
677695
);
678696
runOnlyForDeploymentPostprocessing = 0;
@@ -741,6 +759,7 @@
741759
isa = PBXHeadersBuildPhase;
742760
buildActionMask = 2147483647;
743761
files = (
762+
D0B41D7E1C3EFB6F00510849 /* BFXMLAppLinkResolver.h in Headers */,
744763
81ED94311BE1481900795F05 /* BFWebViewAppLinkResolver.h in Headers */,
745764
81CD062A1CEED28A00497F47 /* BFTask+Exceptions.h in Headers */,
746765
81CF830C1D0B559800633946 /* BFAppLinkReturnToRefererView_Internal.h in Headers */,
@@ -754,6 +773,7 @@
754773
81ED94201BE147CF00795F05 /* BFExecutor.h in Headers */,
755774
81ED94381BE1481900795F05 /* BFAppLinkTarget.h in Headers */,
756775
81ED94301BE1481900795F05 /* BFAppLinkResolving.h in Headers */,
776+
D0B41D7D1C3EFB6300510849 /* BFAppLinkResolvingPrivate.h in Headers */,
757777
81ED943E1BE1481900795F05 /* BFURL.h in Headers */,
758778
81CF830E1D0B559800633946 /* BFMeasurementEvent_Internal.h in Headers */,
759779
81ED94221BE147CF00795F05 /* BFTaskCompletionSource.h in Headers */,
@@ -1070,6 +1090,8 @@
10701090
1D5D7DB41BE3CE8200FD67C7 /* BFAppLink.m in Sources */,
10711091
1D5D7DB51BE3CE8200FD67C7 /* BFExecutor.m in Sources */,
10721092
1D5D7DB61BE3CE8200FD67C7 /* BFCancellationToken.m in Sources */,
1093+
D0B41D7B1C3EF98200510849 /* BFXMLAppLinkResolver.m in Sources */,
1094+
D0B41D791C3EF97A00510849 /* BFAppLinkResolving.m in Sources */,
10731095
);
10741096
runOnlyForDeploymentPostprocessing = 0;
10751097
};
@@ -1147,6 +1169,8 @@
11471169
81ED942D1BE1481900795F05 /* BFAppLink.m in Sources */,
11481170
81ED94181BE147CF00795F05 /* BFExecutor.m in Sources */,
11491171
81ED94191BE147CF00795F05 /* BFCancellationToken.m in Sources */,
1172+
D0B41D7C1C3EF98300510849 /* BFXMLAppLinkResolver.m in Sources */,
1173+
D0B41D7A1C3EF97B00510849 /* BFAppLinkResolving.m in Sources */,
11501174
);
11511175
runOnlyForDeploymentPostprocessing = 0;
11521176
};

Diff for: Bolts/Common/Bolts.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import <Bolts/BFMeasurementEvent.h>
2727
#import <Bolts/BFURL.h>
2828
#import <Bolts/BFWebViewAppLinkResolver.h>
29+
#import <Bolts/BFXMLAppLinkResolver.h>
2930
#endif
3031

3132

Diff for: Bolts/iOS/BFAppLinkNavigation.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
#import <Foundation/Foundation.h>
1212

13-
#import <Bolts/BFAppLink.h>
14-
1513
/*!
1614
The result of calling navigate on a BFAppLinkNavigation
1715
*/
@@ -25,6 +23,7 @@ typedef NS_ENUM(NSInteger, BFAppLinkNavigationType) {
2523
};
2624

2725
@protocol BFAppLinkResolving;
26+
@class BFAppLink;
2827
@class BFTask;
2928

3029
/*!

Diff for: Bolts/iOS/BFAppLinkResolving.m

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
#ifdef __IPHONE_9_3
86+
case UIUserInterfaceIdiomCarPlay:
87+
#endif
88+
case UIUserInterfaceIdiomUnspecified:
89+
default:
90+
// Future-proofing. Other User Interface idioms should only hit ios.
91+
platformData = @[ appLinkDict[BFAppLinkResolverIOSKey] ?: @{} ];
92+
break;
93+
}
94+
95+
for (NSArray *platformObjects in platformData) {
96+
for (NSDictionary *platformDict in platformObjects) {
97+
// The schema requires a single url/app store id/app name,
98+
// but we could find multiple of them. We'll make a best effort
99+
// to interpret this data.
100+
NSArray *urls = platformDict[BFAppLinkResolverIOSURLKey];
101+
NSArray *appStoreIds = platformDict[BFAppLinkResolverIOSAppStoreIdKey];
102+
NSArray *appNames = platformDict[BFAppLinkResolverIOSAppNameKey];
103+
104+
NSUInteger maxCount = MAX(urls.count, MAX(appStoreIds.count, appNames.count));
105+
106+
for (NSUInteger i = 0; i < maxCount; i++) {
107+
NSString *urlString = urls[i][BFAppLinkResolverDictionaryValueKey];
108+
NSURL *url = urlString ? [NSURL URLWithString:urlString] : nil;
109+
NSString *appStoreId = appStoreIds[i][BFAppLinkResolverDictionaryValueKey];
110+
NSString *appName = appNames[i][BFAppLinkResolverDictionaryValueKey];
111+
BFAppLinkTarget *target = [BFAppLinkTarget appLinkTargetWithURL:url
112+
appStoreId:appStoreId
113+
appName:appName];
114+
[linkTargets addObject:target];
115+
}
116+
}
117+
}
118+
119+
NSDictionary *webDict = appLinkDict[BFAppLinkResolverWebKey][0];
120+
NSString *webUrlString = webDict[BFAppLinkResolverWebURLKey][0][BFAppLinkResolverDictionaryValueKey];
121+
NSString *shouldFallbackString = webDict[BFAppLinkResolverShouldFallbackKey][0][BFAppLinkResolverDictionaryValueKey];
122+
123+
NSURL *webUrl = destination;
124+
125+
if (shouldFallbackString &&
126+
[@[ @"no", @"false", @"0" ] containsObject:[shouldFallbackString lowercaseString]]) {
127+
webUrl = nil;
128+
}
129+
if (webUrl && webUrlString) {
130+
webUrl = [NSURL URLWithString:webUrlString];
131+
}
132+
133+
return [BFAppLink appLinkWithSourceURL:destination
134+
targets:linkTargets
135+
webURL:webUrl];
136+
}
137+
138+
BFTask *BFFollowRedirects(NSURL *url) {
139+
// This task will be resolved with either the redirect NSURL
140+
// or a dictionary with the response data to be returned.
141+
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
142+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
143+
[request setValue:BFAppLinkResolverMetaTagPrefix forHTTPHeaderField:BFAppLinkResolverPreferHeader];
144+
145+
void (^completion)(NSURLResponse *response, NSData *data, NSError *error) = ^(NSURLResponse *response, NSData *data, NSError *error) {
146+
if (error) {
147+
[tcs setError:error];
148+
return;
149+
}
150+
151+
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
152+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
153+
154+
// NSURLConnection usually follows redirects automatically, but the
155+
// documentation is unclear what the default is. This helps it along.
156+
if (httpResponse.statusCode >= 300 && httpResponse.statusCode < 400) {
157+
NSString *redirectString = httpResponse.allHeaderFields[@"Location"];
158+
NSURL *redirectURL = [NSURL URLWithString:redirectString];
159+
[tcs setResult:redirectURL];
160+
return;
161+
}
162+
}
163+
164+
[tcs setResult:@{ BFAppLinkResolverRedirectResponseKey : response, BFAppLinkResolverRedirectDataKey : data }];
165+
};
166+
167+
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0 || __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_9
168+
NSURLSession *session = [NSURLSession sharedSession];
169+
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
170+
completion(response, data, error);
171+
}] resume];
172+
#else
173+
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:completion];
174+
#endif
175+
176+
return [tcs.task continueWithSuccessBlock:^id(BFTask *task) {
177+
// If we redirected, just keep recursing.
178+
if ([task.result isKindOfClass:[NSURL class]]) {
179+
return BFFollowRedirects(task.result);
180+
}
181+
return task;
182+
}];
183+
}
184+

Diff for: Bolts/iOS/BFAppLinkResolvingPrivate.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
@class BFAppLink;
12+
13+
/*
14+
Builds up a data structure filled with the app link data from the meta tags on a page.
15+
The structure of this object is a dictionary where each key holds an array of app link
16+
data dictionaries. Values are stored in a key called "_value".
17+
*/
18+
extern NSDictionary *BFAppLinkResolverParseALData(NSArray *dataArray);
19+
20+
/*
21+
Converts app link data into a BFAppLink containing the targets relevant for this platform.
22+
*/
23+
extern BFAppLink *BFAppLinkResolverAppLinkFromALData(NSDictionary *appLinkDict, NSURL *destination);
24+
25+
/*
26+
The returned task will be resolved with a dictionary containing the response data
27+
*/
28+
extern BFTask *BFFollowRedirects(NSURL *url);
29+
30+
extern NSString *const BFAppLinkResolverPreferHeader;
31+
extern NSString *const BFAppLinkResolverMetaTagPrefix;
32+
33+
extern NSString *const BFAppLinkResolverRedirectDataKey;
34+
extern NSString *const BFAppLinkResolverRedirectResponseKey;

0 commit comments

Comments
 (0)