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 Feb 26, 2015
1 parent 35f21ff commit 8ef8f53
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 142 deletions.
6 changes: 5 additions & 1 deletion Bolts.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ Pod::Spec.new do |s|
s.ios.deployment_target = "5.0"
s.ios.source_files = "Bolts/**/*.[hm]"
s.ios.public_header_files = "Bolts/**/*.h"

s.ios.libraries = 'xml2'
s.ios.xcconfig = { 'HEADER_SEARCH_PATHS' => '$(SDKROOT)/usr/include/libxml2' }

s.osx.deployment_target = "10.7"
s.osx.source_files = "Bolts/Common/*.[hm]"
s.osx.public_header_files = "Bolts/Common/*.h"
s.osx.libraries = 'xml2'
s.osx.xcconfig = { 'HEADER_SEARCH_PATHS' => '$(SDKROOT)/usr/include/libxml2' }
end
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 @@ -148,6 +152,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 @@ -247,8 +255,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 @@ -358,11 +370,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 @@ -571,8 +585,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 @@ -849,7 +865,12 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = (
"$(inherited)",
"\"$(SDKROOT)/usr/include/libxml2\"",
);
ONLY_ACTIVE_ARCH = YES;
OTHER_LDFLAGS = "-lxml2";
};
name = Debug;
};
Expand All @@ -870,6 +891,11 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = 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 8ef8f53

Please sign in to comment.