diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100755 index 0000000..ae319c7 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,23 @@ +# How to Contribute + +We'd love to accept your patches and contributions to this project. There are +just a few small guidelines you need to follow. + +## Contributor License Agreement + +Contributions to this project must be accompanied by a Contributor License +Agreement. You (or your employer) retain the copyright to your contribution, +this simply gives us permission to use and redistribute your contributions as +part of the project. Head over to to see +your current agreements on file or to sign a new one. + +You generally only need to submit a CLA once, so if you've already submitted one +(even if it was for a different project), you probably don't need to do it +again. + +## Code reviews + +All submissions, including submissions by project members, require review. We +use GitHub pull requests for this purpose. Consult +[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more +information on using pull requests. diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..511db98 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [2017] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/PersonalizedAdConsent/PersonalizedAdConsent.xcodeproj/project.pbxproj b/PersonalizedAdConsent/PersonalizedAdConsent.xcodeproj/project.pbxproj new file mode 100755 index 0000000..c117efd --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent.xcodeproj/project.pbxproj @@ -0,0 +1,384 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + 5A20E048208A788A0012D068 /* PACView.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A20E046208A788A0012D068 /* PACView.h */; }; + 5A20E049208A788A0012D068 /* PACView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A20E047208A788A0012D068 /* PACView.m */; }; + 5A3B5DFF20AE30C400027FCB /* module.modulemap in CopyFiles */ = {isa = PBXBuildFile; fileRef = 5A4B555A209239AB000E14B8 /* module.modulemap */; }; + 5A4B54FB2090D7FE000E14B8 /* PACConsentForm.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A4B54FA2090D7FE000E14B8 /* PACConsentForm.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 5A5669E120AE287A0061B6B2 /* PersonalizedAdConsent.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A5669CB20AE28170061B6B2 /* PersonalizedAdConsent.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 5A647C3D208A7AE4004B88F0 /* PACConsentForm.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A647C3B208A7AE4004B88F0 /* PACConsentForm.m */; }; + 5A647C40208A7C94004B88F0 /* PACError.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A647C3E208A7C94004B88F0 /* PACError.h */; }; + 5A647C41208A7C94004B88F0 /* PACError.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A647C3F208A7C94004B88F0 /* PACError.m */; }; + 5A92D2B02076961800FFE51D /* PACPersonalizedAdConsent.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A92D2A22076961800FFE51D /* PACPersonalizedAdConsent.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 5A92D2BB2076965D00FFE51D /* PACPersonalizedAdConsent.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A92D2B92076965D00FFE51D /* PACPersonalizedAdConsent.m */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 5A3B5DFE20AE309000027FCB /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = Modules; + dstSubfolderSpec = 7; + files = ( + 5A3B5DFF20AE30C400027FCB /* module.modulemap in CopyFiles */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 5A20E046208A788A0012D068 /* PACView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PACView.h; sourceTree = ""; }; + 5A20E047208A788A0012D068 /* PACView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PACView.m; sourceTree = ""; }; + 5A4B54FA2090D7FE000E14B8 /* PACConsentForm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PACConsentForm.h; sourceTree = ""; }; + 5A4B555A209239AB000E14B8 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; + 5A5022F020AF782A00E98A1F /* PersonalizedAdConsent.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = PersonalizedAdConsent.bundle; sourceTree = ""; }; + 5A5669CB20AE28170061B6B2 /* PersonalizedAdConsent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PersonalizedAdConsent.h; sourceTree = ""; }; + 5A647C3B208A7AE4004B88F0 /* PACConsentForm.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PACConsentForm.m; sourceTree = ""; }; + 5A647C3E208A7C94004B88F0 /* PACError.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PACError.h; sourceTree = ""; }; + 5A647C3F208A7C94004B88F0 /* PACError.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PACError.m; sourceTree = ""; }; + 5A92D29F2076961800FFE51D /* PersonalizedAdConsent.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PersonalizedAdConsent.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 5A92D2A22076961800FFE51D /* PACPersonalizedAdConsent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PACPersonalizedAdConsent.h; sourceTree = ""; }; + 5A92D2A32076961800FFE51D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 5A92D2B92076965D00FFE51D /* PACPersonalizedAdConsent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PACPersonalizedAdConsent.m; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 5A92D29B2076961800FFE51D /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 5A92D2952076961800FFE51D = { + isa = PBXGroup; + children = ( + 5A92D2A12076961800FFE51D /* PersonalizedAdConsent */, + 5A92D2A02076961800FFE51D /* Products */, + ); + sourceTree = ""; + }; + 5A92D2A02076961800FFE51D /* Products */ = { + isa = PBXGroup; + children = ( + 5A92D29F2076961800FFE51D /* PersonalizedAdConsent.framework */, + ); + name = Products; + sourceTree = ""; + }; + 5A92D2A12076961800FFE51D /* PersonalizedAdConsent */ = { + isa = PBXGroup; + children = ( + 5A5022F020AF782A00E98A1F /* PersonalizedAdConsent.bundle */, + 5A4B555A209239AB000E14B8 /* module.modulemap */, + 5A5669CB20AE28170061B6B2 /* PersonalizedAdConsent.h */, + 5A92D2A22076961800FFE51D /* PACPersonalizedAdConsent.h */, + 5A92D2B92076965D00FFE51D /* PACPersonalizedAdConsent.m */, + 5A647C3E208A7C94004B88F0 /* PACError.h */, + 5A647C3F208A7C94004B88F0 /* PACError.m */, + 5A20E046208A788A0012D068 /* PACView.h */, + 5A20E047208A788A0012D068 /* PACView.m */, + 5A4B54FA2090D7FE000E14B8 /* PACConsentForm.h */, + 5A647C3B208A7AE4004B88F0 /* PACConsentForm.m */, + 5A92D2A32076961800FFE51D /* Info.plist */, + ); + path = PersonalizedAdConsent; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 5A92D29C2076961800FFE51D /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 5A647C40208A7C94004B88F0 /* PACError.h in Headers */, + 5A5669E120AE287A0061B6B2 /* PersonalizedAdConsent.h in Headers */, + 5A4B54FB2090D7FE000E14B8 /* PACConsentForm.h in Headers */, + 5A20E048208A788A0012D068 /* PACView.h in Headers */, + 5A92D2B02076961800FFE51D /* PACPersonalizedAdConsent.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 5A92D29E2076961800FFE51D /* PersonalizedAdConsent */ = { + isa = PBXNativeTarget; + buildConfigurationList = 5A92D2B32076961800FFE51D /* Build configuration list for PBXNativeTarget "PersonalizedAdConsent" */; + buildPhases = ( + 5A92D29A2076961800FFE51D /* Sources */, + 5A92D29B2076961800FFE51D /* Frameworks */, + 5A92D29C2076961800FFE51D /* Headers */, + 5A92D29D2076961800FFE51D /* Resources */, + 5A3B5DFE20AE309000027FCB /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = PersonalizedAdConsent; + productName = PersonalizedAdConsent; + productReference = 5A92D29F2076961800FFE51D /* PersonalizedAdConsent.framework */; + productType = "com.apple.product-type.framework"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 5A92D2962076961800FFE51D /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0930; + ORGANIZATIONNAME = "Google LLC"; + TargetAttributes = { + 5A92D29E2076961800FFE51D = { + CreatedOnToolsVersion = 9.3; + }; + }; + }; + buildConfigurationList = 5A92D2992076961800FFE51D /* Build configuration list for PBXProject "PersonalizedAdConsent" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 5A92D2952076961800FFE51D; + productRefGroup = 5A92D2A02076961800FFE51D /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 5A92D29E2076961800FFE51D /* PersonalizedAdConsent */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 5A92D29D2076961800FFE51D /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 5A92D29A2076961800FFE51D /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 5A92D2BB2076965D00FFE51D /* PACPersonalizedAdConsent.m in Sources */, + 5A647C41208A7C94004B88F0 /* PACError.m in Sources */, + 5A647C3D208A7AE4004B88F0 /* PACConsentForm.m in Sources */, + 5A20E049208A788A0012D068 /* PACView.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 5A92D2B12076961800FFE51D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 11.3; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Debug; + }; + 5A92D2B22076961800FFE51D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 11.3; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + VALIDATE_PRODUCT = YES; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Release; + }; + 5A92D2B42076961800FFE51D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_WARN_ASSIGN_ENUM = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = PersonalizedAdConsent/Info.plist; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 7.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + MACH_O_TYPE = staticlib; + MODULEMAP_FILE = "$(SRCROOT)/PersonalizedAdConsent/module.modulemap"; + PRODUCT_BUNDLE_IDENTIFIER = com.google.PersonalizedAdConsent; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 5A92D2B52076961800FFE51D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_WARN_ASSIGN_ENUM = YES; + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = PersonalizedAdConsent/Info.plist; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 7.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + MACH_O_TYPE = staticlib; + MODULEMAP_FILE = "$(SRCROOT)/PersonalizedAdConsent/module.modulemap"; + PRODUCT_BUNDLE_IDENTIFIER = com.google.PersonalizedAdConsent; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 5A92D2992076961800FFE51D /* Build configuration list for PBXProject "PersonalizedAdConsent" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5A92D2B12076961800FFE51D /* Debug */, + 5A92D2B22076961800FFE51D /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 5A92D2B32076961800FFE51D /* Build configuration list for PBXNativeTarget "PersonalizedAdConsent" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5A92D2B42076961800FFE51D /* Debug */, + 5A92D2B52076961800FFE51D /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 5A92D2962076961800FFE51D /* Project object */; +} diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/Info.plist b/PersonalizedAdConsent/PersonalizedAdConsent/Info.plist new file mode 100755 index 0000000..1007fd9 --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0 + CFBundleVersion + $(CURRENT_PROJECT_VERSION) + NSPrincipalClass + + + diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.h b/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.h new file mode 100755 index 0000000..ba36648 --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.h @@ -0,0 +1,50 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "PACPersonalizedAdConsent.h" + +/// Called after load completes. `error` is set if the load failed. +typedef void (^PACLoadCompletion)(NSError *_Nullable error); +typedef void (^PACDismissCompletion)(NSError *_Nullable error, BOOL userPrefersAdFree); + +/// A single use consent form object. +@interface PACConsentForm : NSObject +/// Indicates whether the consent form should show a personalized ad option. Defaults to YES. +@property(nonatomic) BOOL shouldOfferPersonalizedAds; + +/// Indicates whether the consent form should show a non-personalized ad option. Defaults to YES. +@property(nonatomic) BOOL shouldOfferNonPersonalizedAds; + +/// Indicates whether the consent form should show an ad-free app option. Defaults to NO. +@property(nonatomic) BOOL shouldOfferAdFree; + +/// Unavailable. +- (nullable instancetype)init NS_UNAVAILABLE; + +/// Returns an initialized consent form with your application's privacy policy URL. Returns nil if +/// the privacy policy URL is invalid. +- (nullable instancetype)initWithApplicationPrivacyPolicyURL:(nonnull NSURL *)privacyPolicyURL + NS_DESIGNATED_INITIALIZER; + +/// Loads the consent form content and calls loadCompletion on completion. Must be called on the +/// main queue. +- (void)loadWithCompletionHandler:(nonnull PACLoadCompletion)loadCompletion; + +/// Presents the full screen consent form over viewController. The form is dismissed and +/// completionHandler is called after the user selects an option. Must be called on the main queue. +- (void)presentFromViewController:(nonnull UIViewController *)viewController + dismissCompletion:(nullable PACDismissCompletion)completionHandler; +@end diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.m b/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.m new file mode 100755 index 0000000..c2cd8b3 --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACConsentForm.m @@ -0,0 +1,220 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "PACConsentForm.h" + +#include + +#import "PACView.h" +#import "PACError.h" + +static NSTimeInterval const PACAnimationDisplayDuration = 0.3; + +/// Controls presentation of a PACView. +@interface PACViewController : UIViewController +- (nonnull instancetype)initWithConsentView:(nonnull PACView *)pacView; +@end + +@implementation PACConsentForm { + NSURL *_privacyPolicyURL; + PACView *_loadedView; + PACDismissCompletion _completionHandler; + BOOL _hasPresented; +} + +- (nullable instancetype)init { + return nil; +} + +- (instancetype)initWithApplicationPrivacyPolicyURL:(NSURL *)privacyPolicyURL { + self = [super init]; + if (self) { + _privacyPolicyURL = [privacyPolicyURL copy]; + if (!_privacyPolicyURL.absoluteString.length) { + return nil; + } + _shouldOfferPersonalizedAds = YES; + _shouldOfferNonPersonalizedAds = YES; + } + return self; +} + +- (void)loadWithCompletionHandler:(nonnull PACLoadCompletion)loadCompletion { + PAC_MUST_BE_MAIN_THREAD(); + + NSDictionary *consentInfo = + [NSUserDefaults.standardUserDefaults objectForKey:PACUserDefaultsRootKey]; + NSDictionary *formInformation = @{ + PACFormKeyOfferPersonalized : @(_shouldOfferPersonalizedAds), + PACFormKeyOfferNonPersonalized : @(_shouldOfferNonPersonalizedAds), + PACFormKeyOfferAdFree : @(_shouldOfferAdFree), + PACFormKeyAppPrivacyPolicyURLString : _privacyPolicyURL.absoluteString ?: @"", + PACFormKeyConstentInfo : consentInfo ?: @{} + }; + + PACView *pacView = [[PACView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; + PACLoadCompletion wrappedLoadCompletion = ^(NSError *_Nullable error) { + PAC_MUST_BE_MAIN_THREAD(); + if (self->_hasPresented) { + error = PACErrorWithDescription(@"Already presented. Cannot reload form."); + } else { + self->_loadedView = error ? nil : pacView; + } + if (loadCompletion) { + loadCompletion(error); + } + }; + + [pacView loadWithFormInformation:formInformation completionHandler:wrappedLoadCompletion]; +} + +- (void)presentFromViewController:(nonnull UIViewController *)viewController + dismissCompletion:(nullable PACDismissCompletion)completionHandler { + PAC_MUST_BE_MAIN_THREAD(); + + PACDismissCompletion wrappedCompletionHandler = + ^(NSError *_Nullable error, BOOL userPrefersAdFree) { + if (completionHandler) { + dispatch_async(dispatch_get_main_queue(), ^{ + completionHandler(error, userPrefersAdFree); + }); + } + }; + + PACView *view = _loadedView; + NSError *error = nil; + if (_hasPresented) { + error = PACErrorWithDescription(@"Already presented. Cannot reuse form."); + } else if (!view) { + error = PACErrorWithDescription(@"Form not loaded."); + } + + if (error) { + wrappedCompletionHandler(error, NO); + return; + } + + _loadedView = nil; + _hasPresented = YES; + + NSAssert(view != nil, @"View must not be nil."); + + _completionHandler = wrappedCompletionHandler; + + NSProcessInfo *processInfo = NSProcessInfo.processInfo; + BOOL shouldUseViewController = + [processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)]; + if (shouldUseViewController) { + // iOS 8+. + [self presentView:view fromViewController:viewController]; + } else { + // Pre-iOS 8. + UIWindow *window = viewController.view.window; + [self presentView:view usingWindow:window]; + } +} + +- (void)dismissedViewWithError:(nullable NSError *)error userPrefersAdFree:(BOOL)userPrefersAdFree { + dispatch_async(dispatch_get_main_queue(), ^{ + self->_completionHandler(error, userPrefersAdFree); + self->_completionHandler = nil; + }); +} + +#pragma mark Display Using View Controller + +/// Presents the consent view over the provided view controller. +- (void)presentView:(nonnull PACView *)view + fromViewController:(nonnull UIViewController *)viewController { + PACViewController *pacViewController = [[PACViewController alloc] initWithConsentView:view]; + + // Reference the view until the view is dismissed. + view.dismissCompletion = ^(NSError *_Nullable error, BOOL userPrefersAdFree) { + [viewController dismissViewControllerAnimated:YES + completion:^{ + [self dismissedViewWithError:error + userPrefersAdFree:userPrefersAdFree]; + }]; + }; + + [viewController presentViewController:pacViewController animated:YES completion:nil]; +} + +#pragma mark Display Using View + +/// Presents the consent view over the provided window. +- (void)presentView:(nonnull PACView *)view usingWindow:(nonnull UIWindow *)window { + // Reference the view until the view is dismissed. + PACView *strongView = view; + view.dismissCompletion = ^(NSError *_Nullable error, BOOL userPrefersAdFree) { + [self dismissView:strongView error:error userPrefersAdFree:userPrefersAdFree]; + }; + view.frame = window.bounds; + view.alpha = 0; + [window addSubview:view]; + [UIView animateWithDuration:PACAnimationDisplayDuration + animations:^{ + view.alpha = 1; + }]; +} + +/// Dismisses the view presented by -presentView:usingWindow:. +- (void)dismissView:(nonnull PACView *)view + error:(nullable NSError *)error + userPrefersAdFree:(BOOL)userPrefersAdFree { + [UIView animateWithDuration:PACAnimationDisplayDuration + animations:^{ + view.alpha = 0; + } + completion:^(BOOL finished) { + [view removeFromSuperview]; + [self dismissedViewWithError:error userPrefersAdFree:userPrefersAdFree]; + }]; +} + +@end + +@implementation PACViewController { + /// The consent view. + PACView *_pacView; +} + +- (nonnull instancetype)initWithConsentView:(nonnull PACView *)pacView NS_AVAILABLE_IOS(8_0) { + self = [super init]; + if (self) { + self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; + self.modalPresentationStyle = UIModalPresentationOverFullScreen; + _pacView = pacView; + } + return self; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + self.view.backgroundColor = UIColor.clearColor; + self.view.opaque = NO; + + _pacView.frame = self.view.bounds; + + [self.view addSubview:_pacView]; +} + +- (void)viewDidDisappear:(BOOL)animated { + [super viewDidDisappear:animated]; + [_pacView removeFromSuperview]; +} + +@end diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACError.h b/PersonalizedAdConsent/PersonalizedAdConsent/PACError.h new file mode 100755 index 0000000..f4effb6 --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACError.h @@ -0,0 +1,25 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "PACPersonalizedAdConsent.h" + +#define PAC_MUST_BE_MAIN_THREAD() \ + do { \ + NSCAssert([NSThread isMainThread], @"Must be executed on main thread."); \ + } while (0) + +/// Returns an NSError with the consent domain and provided description. +NSError *_Nonnull PACErrorWithDescription(NSString *_Nonnull description); diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACError.m b/PersonalizedAdConsent/PersonalizedAdConsent/PACError.m new file mode 100755 index 0000000..92bd94f --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACError.m @@ -0,0 +1,29 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "PACError.h" + +#import "PACPersonalizedAdConsent.h" + +NSErrorDomain const PACErrorDomain = @"Consent"; + +NSError *_Nonnull PACErrorWithDescription(NSString *_Nonnull description) { + return [[NSError alloc] initWithDomain:PACErrorDomain + code:1 + userInfo:@{ + NSLocalizedDescriptionKey : description ?: @"Internal error." + }]; +} diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.h b/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.h new file mode 100755 index 0000000..2b60dcb --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.h @@ -0,0 +1,90 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import + +/// Personalized ad consent error domain. +extern NSErrorDomain _Nonnull const PACErrorDomain; + +/// Personalized ad consent user defaults root key. Consent information is stored in standard user +/// defaults under this key. +extern NSString *_Nonnull const PACUserDefaultsRootKey; + +/// Consent status values. +typedef NS_ENUM(NSInteger, PACConsentStatus) { + PACConsentStatusUnknown = 0, ///< Unknown consent status. + PACConsentStatusNonPersonalized = 1, ///< User consented to non-personalized ads. + PACConsentStatusPersonalized = 2, ///< User consented to personalized ads. +}; + +/// Debug values for testing geography. +typedef NS_ENUM(NSInteger, PACDebugGeography) { + PACDebugGeographyDisabled = 0, ///< Disable geography debugging. + PACDebugGeographyEEA = 1, ///< Geography appears as in EEA for debug devices. + PACDebugGeographyNotEEA = 2, ///< Geography appears as not in EEA for debug devices. +}; + +/// Ad provider information. +@interface PACAdProvider : NSObject +/// Ad provider's identifier. +@property(nonatomic, readonly, nonnull) NSNumber *identifier; +/// Ad provider's name. +@property(nonatomic, readonly, nonnull) NSString *name; +/// Ad provider's privacy policy URL. +@property(nonatomic, readonly, nonnull) NSURL *privacyPolicyURL; +@end + +/// Called when the consent info request completes. Error is nil on success, and non-nil if the +/// update failed. +typedef void (^PACConsentInformationUpdateHandler)(NSError *_Nullable error); + +/// Consent information. Not thread safe. All methods must be called on the main thread. +@interface PACConsentInformation : NSObject + +/// The shared consent information instance. +@property(class, nonatomic, readonly, nonnull) PACConsentInformation *sharedInstance; + +/// The user's consent status. +@property(nonatomic) PACConsentStatus consentStatus; + +/// Indicates whether the user is tagged for under age of consent. +@property(nonatomic, getter=isTaggedForUnderAgeOfConsent) BOOL tagForUnderAgeOfConsent; + +/// The consent info update request was in EEA or from an unknown location. +@property(nonatomic, readonly, getter=isRequestLocationInEEAOrUnknown) + BOOL requestLocationInEEAOrUnknown; + +/// Array of ad providers. +@property(nonatomic, readonly, nullable) NSArray *adProviders; + +/// Array of advertising identifier UUID strings. Debug features are enabled for devices with these +/// identifiers. Debug features are always enabled for simulators. +@property(nonatomic, nullable) NSArray *debugIdentifiers; + +/// Debug geography. Used for debug devices only. +@property(nonatomic) PACDebugGeography debugGeography; + +/// Resets consent information to default state and clears ad providers. +- (void)reset; + +/// Requests consent information update for the provided publisher identifiers. All publisher +/// identifiers used in the application should be specified in this call. Consent status is reset to +/// unknown when the ad provider list changes. +- (void)requestConsentInfoUpdateForPublisherIdentifiers: + (nonnull NSArray *)publisherIdentifiers + completionHandler: + (nonnull PACConsentInformationUpdateHandler)handler; +@end diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m b/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m new file mode 100755 index 0000000..8d6e87f --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACPersonalizedAdConsent.m @@ -0,0 +1,473 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "PACPersonalizedAdConsent.h" + +#import + +#import "PACError.h" + +// Internal API. Do not use. Use public API from PACPersonalizedAdConsent.h. +@interface PACAdProvider () +/// Internal API. Do not use. The dictionary representation of the ad provider. +@property(nonatomic, readonly, nonnull) NSDictionary *dictionaryRepresentation; +/// Internal API. Do not use. Returns an initialized ad provider or nil if the dictionary contains +/// invalid information. +- (nullable instancetype)initWithDictionary:(nullable NSDictionary *)dictionary; +@end + +NSString *const PACUserDefaultsRootKey = @"personalized_ad_status"; + +static NSString *const PACInfoUpdateURLFormat = + @"https://adservice.google.com/getconfig/pubvendors?es=2&pubs=%@"; + +typedef NSString *PACStoreKey NS_STRING_ENUM; +static PACStoreKey const PACStoreKeyTaggedForUnderAgeOfConsent = @"tag_for_under_age_of_consent"; +static PACStoreKey const PACStoreKeyConsentStatus = @"consent_state"; +static PACStoreKey const PACStoreKeyPublisherIdentifiers = @"pub_ids"; +static PACStoreKey const PACStoreKeyHasAnyNonPersonalizedPublisherIdentifier = + @"has_any_npa_pub_id"; +static PACStoreKey const PACStoreKeyProviders = @"providers"; +static PACStoreKey const PACStoreKeyConsentedProviders = @"consented_providers"; +static PACStoreKey const PACStoreKeyRawResponse = @"raw_response"; +static PACStoreKey const PACStoreKeyIsRequestInEEAOrUnknown = @"is_request_in_eea_or_unknown"; + +typedef NSString *PACConsentStatusString NS_STRING_ENUM; +static PACConsentStatusString const PACConsentStatusStringUnknown = @"unknown"; +static PACConsentStatusString const PACConsentStatusStringNonPersonalized = @"non_personalized"; +static PACConsentStatusString const PACConsentStatusStringPersonalized = @"personalized"; +static PACConsentStatusString const PACConsentStatusStringNotEEA = @"not_eea"; + +#pragma mark - Support Functions + +/// Returns the consent status string for the provided status. +static PACConsentStatusString _Nonnull PACConsentStatusStringForStatus(PACConsentStatus status) { + switch (status) { + case PACConsentStatusNonPersonalized: + return PACConsentStatusStringNonPersonalized; + case PACConsentStatusPersonalized: + return PACConsentStatusStringPersonalized; + case PACConsentStatusUnknown: + return PACConsentStatusStringUnknown; + } +} + +/// Returns the consent status for the provided status string. +static PACConsentStatus PACConsentStatusForStatusString(NSString *_Nullable statusString) { + NSDictionary *statusStringValueMapping = @{ + PACConsentStatusStringUnknown : @(PACConsentStatusUnknown), + PACConsentStatusStringNonPersonalized : @(PACConsentStatusNonPersonalized), + PACConsentStatusStringPersonalized : @(PACConsentStatusPersonalized) + }; + NSNumber *statusValue = statusStringValueMapping[statusString]; + return statusValue == nil ? PACConsentStatusUnknown : statusValue.integerValue; +} + +/// Returns an array of ad provider dictionary representations for the provided ad providers. +static NSArray *_Nonnull +PACSerializeAdProviders(NSOrderedSet *_Nullable providers) { + NSMutableArray *serializedProviders = [[NSMutableArray alloc] init]; + for (PACAdProvider *provider in providers) { + [serializedProviders addObject:provider.dictionaryRepresentation]; + } + return serializedProviders; +} + +/// Returns an ordered set of ad providers for the provided array of ad provider dictionary +/// representations. +static NSOrderedSet *_Nonnull +PACDeserializeAdProviders(NSArray *_Nullable serializedProviders) { + NSMutableOrderedSet *adProviders = [[NSMutableOrderedSet alloc] init]; + for (NSDictionary *providerInfo in serializedProviders) { + PACAdProvider *provider = [[PACAdProvider alloc] initWithDictionary:providerInfo]; + if (provider) { + [adProviders addObject:provider]; + } + } + return adProviders; +} + +#pragma mark - PACAdProvider + +@implementation PACAdProvider + +- (nullable instancetype)initWithDictionary:(nullable NSDictionary *)dictionary { + self = [super init]; + if (self) { + _dictionaryRepresentation = [dictionary copy]; + _identifier = [_dictionaryRepresentation[@"company_id"] copy]; + _name = [_dictionaryRepresentation[@"company_name"] copy]; + _privacyPolicyURL = [NSURL URLWithString:_dictionaryRepresentation[@"policy_url"]]; + if (!_dictionaryRepresentation || _identifier == nil || !_name || !_privacyPolicyURL) { + return nil; + } + } + return self; +} + +- (NSUInteger)hash { + return _dictionaryRepresentation.hash; +} + +- (BOOL)isEqual:(nullable id)object { + if (![object isKindOfClass:[PACAdProvider class]]) { + return NO; + } + PACAdProvider *other = object; + return [_dictionaryRepresentation isEqual:other.dictionaryRepresentation]; +} + +@end + +#pragma mark - PACConsentInformation + +@implementation PACConsentInformation { + NSString *_rawResponse; + NSSet *_publisherIdentifiers; + NSOrderedSet *_adProviders; + NSOrderedSet *_consentedProviders; + PACConsentStatus _status; + BOOL _tagForUnderAgeOfConsent; + BOOL _isRequestInEEAOrUnknown; + BOOL _hasAnyNonPersonalizedPublisherIdentifier; +} + ++ (PACConsentInformation *)sharedInstance { + static PACConsentInformation *sharedInstance; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [[PACConsentInformation alloc] init]; + [sharedInstance readStatusFromUserDefaults]; + }); + return sharedInstance; +} + +- (NSArray *)adProviders { + PAC_MUST_BE_MAIN_THREAD(); + return _adProviders.array; +} + +- (BOOL)isRequestLocationInEEAOrUnknown { + PAC_MUST_BE_MAIN_THREAD(); + return _isRequestInEEAOrUnknown; +} + +- (void)setConsentStatus:(PACConsentStatus)status { + PAC_MUST_BE_MAIN_THREAD(); + _status = status; + switch (_status) { + case PACConsentStatusUnknown: + _consentedProviders = nil; + break; + case PACConsentStatusNonPersonalized: + case PACConsentStatusPersonalized: + _consentedProviders = _adProviders; + break; + } + [self writeStatusToUserDefaults]; +} + +- (PACConsentStatus)consentStatus { + PAC_MUST_BE_MAIN_THREAD(); + return _status; +} + +- (void)setTagForUnderAgeOfConsent:(BOOL)tagForUnderAgeOfConsent { + PAC_MUST_BE_MAIN_THREAD(); + if (_tagForUnderAgeOfConsent == tagForUnderAgeOfConsent) { + return; + } + _tagForUnderAgeOfConsent = tagForUnderAgeOfConsent; + [self writeStatusToUserDefaults]; +} + +- (BOOL)isTaggedForUnderAgeOfConsent { + PAC_MUST_BE_MAIN_THREAD(); + return _tagForUnderAgeOfConsent; +} + +- (void)reset { + PAC_MUST_BE_MAIN_THREAD(); + [NSUserDefaults.standardUserDefaults removeObjectForKey:PACUserDefaultsRootKey]; + [self readStatusFromUserDefaults]; + [self writeStatusToUserDefaults]; +} + +/// Reads status from user defaults and updates the receiver's information. +- (void)readStatusFromUserDefaults { + NSDictionary *info = + [NSUserDefaults.standardUserDefaults objectForKey:PACUserDefaultsRootKey]; + + NSNumber *tagForUnderAgeOfConsentValue = info[PACStoreKeyTaggedForUnderAgeOfConsent]; + _tagForUnderAgeOfConsent = tagForUnderAgeOfConsentValue.integerValue != 0; + + NSNumber *isRequestInEEAOrUnknownValue = info[PACStoreKeyIsRequestInEEAOrUnknown]; + _isRequestInEEAOrUnknown = isRequestInEEAOrUnknownValue.integerValue != 0; + + NSNumber *hasAnyNonPersonalizedPublisherIdentifierValue = + info[PACStoreKeyHasAnyNonPersonalizedPublisherIdentifier]; + _hasAnyNonPersonalizedPublisherIdentifier = + hasAnyNonPersonalizedPublisherIdentifierValue.integerValue != 0; + + _status = PACConsentStatusForStatusString(info[PACStoreKeyConsentStatus]); + + NSArray *publisherIdentifierArray = info[PACStoreKeyPublisherIdentifiers] ?: @[]; + _publisherIdentifiers = [[NSSet alloc] initWithArray:publisherIdentifierArray]; + + _adProviders = PACDeserializeAdProviders(info[PACStoreKeyProviders]); + _consentedProviders = PACDeserializeAdProviders(info[PACStoreKeyConsentedProviders]); +} + +/// Writes status to user defaults. +- (void)writeStatusToUserDefaults { + PAC_MUST_BE_MAIN_THREAD(); + + NSDictionary *personalizedAdStatus = @{ + PACStoreKeyTaggedForUnderAgeOfConsent : _tagForUnderAgeOfConsent ? @1 : @0, + PACStoreKeyIsRequestInEEAOrUnknown : _isRequestInEEAOrUnknown ? @1 : @0, + PACStoreKeyHasAnyNonPersonalizedPublisherIdentifier : + _hasAnyNonPersonalizedPublisherIdentifier ? @1 : @0, + PACStoreKeyConsentStatus : PACConsentStatusStringForStatus(_status), + PACStoreKeyPublisherIdentifiers : _publisherIdentifiers.allObjects ?: @[], + PACStoreKeyProviders : PACSerializeAdProviders(_adProviders), + PACStoreKeyConsentedProviders : PACSerializeAdProviders(_consentedProviders), + PACStoreKeyRawResponse : _rawResponse ?: @"", + }; + + [NSUserDefaults.standardUserDefaults setObject:personalizedAdStatus + forKey:PACUserDefaultsRootKey]; +} + +- (BOOL)debugModeEnabled { +#if TARGET_IPHONE_SIMULATOR + return YES; +#else + NSString *identifier = ASIdentifierManager.sharedManager.advertisingIdentifier.UUIDString; + return [_debugIdentifiers containsObject:identifier]; +#endif +} + +/// Returns an info update URL for the provided publisher identifiers. +- (nullable NSURL *)infoUpdateURLForPublisherIdentifiers: + (nonnull NSArray *)publisherIdentifiers { + NSString *publisherIdentifierString = [publisherIdentifiers componentsJoinedByString:@","]; + if (!publisherIdentifierString.length) { + publisherIdentifierString = @""; + } + NSString *infoUpdateURLString = + [[NSString alloc] initWithFormat:PACInfoUpdateURLFormat, publisherIdentifierString]; + + if ([self debugModeEnabled]) { + NSString *debugGeographyParam = @""; + switch (_debugGeography) { + case PACDebugGeographyEEA: + debugGeographyParam = @"&debug_geo=1"; + break; + case PACDebugGeographyNotEEA: + debugGeographyParam = @"&debug_geo=2"; + break; + case PACDebugGeographyDisabled: + debugGeographyParam = @""; + break; + } + infoUpdateURLString = [infoUpdateURLString stringByAppendingString:debugGeographyParam]; + } + + NSURL *infoUpdateURL = [NSURL URLWithString:infoUpdateURLString]; + return infoUpdateURL; +} + +- (void)requestConsentInfoUpdateForPublisherIdentifiers: + (nonnull NSArray *)publisherIdentifiers + completionHandler: + (nonnull PACConsentInformationUpdateHandler)handler { + PAC_MUST_BE_MAIN_THREAD(); + + NSSet *publisherIdentifiersSet = [[NSSet alloc] initWithArray:publisherIdentifiers]; + if (_publisherIdentifiers.count && ![_publisherIdentifiers isEqual:publisherIdentifiersSet]) { + NSLog(@"Warning: publisher identifiers changed since last info update request. publisher " + @"identifiers shouldn't change during an app session."); + } + + // Calls handler asynchronously. + PACConsentInformationUpdateHandler asyncHandler = ^(NSError *_Nullable error) { + if (!handler) { + return; + } + dispatch_async(dispatch_get_main_queue(), ^{ + handler(error); + }); + }; + NSURL *infoURL = [self infoUpdateURLForPublisherIdentifiers:publisherIdentifiers]; + NSURLSession *session = [NSURLSession sharedSession]; + NSURLSessionDataTask *dataTask = + [session dataTaskWithURL:infoURL + completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, + NSError *_Nullable error) { + if (error || !data.length) { + if (!error) { + error = PACErrorWithDescription(@"Unable to update publisher identifier info."); + } + asyncHandler(error); + return; + } + + NSDictionary *info = + [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; + if (error || ![info isKindOfClass:[NSDictionary class]]) { + if (!error) { + error = PACErrorWithDescription(@"Invalid response."); + } + asyncHandler(error); + return; + } + + NSString *responseString = + [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [self handleInfoUpdateResponse:publisherIdentifiers + info:info + responseString:responseString + asyncCompletionHandler:asyncHandler]; + }); + }]; + [dataTask resume]; +} + +- (nullable NSError *)validateInfo:(nonnull NSDictionary *)info { + // Validate EEA or unknown only. + NSNumber *requestInEEAValue = info[@"is_request_in_eea_or_unknown"]; + BOOL isRequestInEEAOrUnknown = requestInEEAValue.boolValue; + if (!isRequestInEEAOrUnknown) { + return nil; + } + + // Identify unmatched, not found, and publisher identifiers lookup failures. + NSMutableArray *lookupFailedPublisherIdentifiers = [[NSMutableArray alloc] init]; + NSMutableArray *notFoundPublisherIdentifiers = [[NSMutableArray alloc] init]; + NSArray *adNetworks = info[@"ad_network_ids"]; + for (NSDictionary *adNetwork in adNetworks) { + NSString *publisherIdentifier = adNetwork[@"ad_network_id"] ?: @"Publisher identifier missing"; + NSNumber *lookupFailed = adNetwork[@"lookup_failed"] ?: @NO; + NSNumber *notFound = adNetwork[@"not_found"] ?: @NO; + if (lookupFailed.boolValue) { + [lookupFailedPublisherIdentifiers addObject:publisherIdentifier]; + } + if (notFound.boolValue) { + [notFoundPublisherIdentifiers addObject:publisherIdentifier]; + } + } + + if (!lookupFailedPublisherIdentifiers.count && !notFoundPublisherIdentifiers.count) { + return nil; + } + + NSMutableArray *errorMessages = [[NSMutableArray alloc] init]; + [errorMessages addObject:@"Response error."]; + + if (lookupFailedPublisherIdentifiers.count) { + NSString *commaSeparatedPublisherIdentifiers = + [lookupFailedPublisherIdentifiers componentsJoinedByString:@", "]; + NSString *message = [[NSString alloc] + initWithFormat:@"Lookup failure for: %@", commaSeparatedPublisherIdentifiers]; + [errorMessages addObject:message]; + } + + if (notFoundPublisherIdentifiers.count) { + NSString *commaSeparatedPublisherIdentifiers = + [notFoundPublisherIdentifiers componentsJoinedByString:@", "]; + NSString *message = [[NSString alloc] + initWithFormat:@"Publisher identifiers not found: %@", commaSeparatedPublisherIdentifiers]; + [errorMessages addObject:message]; + } + + NSString *errorDescription = [errorMessages componentsJoinedByString:@" "]; + NSError *error = PACErrorWithDescription(errorDescription); + return error; +} + +/// Handles info update response. +- (void)handleInfoUpdateResponse:(nonnull NSArray *)publisherIdentifiers + info:(nonnull NSDictionary *)info + responseString:(nonnull NSString *)responseString + asyncCompletionHandler:(nonnull PACConsentInformationUpdateHandler)asyncHandler { + PAC_MUST_BE_MAIN_THREAD(); + // Validate response. + NSError *error = [self validateInfo:info]; + if (error) { + asyncHandler(error); + return; + } + + // Identify if any publisher identifier is configured for "non-personalized only". + BOOL previousHasAnyNonPersonalizedPublisherIdentifier = _hasAnyNonPersonalizedPublisherIdentifier; + _hasAnyNonPersonalizedPublisherIdentifier = NO; + NSMutableSet *nonPersonalizedOnlyProviderIdentifiers = [[NSMutableSet alloc] init]; + NSArray *adNetworkInfoDictionaries = info[@"ad_network_ids"]; + for (NSDictionary *adNetworkInfo in adNetworkInfoDictionaries) { + NSNumber *isNonPersonalizedValue = adNetworkInfo[@"is_npa"]; + if (!isNonPersonalizedValue.boolValue) { + continue; + } + _hasAnyNonPersonalizedPublisherIdentifier = YES; + NSArray *providerIdentifiers = adNetworkInfo[@"company_ids"]; + if (providerIdentifiers) { + [nonPersonalizedOnlyProviderIdentifiers addObjectsFromArray:providerIdentifiers]; + } + } + + // Raw response. + _rawResponse = responseString; + + // Publisher identifiers. + _publisherIdentifiers = [[NSSet alloc] initWithArray:publisherIdentifiers]; + + // Request origin. + NSNumber *requestInEEAValue = info[@"is_request_in_eea_or_unknown"]; + _isRequestInEEAOrUnknown = requestInEEAValue.boolValue; + + // Process providers. + NSArray *providersInfo = info[@"companies"]; + NSMutableOrderedSet *providers = [[NSMutableOrderedSet alloc] init]; + for (NSDictionary *providerInfo in providersInfo) { + PACAdProvider *provider = [[PACAdProvider alloc] initWithDictionary:providerInfo]; + if (!provider) { + continue; + } + // If any publisher identifier is configured for "non-personalized only", include + // "non-personalized only" providers in the provider array. Exclude all others. + if (!_hasAnyNonPersonalizedPublisherIdentifier || + [nonPersonalizedOnlyProviderIdentifiers containsObject:provider.identifier]) { + [providers addObject:provider]; + } + } + _adProviders = providers; + + BOOL nonPersonalizedOnlyValueChanged = + _hasAnyNonPersonalizedPublisherIdentifier != previousHasAnyNonPersonalizedPublisherIdentifier; + BOOL providersMatchesConsentedProviders = [_consentedProviders.set isEqual:_adProviders.set]; + if (_isRequestInEEAOrUnknown && + (!providersMatchesConsentedProviders || nonPersonalizedOnlyValueChanged)) { + _consentedProviders = nil; + _status = PACConsentStatusUnknown; + } + + [self writeStatusToUserDefaults]; + asyncHandler(nil); +} + +@end diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACView.h b/PersonalizedAdConsent/PersonalizedAdConsent/PACView.h new file mode 100755 index 0000000..32323ea --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACView.h @@ -0,0 +1,37 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "PACConsentForm.h" + +typedef NSString *PACFormKey NS_STRING_ENUM; +static PACFormKey const PACFormKeyOfferPersonalized = @"offer_personalized"; +static PACFormKey const PACFormKeyOfferNonPersonalized = @"offer_non_personalized"; +static PACFormKey const PACFormKeyOfferAdFree = @"offer_ad_free"; +static PACFormKey const PACFormKeyAppPrivacyPolicyURLString = @"app_privacy_url"; +static PACFormKey const PACFormKeyConstentInfo = @"consent_info"; +static PACFormKey const PACFormKeyAppName = @"app_name"; +static PACFormKey const PACFormKeyAppIcon = @"app_icon"; + +/// Loads and displays the consent form. +@interface PACView : UIView +@property(nonatomic, nullable) PACDismissCompletion dismissCompletion; +@property(nonatomic) BOOL shouldNonPersonalizedAds; +@property(nonatomic) BOOL shouldOfferAdFree; + +/// Loads the view with form information and calls the handler on the main queue. +- (void)loadWithFormInformation:(nonnull NSDictionary *)formInformation + completionHandler:(nonnull PACLoadCompletion)handler; +@end diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PACView.m b/PersonalizedAdConsent/PersonalizedAdConsent/PACView.m new file mode 100755 index 0000000..095fcb6 --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PACView.m @@ -0,0 +1,332 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "PACView.h" + +#import "PACError.h" + +/// Dictionary keys for processed form status strings. +typedef NSString *PACFormStatusKey NS_STRING_ENUM; +/// Error object key. +static PACFormStatusKey const PACFormStatusKeyError = @"error"; +/// Indicates whether the user chose the ad-free option. +static PACFormStatusKey const PACFormStatusKeyUserPrefersAdFree = @"ad_free"; + +/// Returns a JSON encoded string for the provided dictionary. Returns JSON encoded empty dictionary +/// if the encoding fails. +static NSString *_Nullable PACJSONStringForDictionary(NSDictionary *_Nonnull dictionary) { + NSCAssert([NSJSONSerialization isValidJSONObject:dictionary], @"Must be valid JSON object."); + NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:NULL]; + if (!data.length) { + return @"{}"; + } + NSString *JSONString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + return JSONString; +} + +/// Returns the application's short name. +static NSString *_Nonnull PACShortAppName(void) { + NSBundle *mainBundle = [NSBundle mainBundle]; + NSDictionary *localizedInfoDictionary = [mainBundle localizedInfoDictionary]; + + NSString *shortAppName = localizedInfoDictionary[@"CFBundleDisplayName"]; + if (shortAppName) { + return shortAppName; + } + + NSDictionary *infoDictionary = [mainBundle infoDictionary]; + shortAppName = infoDictionary[@"CFBundleDisplayName"]; + if (shortAppName) { + return shortAppName; + } + + shortAppName = localizedInfoDictionary[@"CFBundleName"]; + if (shortAppName) { + return shortAppName; + } + + shortAppName = infoDictionary[@"CFBundleName"]; + if (shortAppName) { + return shortAppName; + } + + return @""; +} + +/// Returns the application's icon as a data URI string. +static NSString *_Nonnull PACIconDataURIString(void) { + NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; + NSArray *iconFiles = + infoDictionary[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"]; + NSString *iconName = iconFiles.lastObject; + if (!iconName) { + return @""; + } + + UIImage *iconImage = [UIImage imageNamed:iconName]; + NSData *iconData = UIImagePNGRepresentation(iconImage); + NSString *iconBase64String = [iconData base64EncodedStringWithOptions:0]; + + return [@"data:image/png;base64," stringByAppendingString:iconBase64String]; +} + +/// 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 }; + NSString *wrappedArgsJSONString = PACJSONStringForDictionary(wrappedArgs); + NSString *command = [[NSString alloc] + initWithFormat:@"setTimeout(function(){%@(%@);}, 1);", functionName, wrappedArgsJSONString]; + return command; +} + +/// Returns YES if the status string represents an error status. +static BOOL PACIsErrorStatusString(NSString *_Nonnull statusString) { + 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) { + NSString *queryString = URL.query; + if (!queryString) { + NSString *resourceSpecifier = URL.resourceSpecifier; + NSRange questionPosition = [resourceSpecifier rangeOfString:@"?"]; + if (questionPosition.location != NSNotFound) { + queryString = [URL.resourceSpecifier + substringFromIndex:questionPosition.location + questionPosition.length]; + } + } + + NSArray *keyValuePairStrings = [queryString componentsSeparatedByString:@"&"]; + NSMutableDictionary *parameterDictionary = [[NSMutableDictionary alloc] init]; + + [keyValuePairStrings + enumerateObjectsUsingBlock:^(NSString *pairString, NSUInteger idx, BOOL *stop) { + NSArray *keyValuePair = [pairString componentsSeparatedByString:@"="]; + if (keyValuePair.count != 2) { + return; + } + NSString *key = keyValuePair.firstObject.stringByRemovingPercentEncoding; + NSString *value = keyValuePair.lastObject.stringByRemovingPercentEncoding; + if (value && key) { + parameterDictionary[key] = value; + } + }]; + + return parameterDictionary; +} + +@interface PACView () +@end + +@implementation PACView { + UIWebView *_webView; + NSDictionary *_formInformation; + PACLoadCompletion _loadCompletionHandler; +} + +- (instancetype)initWithFrame:(CGRect)frame { + self = [super initWithFrame:frame]; + if (self) { + self.backgroundColor = UIColor.clearColor; + self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + + _webView = [[UIWebView alloc] initWithFrame:frame]; + _webView.delegate = self; + _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + _webView.backgroundColor = UIColor.clearColor; + _webView.opaque = NO; + _webView.scrollView.bounces = NO; + + [self addSubview:_webView]; + } + return self; +} + +- (void)loadWithFormInformation:(nonnull NSDictionary *)formInformation + completionHandler:(nonnull PACLoadCompletion)handler { + formInformation = [formInformation copy]; + dispatch_async(dispatch_get_main_queue(), ^{ + if (self->_loadCompletionHandler) { + // In progress. + NSError *error = PACErrorWithDescription(@"Another load is in progress."); + if (handler) { + handler(error); + } + return; + } + self->_formInformation = formInformation; + self->_loadCompletionHandler = ^(NSError *_Nullable error) { + if (handler) { + handler(error); + } + }; + [self loadWebView]; + }); +} + +/// Loads the consent form HTML into the web view. +- (void)loadWebView { + NSURL *bundleURL = + [[NSBundle mainBundle] URLForResource:@"PersonalizedAdConsent" withExtension:@"bundle"]; + NSBundle *bundle = [NSBundle bundleWithURL:bundleURL]; + NSURL *URL = [bundle URLForResource:@"consentform" withExtension:@"html"]; + NSURLRequest *URLRequest = [[NSURLRequest alloc] initWithURL:URL]; + [_webView loadRequest:URLRequest]; +} + +/// Updates the web view with consent form and app information. +- (void)updateWebViewInformation { + dispatch_async(dispatch_get_main_queue(), ^{ + NSMutableDictionary *mutableFormInformation = + [self->_formInformation mutableCopy]; + mutableFormInformation[PACFormKeyAppName] = PACShortAppName(); + mutableFormInformation[PACFormKeyAppIcon] = PACIconDataURIString(); + + NSString *infoString = PACJSONStringForDictionary(mutableFormInformation); + NSString *command = PACCreateJavaScriptCommandString(@"setUpConsentDialog", @{ + @"info" : infoString + }); + [self->_webView stringByEvaluatingJavaScriptFromString:command]; + }); +} + +/// Handles load completion. +- (void)loadCompletedWithError:(nullable NSError *)error { + dispatch_async(dispatch_get_main_queue(), ^{ + PAC_MUST_BE_MAIN_THREAD(); + if (self->_loadCompletionHandler) { + self->_loadCompletionHandler(error); + } + self->_loadCompletionHandler = nil; + }); +} + +/// Handles dismissing the view. +- (void)dismissWithStatusString:(nullable NSString *)statusString { + NSDictionary *formStatus = [self formStatusForStatusString:statusString]; + dispatch_async(dispatch_get_main_queue(), ^{ + PAC_MUST_BE_MAIN_THREAD(); + if (self->_dismissCompletion) { + NSError *error = formStatus[PACFormStatusKeyError]; + NSNumber *userPrefersAdFreeNumber = formStatus[PACFormStatusKeyUserPrefersAdFree]; + self->_dismissCompletion(error, userPrefersAdFreeNumber.boolValue); + } + self->_dismissCompletion = nil; + }); +} + +/// Handles showing the URL in a browser. +- (void)showBrowser:(nonnull NSURL *)URL { + UIApplication *sharedApplication = UIApplication.sharedApplication; + if ([sharedApplication respondsToSelector:@selector(openURL:options:completionHandler:)]) { + if (@available(iOS 10.0, *)) { + [sharedApplication openURL:URL options:@{} completionHandler:nil]; + } + } else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + [UIApplication.sharedApplication openURL:URL]; +#pragma clang diagnostic pop + } +} + +/// Returns a form status dictionary for the provided status string. +- (NSDictionary *)formStatusForStatusString: + (nullable NSString *)statusString { + NSMutableDictionary *formStatus = [[NSMutableDictionary alloc] init]; + // Handle errors and ad-free option. + if (!statusString.length) { + formStatus[PACFormStatusKeyError] = PACErrorWithDescription(@"No information."); + } + if (PACIsErrorStatusString(statusString)) { + formStatus[PACFormStatusKeyError] = PACErrorWithDescription(statusString); + } + + formStatus[PACFormStatusKeyUserPrefersAdFree] = @([statusString isEqual:@"ad_free"]); + + // Handle personalized ad consent. + BOOL selectedNonPersonalizedAds = [statusString isEqual:@"non_personalized"]; + BOOL selectedPersonalizedAds = [statusString isEqual:@"personalized"]; + + if (selectedPersonalizedAds) { + PACConsentInformation.sharedInstance.consentStatus = PACConsentStatusPersonalized; + } else if (selectedNonPersonalizedAds) { + PACConsentInformation.sharedInstance.consentStatus = PACConsentStatusNonPersonalized; + } else { + PACConsentInformation.sharedInstance.consentStatus = PACConsentStatusUnknown; + } + + return formStatus; +} + +#pragma mark UIWebViewDelegate + +- (void)webViewDidFinishLoad:(UIWebView *)webView { + [self updateWebViewInformation]; +} + +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { + [self loadCompletedWithError:error]; +} + +- (BOOL)webView:(nonnull UIWebView *)webView + shouldStartLoadWithRequest:(nonnull NSURLRequest *)request + navigationType:(UIWebViewNavigationType)navigationType { + NSString *URLString = request.URL.absoluteString; + + if (![URLString hasPrefix:@"consent://"]) { + return YES; + } + + NSDictionary *parameters = PACQueryParametersFromURL(request.URL); + NSString *action = parameters[@"action"]; + NSCAssert(action.length > 0, @"Messages must have actions."); + + if ([action isEqual:@"load_complete"]) { + NSString *statusString = parameters[@"status"]; + NSError *loadError = nil; + if (!statusString.length) { + loadError = PACErrorWithDescription(@"No information."); + } + if (PACIsErrorStatusString(statusString)) { + loadError = PACErrorWithDescription(statusString); + } + + // Load was successful if the status string isn't an empty or error string. + [self loadCompletedWithError:loadError]; + } + + if ([action isEqual:@"dismiss"]) { + NSString *statusString = parameters[@"status"]; + [self dismissWithStatusString:statusString]; + } + + if ([action isEqual:@"browser"]) { + NSString *URLString = parameters[@"url"]; + NSURL *URL = [NSURL URLWithString:URLString]; + if (URL) { + [self showBrowser:URL]; + } + } + + return NO; +} + +@end diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.bundle/consentform.html b/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.bundle/consentform.html new file mode 100755 index 0000000..f11aa6d --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.bundle/consentform.html @@ -0,0 +1,849 @@ + + + + + + + + + + + + diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.h b/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.h new file mode 100755 index 0000000..3f1fa99 --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/PersonalizedAdConsent.h @@ -0,0 +1,18 @@ +// +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import +#import diff --git a/PersonalizedAdConsent/PersonalizedAdConsent/module.modulemap b/PersonalizedAdConsent/PersonalizedAdConsent/module.modulemap new file mode 100755 index 0000000..7e51d05 --- /dev/null +++ b/PersonalizedAdConsent/PersonalizedAdConsent/module.modulemap @@ -0,0 +1,9 @@ +framework module PersonalizedAdConsent { + umbrella header "PersonalizedAdConsent.h" + + export * + module * { export * } + + header "PACPersonalizedAdConsent.h" + header "PACConsentForm.h" +} diff --git a/README.md b/README.md new file mode 100755 index 0000000..4371368 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Google Mobile Ads Consent SDK + +Under the Google [EU User Consent +Policy](//google.com/about/company/consentstaging.html), you must make certain +disclosures to your users in the European Economic Area (EEA) and obtain their +consent to use cookies or other local storage, where legally required, and to +use personal data (such as AdID) to serve ads. This policy reflects the +requirements of the EU ePrivacy Directive and the General Data Protection +Regulation (GDPR). To support publishers in meeting their duties under this +policy, Google offers this Consent SDK. + +## Documentation + +For additional documentation on the Google Mobile Ads Consent SDK, refer to the +Consent SDK [developer docs](//developers.google.com/admob/ios/eu-consent). + +## License + +[Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0.html)