Skip to content

Commit

Permalink
Added XML App Link Resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Kramer committed Apr 18, 2015
1 parent 372cb99 commit 90b710b
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 141 deletions.
2 changes: 2 additions & 0 deletions Bolts.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Pod::Spec.new do |s|

ss.ios.source_files = 'Bolts/iOS/*.[hm]'
ss.ios.public_header_files = 'Bolts/iOS/*.h'
ss.ios.libraries = 'xml2'
ss.ios.xcconfig = { 'HEADER_SEARCH_PATHS' => '$(SDKROOT)/usr/include/libxml2' }
ss.osx.source_files = ''
end

Expand Down
26 changes: 26 additions & 0 deletions Bolts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
8EA6BF691805CF5600337041 /* BoltsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EA6BF681805CF5600337041 /* BoltsTests.m */; };
8EDDA63017E17DDC00655F8A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E9C3CEC17DE9DE000427E62 /* Foundation.framework */; };
B242FABB19A567660097ECAE /* BFMeasurementEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = B242FAB919A567660097ECAE /* BFMeasurementEvent.m */; };
D0A9104F1A86BF8500BF399F /* BFXMLAppLinkResolver.h in Headers */ = {isa = PBXBuildFile; fileRef = D0A9104D1A86BF8500BF399F /* BFXMLAppLinkResolver.h */; };
D0A910501A86BF8500BF399F /* BFXMLAppLinkResolver.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A9104E1A86BF8500BF399F /* BFXMLAppLinkResolver.m */; };
D0A910521A86C4AF00BF399F /* BFAppLinkResolving.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A910511A86C4AF00BF399F /* BFAppLinkResolving.m */; };
D0A910541A86C83E00BF399F /* BFAppLinkResolvingPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = D0A910531A86C83E00BF399F /* BFAppLinkResolvingPrivate.h */; settings = {ATTRIBUTES = (Private, ); }; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -150,6 +154,10 @@
B242FAB919A567660097ECAE /* BFMeasurementEvent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFMeasurementEvent.m; sourceTree = "<group>"; };
B242FABA19A567660097ECAE /* BFURL_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFURL_Internal.h; sourceTree = "<group>"; };
B242FAC019A599CD0097ECAE /* BFMeasurementEvent_Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BFMeasurementEvent_Internal.h; sourceTree = "<group>"; };
D0A9104D1A86BF8500BF399F /* BFXMLAppLinkResolver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFXMLAppLinkResolver.h; sourceTree = "<group>"; };
D0A9104E1A86BF8500BF399F /* BFXMLAppLinkResolver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFXMLAppLinkResolver.m; sourceTree = "<group>"; };
D0A910511A86C4AF00BF399F /* BFAppLinkResolving.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFAppLinkResolving.m; sourceTree = "<group>"; };
D0A910531A86C83E00BF399F /* BFAppLinkResolvingPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFAppLinkResolvingPrivate.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -249,8 +257,12 @@
8103FA5A19900A84000BAE3F /* BFAppLinkNavigation.h */,
8103FA5B19900A84000BAE3F /* BFAppLinkNavigation.m */,
8103FA5C19900A84000BAE3F /* BFAppLinkResolving.h */,
D0A910531A86C83E00BF399F /* BFAppLinkResolvingPrivate.h */,
D0A910511A86C4AF00BF399F /* BFAppLinkResolving.m */,
8103FA6619900A84000BAE3F /* BFWebViewAppLinkResolver.h */,
8103FA6719900A84000BAE3F /* BFWebViewAppLinkResolver.m */,
D0A9104D1A86BF8500BF399F /* BFXMLAppLinkResolver.h */,
D0A9104E1A86BF8500BF399F /* BFXMLAppLinkResolver.m */,
8103FA5D19900A84000BAE3F /* BFAppLinkReturnToRefererController.h */,
8103FA5E19900A84000BAE3F /* BFAppLinkReturnToRefererController.m */,
8103FA5F19900A84000BAE3F /* BFAppLinkReturnToRefererView.h */,
Expand Down Expand Up @@ -362,11 +374,13 @@
81D0EE8F19AFAA5F0000AE75 /* BFAppLinkReturnToRefererController.h in Headers */,
81D0EE8E19AFAA5F0000AE75 /* BFAppLinkResolving.h in Headers */,
81D0EE9019AFAA5F0000AE75 /* BFAppLinkReturnToRefererView.h in Headers */,
D0A9104F1A86BF8500BF399F /* BFXMLAppLinkResolver.h in Headers */,
81D0EE8719AFAA220000AE75 /* BFExecutor.h in Headers */,
81D0EE8919AFAA2B0000AE75 /* BFTaskCompletionSource.h in Headers */,
81D0EE9119AFAA6F0000AE75 /* BFAppLinkTarget.h in Headers */,
81D0EE8019AFA9E20000AE75 /* BoltsVersion.h in Headers */,
81D0EE8C19AFAA5F0000AE75 /* BFAppLink.h in Headers */,
D0A910541A86C83E00BF399F /* BFAppLinkResolvingPrivate.h in Headers */,
81D0EE9219AFAA6F0000AE75 /* BFMeasurementEvent.h in Headers */,
81D0EE9319AFAA6F0000AE75 /* BFURL.h in Headers */,
81D0EE8419AFAA100000AE75 /* Bolts.h in Headers */,
Expand Down Expand Up @@ -575,8 +589,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D0A910501A86BF8500BF399F /* BFXMLAppLinkResolver.m in Sources */,
8103FA7819900A84000BAE3F /* BFAppLinkTarget.m in Sources */,
8103FA6C19900A84000BAE3F /* BFTaskCompletionSource.m in Sources */,
D0A910521A86C4AF00BF399F /* BFAppLinkResolving.m in Sources */,
8103FA7619900A84000BAE3F /* BFAppLinkReturnToRefererView.m in Sources */,
8103FA7419900A84000BAE3F /* BFAppLinkReturnToRefererController.m in Sources */,
8103FA7C19900A84000BAE3F /* BFWebViewAppLinkResolver.m in Sources */,
Expand Down Expand Up @@ -864,7 +880,12 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNKNOWN_PRAGMAS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
"\"$(SDKROOT)/usr/include/libxml2\"",
);
ONLY_ACTIVE_ARCH = YES;
OTHER_LDFLAGS = "-lxml2";
};
name = Debug;
};
Expand Down Expand Up @@ -896,6 +917,11 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNKNOWN_PRAGMAS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
"\"$(SDKROOT)/usr/include/libxml2\"",
);
OTHER_LDFLAGS = "-lxml2";
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand Down
124 changes: 124 additions & 0 deletions Bolts/iOS/BFAppLinkResolving.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#import <UIKit/UIKit.h>

