Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
Consent SDK now uses WKWebView instead of UIWebView.
Browse files Browse the repository at this point in the history
  • Loading branch information
jweisbaum committed Feb 4, 2020
1 parent 1683244 commit 5cc1086
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = PersonalizedAdConsent/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MACH_O_TYPE = staticlib;
MODULEMAP_FILE = "$(SRCROOT)/PersonalizedAdConsent/module.modulemap";
Expand All @@ -339,7 +339,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = PersonalizedAdConsent/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MACH_O_TYPE = staticlib;
MODULEMAP_FILE = "$(SRCROOT)/PersonalizedAdConsent/module.modulemap";
Expand Down
Empty file modified PersonalizedAdConsent/PersonalizedAdConsent/Info.plist
100644 → 100755
Empty file.
Empty file modified PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.h
100644 → 100755
Empty file.
Empty file modified PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.m
100644 → 100755
Empty file.
Empty file modified PersonalizedAdConsent/PersonalizedAdConsent/PACError.h
100644 → 100755
Empty file.
Empty file modified PersonalizedAdConsent/PersonalizedAdConsent/PACError.m
100644 → 100755
Empty file.
Empty file.
4 changes: 2 additions & 2 deletions PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ @interface PACAdProvider ()
- (nullable instancetype)initWithDictionary:(nullable NSDictionary<NSString *, id> *)dictionary;
@end

NSString *const PACVersionString = @"1.0.4";
NSString *const PACVersionString = @"1.0.5";
NSString *const PACUserDefaultsRootKey = @"personalized_ad_status";

