From 5cc10860a9e3edec73264c5fe8a77759b8d11148 Mon Sep 17 00:00:00 2001 From: Jon Weisbaum Date: Tue, 4 Feb 2020 15:24:38 -0800 Subject: [PATCH] Consent SDK now uses WKWebView instead of UIWebView. --- .../project.pbxproj | 4 +- .../PersonalizedAdConsent/Info.plist | 0 .../PersonalizedAdConsent/PACConsentForm.h | 0 .../PersonalizedAdConsent/PACConsentForm.m | 0 .../PersonalizedAdConsent/PACError.h | 0 .../PersonalizedAdConsent/PACError.m | 0 .../PACPersonalizedAdConsent.h | 0 .../PACPersonalizedAdConsent.m | 4 +- .../PersonalizedAdConsent/PACView.h | 4 +- .../PersonalizedAdConsent/PACView.m | 66 +++++++++++-------- .../consentform.html | 0 .../PersonalizedAdConsent.h | 0 .../PersonalizedAdConsent/module.modulemap | 0 13 files changed, 47 insertions(+), 31 deletions(-) mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/Info.plist mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.h mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.m mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PACError.h mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PACError.m mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.h mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PACView.h mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PACView.m mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.bundle/consentform.html mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.h mode change 100644 => 100755 PersonalizedAdConsent/PersonalizedAdConsent/module.modulemap diff --git a/PersonalizedAdConsent/PersonalizedAdConsent.xcodeproj/project.pbxproj b/PersonalizedAdConsent/PersonalizedAdConsent.xcodeproj/project.pbxproj index 93309b7..d48ffef 100755 --- a/PersonalizedAdConsent/PersonalizedAdConsent.xcodeproj/project.pbxproj +++ b/PersonalizedAdConsent/PersonalizedAdConsent.xcodeproj/project.pbxproj @@ -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"; @@ -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"; diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/Info.plist b/PersonalizedAdConsent/PersonalizedAdConsent/Info.plist old mode 100644 new mode 100755 diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.h b/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.h old mode 100644 new mode 100755 diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.m b/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.m old mode 100644 new mode 100755 diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACError.h b/PersonalizedAdConsent/PersonalizedAdConsent/PACError.h old mode 100644 new mode 100755 diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACError.m b/PersonalizedAdConsent/PersonalizedAdConsent/PACError.m old mode 100644 new mode 100755 diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.h b/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.h old mode 100644 new mode 100755 diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m b/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m old mode 100644 new mode 100755 index 056894f..8118394 --- a/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m @@ -29,7 +29,7 @@ @interface PACAdProvider () - (nullable instancetype)initWithDictionary:(nullable NSDictionary *)dictionary; @end -NSString *const PACVersionString = @"1.0.4"; +NSString *const PACVersionString = @"1.0.5"; NSString *const PACUserDefaultsRootKey = @"personalized_ad_status"; static NSString *const PACInfoUpdateURLFormat = @@ -267,7 +267,7 @@ - (BOOL)debugModeEnabled { /// Returns an info update URL for the provided publisher identifiers. - (nullable NSURL *)infoUpdateURLForPublisherIdentifiers: - (nonnull NSArray *)publisherIdentifiers { + (nonnull NSArray *)publisherIdentifiers { NSString *publisherIdentifierString = [publisherIdentifiers componentsJoinedByString:@","]; if (!publisherIdentifierString.length) { publisherIdentifierString = @""; diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACView.h b/PersonalizedAdConsent/PersonalizedAdConsent/PACView.h old mode 100644 new mode 100755 index deb7342..976271a --- a/PersonalizedAdConsent/PersonalizedAdConsent/PACView.h +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACView.h @@ -16,6 +16,8 @@ #import "PACConsentForm.h" +#import + typedef NSString *PACFormKey NS_STRING_ENUM; static PACFormKey _Nonnull const PACFormKeyOfferPersonalized = @"offer_personalized"; static PACFormKey _Nonnull const PACFormKeyOfferNonPersonalized = @"offer_non_personalized"; @@ -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 +@interface PACView : UIView @property(nonatomic, nullable) PACDismissCompletion dismissCompletion; @property(nonatomic) BOOL shouldNonPersonalizedAds; @property(nonatomic) BOOL shouldOfferAdFree; diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACView.m b/PersonalizedAdConsent/PersonalizedAdConsent/PACView.m old mode 100644 new mode 100755 index 1fb1ed1..e761d67 --- a/PersonalizedAdConsent/PersonalizedAdConsent/PACView.m +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACView.m @@ -16,6 +16,8 @@ #import "PACView.h" +#import + #import "PACError.h" /// Dictionary keys for processed form status strings. @@ -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]; @@ -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 *_Nonnull -PACQueryParametersFromURL(NSURL *_Nonnull URL) { +static NSDictionary *_Nonnull PACQueryParametersFromURL( + NSURL *_Nonnull URL) { NSString *queryString = URL.query; if (!queryString) { NSString *resourceSpecifier = URL.resourceSpecifier; @@ -132,11 +134,11 @@ static BOOL PACIsErrorStatusString(NSString *_Nonnull statusString) { return parameterDictionary; } -@interface PACView () +@interface PACView () @end @implementation PACView { - UIWebView *_webView; + WKWebView *_webView; NSDictionary *_formInformation; PACLoadCompletion _loadCompletionHandler; } @@ -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]; } @@ -182,8 +187,8 @@ - (void)loadWithFormInformation:(nonnull NSDictionary *)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]; } @@ -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]; }); } @@ -268,7 +272,7 @@ - (void)showBrowser:(nonnull NSURL *)URL { /// Returns a form status dictionary for the provided status string. - (NSDictionary *)formStatusForStatusString: - (nullable NSString *)statusString { + (nullable NSString *)statusString { NSMutableDictionary *formStatus = [[NSMutableDictionary alloc] init]; // Handle errors and ad-free option. if (!statusString.length) { @@ -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 *parameters = PACQueryParametersFromURL(request.URL); + NSDictionary *parameters = + PACQueryParametersFromURL(navigationAction.request.URL); NSString *action = parameters[@"action"]; NSCAssert(action.length > 0, @"Messages must have actions."); @@ -345,7 +359,7 @@ - (BOOL)webView:(nonnull UIWebView *)webView } } - return NO; + decisionHandler(WKNavigationActionPolicyCancel); } @end diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.bundle/consentform.html b/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.bundle/consentform.html old mode 100644 new mode 100755 diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.h b/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.h old mode 100644 new mode 100755 diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/module.modulemap b/PersonalizedAdConsent/PersonalizedAdConsent/module.modulemap old mode 100644 new mode 100755