#import "BFAppLinkResolving.h"
#import "BFAppLinkResolvingPrivate.h"
#import "BFAppLink.h"
#import "BFAppLinkTarget.h"

NSString *const BFAppLinkResolverPreferHeader = @"Prefer-Html-Meta-Tags";
NSString *const BFAppLinkResolverMetaTagPrefix = @"al";

static NSString *const BFAppLinkResolverIOSURLKey = @"url";
static NSString *const BFAppLinkResolverIOSAppStoreIdKey = @"app_store_id";
static NSString *const BFAppLinkResolverIOSAppNameKey = @"app_name";
static NSString *const BFAppLinkResolverDictionaryValueKey = @"_value";
static NSString *const BFAppLinkResolverWebKey = @"web";
static NSString *const BFAppLinkResolverIOSKey = @"ios";
static NSString *const BFAppLinkResolverIPhoneKey = @"iphone";
static NSString *const BFAppLinkResolverIPadKey = @"ipad";
static NSString *const BFAppLinkResolverWebURLKey = @"url";
static NSString *const BFAppLinkResolverShouldFallbackKey = @"should_fallback";

NSDictionary *BFAppLinkResolverParseALData(NSArray *dataArray) {
NSMutableDictionary *al = [NSMutableDictionary dictionary];
for (NSDictionary *tag in dataArray) {
NSString *name = tag[@"property"];
if (![name isKindOfClass:[NSString class]]) {
continue;
}
NSArray *nameComponents = [name componentsSeparatedByString:@":"];
if (![nameComponents[0] isEqualToString:BFAppLinkResolverMetaTagPrefix]) {
continue;
}
NSMutableDictionary *root = al;
for (int i = 1; i < nameComponents.count; i++) {
NSMutableArray *children = root[nameComponents[i]];
if (!children) {
children = [NSMutableArray array];
root[nameComponents[i]] = children;
}
NSMutableDictionary *child = children.lastObject;
if (!child || i == nameComponents.count - 1) {
child = [NSMutableDictionary dictionary];
[children addObject:child];
}
root = child;
}
if (tag[@"content"]) {
root[BFAppLinkResolverDictionaryValueKey] = tag[@"content"];
}
}
return al;
}