static NSString *const PACInfoUpdateURLFormat =
Expand Down Expand Up @@ -267,7 +267,7 @@ - (BOOL)debugModeEnabled {

/// Returns an info update URL for the provided publisher identifiers.
- (nullable NSURL *)infoUpdateURLForPublisherIdentifiers:
(nonnull NSArray<NSString *> *)publisherIdentifiers {
(nonnull NSArray<NSString *> *)publisherIdentifiers {
NSString *publisherIdentifierString = [publisherIdentifiers componentsJoinedByString:@","];
if (!publisherIdentifierString.length) {
publisherIdentifierString = @"";
Expand Down
4 changes: 3 additions & 1 deletion PersonalizedAdConsent/PersonalizedAdConsent/PACView.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import "PACConsentForm.h"

#import <WebKit/WebKit.h>

typedef NSString *PACFormKey NS_STRING_ENUM;
static PACFormKey _Nonnull const PACFormKeyOfferPersonalized = @"offer_personalized";
static PACFormKey _Nonnull const PACFormKeyOfferNonPersonalized = @"offer_non_personalized";
Expand All @@ -27,7 +29,7 @@ static PACFormKey _Nonnull const PACFormKeyAppIcon = @"app_icon";
static PACFormKey _Nonnull const PACFormKeyPlatform = @"plat";

/// Loads and displays the consent form.
@interface PACView : UIView<UIWebViewDelegate>
@interface PACView : UIView <WKNavigationDelegate>
@property(nonatomic, nullable) PACDismissCompletion dismissCompletion;
@property(nonatomic) BOOL shouldNonPersonalizedAds;
@property(nonatomic) BOOL shouldOfferAdFree;
Expand Down
66 changes: 40 additions & 26 deletions PersonalizedAdConsent/PersonalizedAdConsent/PACView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import "PACView.h"

#import <WebKit/WebKit.h>

#import "PACError.h"

/// Dictionary keys for processed form status strings.
Expand Down Expand Up @@ -86,7 +88,7 @@
/// Returns a JavaScript command with the provided function name and arguments.
static NSString *_Nonnull PACCreateJavaScriptCommandString(NSString *_Nonnull functionName,
NSDictionary *_Nonnull arguments) {
NSDictionary *wrappedArgs = @{ @"args" : arguments };
NSDictionary *wrappedArgs = @{@"args" : arguments};
NSString *wrappedArgsJSONString = PACJSONStringForDictionary(wrappedArgs);
NSString *command = [[NSString alloc]
initWithFormat:@"setTimeout(function(){%@(%@);}, 1);", functionName, wrappedArgsJSONString];
Expand All @@ -95,14 +97,14 @@

/// Returns YES if the status string represents an error status.
static BOOL PACIsErrorStatusString(NSString *_Nonnull statusString) {
NSRange range =
[statusString rangeOfString:@"error" options:NSAnchoredSearch | NSCaseInsensitiveSearch];
NSRange range = [statusString rangeOfString:@"error"
options:NSAnchoredSearch | NSCaseInsensitiveSearch];
return range.location != NSNotFound;
}

/// Returns the provided URL's query parameters as a dictionary.
static NSDictionary<NSString *, NSString *> *_Nonnull
PACQueryParametersFromURL(NSURL *_Nonnull URL) {
static NSDictionary<NSString *, NSString *> *_Nonnull PACQueryParametersFromURL(
NSURL *_Nonnull URL) {
NSString *queryString = URL.query;
if (!queryString) {
NSString *resourceSpecifier = URL.resourceSpecifier;
Expand Down Expand Up @@ -132,11 +134,11 @@ static BOOL PACIsErrorStatusString(NSString *_Nonnull statusString) {
return parameterDictionary;
}

@interface PACView () <UIWebViewDelegate>
@interface PACView () <WKNavigationDelegate>
@end

@implementation PACView {
UIWebView *_webView;
WKWebView *_webView;
NSDictionary<PACFormKey, id> *_formInformation;
PACLoadCompletion _loadCompletionHandler;
}
Expand All @@ -147,12 +149,15 @@ - (instancetype)initWithFrame:(CGRect)frame {
self.backgroundColor = UIColor.clearColor;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

_webView = [[UIWebView alloc] initWithFrame:frame];
_webView.delegate = self;
_webView = [[WKWebView alloc] initWithFrame:frame];
_webView.navigationDelegate = self;
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_webView.backgroundColor = UIColor.clearColor;
_webView.opaque = NO;
_webView.scrollView.bounces = NO;
if (@available(iOS 11.0, *)) {
_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

[self addSubview:_webView];
}
Expand Down Expand Up @@ -182,8 +187,8 @@ - (void)loadWithFormInformation:(nonnull NSDictionary<PACFormKey, id> *)formInfo

/// Returns the resource bundle located within |bundle|.
- (nullable NSBundle *)resourceBundleForBundle:(nonnull NSBundle *)bundle {
NSURL *resourceBundleURL =
[bundle URLForResource:@"PersonalizedAdConsent" withExtension:@"bundle"];
NSURL *resourceBundleURL = [bundle URLForResource:@"PersonalizedAdConsent"
withExtension:@"bundle"];
if (resourceBundleURL) {
return [NSBundle bundleWithURL:resourceBundleURL];
}
Expand Down Expand Up @@ -219,10 +224,9 @@ - (void)updateWebViewInformation {
mutableFormInformation[PACFormKeyPlatform] = @"ios";

NSString *infoString = PACJSONStringForDictionary(mutableFormInformation);
NSString *command = PACCreateJavaScriptCommandString(@"setUpConsentDialog", @{
@"info" : infoString
});
[self->_webView stringByEvaluatingJavaScriptFromString:command];
NSString *command =
PACCreateJavaScriptCommandString(@"setUpConsentDialog", @{@"info" : infoString});
[self->_webView evaluateJavaScript:command completionHandler:nil];
});
}

Expand Down Expand Up @@ -268,7 +272,7 @@ - (void)showBrowser:(nonnull NSURL *)URL {

/// Returns a form status dictionary for the provided status string.
- (NSDictionary<PACFormStatusKey, id> *)formStatusForStatusString:
(nullable NSString *)statusString {
(nullable NSString *)statusString {
NSMutableDictionary<PACFormStatusKey, id> *formStatus = [[NSMutableDictionary alloc] init];
// Handle errors and ad-free option.
if (!statusString.length) {
Expand All @@ -295,26 +299,36 @@ - (void)showBrowser:(nonnull NSURL *)URL {
return formStatus;
}

#pragma mark UIWebViewDelegate
#pragma mark WKNavigationDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView {
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[self updateWebViewInformation];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
- (void)webView:(nonnull WKWebView *)webView
didFailNavigation:(null_unspecified WKNavigation *)navigation
withError:(nonnull NSError *)error {
[self loadCompletedWithError:error];
}

- (void)webView:(nonnull WKWebView *)webView
didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation
withError:(nonnull NSError *)error {
[self loadCompletedWithError:error];
}

- (BOOL)webView:(nonnull UIWebView *)webView
shouldStartLoadWithRequest:(nonnull NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *URLString = request.URL.absoluteString;
- (void)webView:(nonnull WKWebView *)webView
decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction
decisionHandler:(void (^_Nonnull)(WKNavigationActionPolicy))decisionHandler {
NSString *URLString = navigationAction.request.URL.absoluteString;

if (![URLString hasPrefix:@"consent://"]) {
return YES;
decisionHandler(WKNavigationActionPolicyAllow);
return;
}

NSDictionary<NSString *, NSString *> *parameters = PACQueryParametersFromURL(request.URL);
NSDictionary<NSString *, NSString *> *parameters =
PACQueryParametersFromURL(navigationAction.request.URL);
NSString *action = parameters[@"action"];
NSCAssert(action.length > 0, @"Messages must have actions.");

Expand Down Expand Up @@ -345,7 +359,7 @@ - (BOOL)webView:(nonnull UIWebView *)webView
}
}

return NO;
decisionHandler(WKNavigationActionPolicyCancel);
}

@end
Empty file.
Empty file.
Empty file modified PersonalizedAdConsent/PersonalizedAdConsent/module.modulemap
100644 → 100755
Empty file.

0 comments on commit 5cc1086

Please sign in to comment.