BFAppLink *BFAppLinkResolverAppLinkFromALData(NSDictionary *appLinkDict, NSURL *destination) {
NSMutableArray *linkTargets = [NSMutableArray array];

NSArray *platformData = nil;
switch (UI_USER_INTERFACE_IDIOM()) {
case UIUserInterfaceIdiomPad:
platformData = @[appLinkDict[BFAppLinkResolverIPadKey] ?: @{},
appLinkDict[BFAppLinkResolverIOSKey] ?: @{}];
break;
case UIUserInterfaceIdiomPhone:
platformData = @[appLinkDict[BFAppLinkResolverIPhoneKey] ?: @{},
appLinkDict[BFAppLinkResolverIOSKey] ?: @{}];
break;
default:
// Future-proofing. Other User Interface idioms should only hit ios.
platformData = @[appLinkDict[BFAppLinkResolverIOSKey] ?: @{}];
break;
}

for (NSArray *platformObjects in platformData) {
for (NSDictionary *platformDict in platformObjects) {
// The schema requires a single url/app store id/app name,
// but we could find multiple of them. We'll make a best effort
// to interpret this data.
NSArray *urls = platformDict[BFAppLinkResolverIOSURLKey];
NSArray *appStoreIds = platformDict[BFAppLinkResolverIOSAppStoreIdKey];
NSArray *appNames = platformDict[BFAppLinkResolverIOSAppNameKey];

NSUInteger maxCount = MAX(urls.count, MAX(appStoreIds.count, appNames.count));

for (NSUInteger i = 0; i < maxCount; i++) {
NSString *urlString = urls[i][BFAppLinkResolverDictionaryValueKey];
NSURL *url = urlString ? [NSURL URLWithString:urlString] : nil;
NSString *appStoreId = appStoreIds[i][BFAppLinkResolverDictionaryValueKey];
NSString *appName = appNames[i][BFAppLinkResolverDictionaryValueKey];
BFAppLinkTarget *target = [BFAppLinkTarget appLinkTargetWithURL:url
appStoreId:appStoreId
appName:appName];
[linkTargets addObject:target];
}
}
}

NSDictionary *webDict = appLinkDict[BFAppLinkResolverWebKey][0];
NSString *webUrlString = webDict[BFAppLinkResolverWebURLKey][0][BFAppLinkResolverDictionaryValueKey];
NSString *shouldFallbackString = webDict[BFAppLinkResolverShouldFallbackKey][0][BFAppLinkResolverDictionaryValueKey];

NSURL *webUrl = destination;

if (shouldFallbackString &&
[@[@"no", @"false", @"0"] containsObject:[shouldFallbackString lowercaseString]]) {
webUrl = nil;
}
if (webUrl && webUrlString) {
webUrl = [NSURL URLWithString:webUrlString];
}

return [BFAppLink appLinkWithSourceURL:destination
targets:linkTargets
webURL:webUrl];
}
26 changes: 26 additions & 0 deletions Bolts/iOS/BFAppLinkResolvingPrivate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

@class BFAppLink;

/*
Builds up a data structure filled with the app link data from the meta tags on a page.
The structure of this object is a dictionary where each key holds an array of app link
data dictionaries. Values are stored in a key called "_value".
*/
extern NSDictionary *BFAppLinkResolverParseALData(NSArray *dataArray);

/*
Converts app link data into a BFAppLink containing the targets relevant for this platform.
*/
extern BFAppLink *BFAppLinkResolverAppLinkFromALData(NSDictionary *appLinkDict, NSURL *destination);

extern NSString *const BFAppLinkResolverPreferHeader;
extern NSString *const BFAppLinkResolverMetaTagPrefix;
Loading

0 comments on commit 90b710b

Please sign in to comment.