From 202d927c896d9eff3db88a6134fc0e6e3d85ca32 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 18 Oct 2023 16:06:26 +0200 Subject: [PATCH] changes --- PostHog.xcodeproj/project.pbxproj | 20 ++++++++++++---- .../xcshareddata/xcschemes/PostHog.xcscheme | 2 +- .../xcschemes/PostHogExample.xcscheme | 2 +- .../xcschemes/PostHogTests.xcscheme | 24 +++++++++++++++++-- PostHog/PostHog.h | 2 ++ PostHog/PostHogExtensions.swift | 2 +- PostHog/PostHogFeatureFlags.swift | 2 +- PostHog/{PostHog.swift => PostHogSDK.swift} | 19 ++++++++------- PostHog/PostHogSessionManager.swift | 2 +- PostHog/Utils/NSData+PHGGZIP.m | 2 +- PostHogExample/Api.swift | 5 +++- PostHogExample/AppDelegate.swift | 6 ++--- PostHogExample/ContentView.swift | 19 +++++++++------ PostHogTests/CaptureTests.swift | 2 +- PostHogTests/FeatureFlagsTest.swift | 2 +- PostHogTests/PostHogTest.swift | 4 ++-- PostHogTests/QueueTest.swift | 8 +++---- PostHogTests/TestUtils/TestPostHog.swift | 4 ++-- 18 files changed, 84 insertions(+), 43 deletions(-) rename PostHog/{PostHog.swift => PostHogSDK.swift} (96%) diff --git a/PostHog.xcodeproj/project.pbxproj b/PostHog.xcodeproj/project.pbxproj index 445524677..c4cea5624 100644 --- a/PostHog.xcodeproj/project.pbxproj +++ b/PostHog.xcodeproj/project.pbxproj @@ -43,7 +43,7 @@ 3AE3FB332991388500AFFC18 /* PostHogQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB322991388500AFFC18 /* PostHogQueue.swift */; }; 3AE3FB37299162EA00AFFC18 /* PostHogApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB36299162EA00AFFC18 /* PostHogApi.swift */; }; 3AE3FB3B29924CE100AFFC18 /* NSData+PHGGZIP.m in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB3929924CE100AFFC18 /* NSData+PHGGZIP.m */; }; - 3AE3FB3D29924E8200AFFC18 /* PostHog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB3C29924E8200AFFC18 /* PostHog.swift */; }; + 3AE3FB3D29924E8200AFFC18 /* PostHogSDK.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB3C29924E8200AFFC18 /* PostHogSDK.swift */; }; 3AE3FB3F29924F4F00AFFC18 /* PostHogConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB3E29924F4F00AFFC18 /* PostHogConfig.swift */; }; 3AE3FB432992985A00AFFC18 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB422992985A00AFFC18 /* Reachability.swift */; }; 3AE3FB472992AB0000AFFC18 /* Hedgelog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB462992AB0000AFFC18 /* Hedgelog.swift */; }; @@ -141,7 +141,7 @@ 3AE3FB36299162EA00AFFC18 /* PostHogApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogApi.swift; sourceTree = ""; }; 3AE3FB3829924CE100AFFC18 /* NSData+PHGGZIP.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+PHGGZIP.h"; sourceTree = ""; }; 3AE3FB3929924CE100AFFC18 /* NSData+PHGGZIP.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+PHGGZIP.m"; sourceTree = ""; }; - 3AE3FB3C29924E8200AFFC18 /* PostHog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHog.swift; sourceTree = ""; }; + 3AE3FB3C29924E8200AFFC18 /* PostHogSDK.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogSDK.swift; sourceTree = ""; }; 3AE3FB3E29924F4F00AFFC18 /* PostHogConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogConfig.swift; sourceTree = ""; }; 3AE3FB422992985A00AFFC18 /* Reachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = ""; }; 3AE3FB462992AB0000AFFC18 /* Hedgelog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Hedgelog.swift; sourceTree = ""; }; @@ -296,7 +296,7 @@ 3AA4C09B2988315D006C4731 /* Utils */, 3AE3FB36299162EA00AFFC18 /* PostHogApi.swift */, 3AE3FB322991388500AFFC18 /* PostHogQueue.swift */, - 3AE3FB3C29924E8200AFFC18 /* PostHog.swift */, + 3AE3FB3C29924E8200AFFC18 /* PostHogSDK.swift */, 3AE3FB3E29924F4F00AFFC18 /* PostHogConfig.swift */, 3AE3FB4D2993D1D600AFFC18 /* PostHogSessionManager.swift */, 3AE3FB48299391DF00AFFC18 /* PostHogStorage.swift */, @@ -439,7 +439,7 @@ attributes = { BuildIndependentTargetsInParallel = 1; LastSwiftUpdateCheck = 1420; - LastUpgradeCheck = 1420; + LastUpgradeCheck = 1500; TargetAttributes = { 3AA34CF6296D951A003398F4 = { CreatedOnToolsVersion = 14.2; @@ -555,7 +555,7 @@ 6926DA8E2ADD2876005760D2 /* PostHogContext.swift in Sources */, 69261D192AD9673500232EC7 /* PostHogBatchUploadInfo.swift in Sources */, 3A0F108529C9ABB6002C0084 /* ReadWriteLock.swift in Sources */, - 3AE3FB3D29924E8200AFFC18 /* PostHog.swift in Sources */, + 3AE3FB3D29924E8200AFFC18 /* PostHogSDK.swift in Sources */, 3AE3FB3F29924F4F00AFFC18 /* PostHogConfig.swift in Sources */, 3AE3FB332991388500AFFC18 /* PostHogQueue.swift in Sources */, 3AE3FB3B29924CE100AFFC18 /* NSData+PHGGZIP.m in Sources */, @@ -746,6 +746,7 @@ DEBUG_INFORMATION_FORMAT = dwarf; ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; GCC_C_LANGUAGE_STANDARD = gnu11; GCC_DYNAMIC_NO_PIC = NO; GCC_NO_COMMON_BLOCKS = YES; @@ -810,6 +811,7 @@ DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; GCC_C_LANGUAGE_STANDARD = gnu11; GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; @@ -835,12 +837,14 @@ buildSettings = { BUILD_LIBRARY_FOR_DISTRIBUTION = YES; CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = ""; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_MODULE_VERIFIER = YES; EXCLUDED_ARCHS = ""; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_NSHumanReadableCopyright = ""; @@ -853,6 +857,8 @@ ); MACOSX_DEPLOYMENT_TARGET = 10.14; MARKETING_VERSION = 1.0; + MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++"; + MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++20"; PRODUCT_BUNDLE_IDENTIFIER = com.posthog.PostHog; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; @@ -873,12 +879,14 @@ buildSettings = { BUILD_LIBRARY_FOR_DISTRIBUTION = YES; CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = ""; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_MODULE_VERIFIER = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_NSHumanReadableCopyright = ""; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; @@ -890,6 +898,8 @@ ); MACOSX_DEPLOYMENT_TARGET = 10.14; MARKETING_VERSION = 1.0; + MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++"; + MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++20"; PRODUCT_BUNDLE_IDENTIFIER = com.posthog.PostHog; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; diff --git a/PostHog.xcodeproj/xcshareddata/xcschemes/PostHog.xcscheme b/PostHog.xcodeproj/xcshareddata/xcschemes/PostHog.xcscheme index d9eabc148..f03433206 100644 --- a/PostHog.xcodeproj/xcshareddata/xcschemes/PostHog.xcscheme +++ b/PostHog.xcodeproj/xcshareddata/xcschemes/PostHog.xcscheme @@ -1,6 +1,6 @@ + LastUpgradeVersion = "1500" + version = "2.2"> + + + + + + + + + + diff --git a/PostHog/PostHog.h b/PostHog/PostHog.h index 3623f4f50..902bb76a1 100644 --- a/PostHog/PostHog.h +++ b/PostHog/PostHog.h @@ -7,6 +7,8 @@ #import +// do not use <> +// 'NSData+PHGGZIP.h' file not found with include; use "quotes" instead #import "NSData+PHGGZIP.h" //! Project version number for PostHog. diff --git a/PostHog/PostHogExtensions.swift b/PostHog/PostHogExtensions.swift index 1ccb9e472..4a5c252b2 100644 --- a/PostHog/PostHogExtensions.swift +++ b/PostHog/PostHogExtensions.swift @@ -14,7 +14,7 @@ import Foundation */ -public extension PostHog { +public extension PostHogSDK { static let didStartNotification = Notification.Name("PostHogDidStart") // object: nil static let didStartRecordingNotification = Notification.Name("PostHogDidStartRecording") // object: nil static let didResetSessionNotification = Notification.Name("PostHogDidResetSession") // object: String diff --git a/PostHog/PostHogFeatureFlags.swift b/PostHog/PostHogFeatureFlags.swift index 3eab71f1a..6eec7629b 100644 --- a/PostHog/PostHogFeatureFlags.swift +++ b/PostHog/PostHogFeatureFlags.swift @@ -75,7 +75,7 @@ class PostHogFeatureFlags { } DispatchQueue.main.async { - NotificationCenter.default.post(name: PostHog.didReceiveFeatureFlags, object: nil) + NotificationCenter.default.post(name: PostHogSDK.didReceiveFeatureFlags, object: nil) } self.setLoading(false) diff --git a/PostHog/PostHog.swift b/PostHog/PostHogSDK.swift similarity index 96% rename from PostHog/PostHog.swift rename to PostHog/PostHogSDK.swift index 4228c4f82..923acb998 100644 --- a/PostHog/PostHog.swift +++ b/PostHog/PostHogSDK.swift @@ -14,7 +14,8 @@ import Foundation let retryDelay = 5.0 let maxRetryDelay = 30.0 -@objc public class PostHog: NSObject { +// renamed to PostHogSDK due to https://github.com/apple/swift/issues/56573 +@objc public class PostHogSDK: NSObject { private var config: PostHogConfig private init(_ config: PostHogConfig) { @@ -37,8 +38,8 @@ let maxRetryDelay = 30.0 private var context: PostHogContext? private static var apiKeys = Set() - @objc public static let shared: PostHog = { - let instance = PostHog(PostHogConfig(apiKey: "")) + @objc public static let shared: PostHogSDK = { + let instance = PostHogSDK(PostHogConfig(apiKey: "")) return instance }() @@ -61,10 +62,10 @@ let maxRetryDelay = 30.0 return } - if PostHog.apiKeys.contains(config.apiKey) { + if PostHogSDK.apiKeys.contains(config.apiKey) { hedgeLog("API Key: ${config.apiKey} already has a PostHog instance.") } else { - PostHog.apiKeys.insert(config.apiKey) + PostHogSDK.apiKeys.insert(config.apiKey) } enabled = true @@ -95,7 +96,7 @@ let maxRetryDelay = 30.0 queue?.start() DispatchQueue.main.async { - NotificationCenter.default.post(name: PostHog.didStartNotification, object: nil) + NotificationCenter.default.post(name: PostHogSDK.didStartNotification, object: nil) } if config.preloadFeatureFlags { @@ -495,7 +496,7 @@ let maxRetryDelay = 30.0 setupLock.withLock { enabled = false - PostHog.apiKeys.remove(config.apiKey) + PostHogSDK.apiKeys.remove(config.apiKey) queue?.stop() queue = nil @@ -509,8 +510,8 @@ let maxRetryDelay = 30.0 } } - @objc public static func with(_ config: PostHogConfig) -> PostHog { - let postHog = PostHog(config) + @objc public static func with(_ config: PostHogConfig) -> PostHogSDK { + let postHog = PostHogSDK(config) postHog.setup(config) return postHog } diff --git a/PostHog/PostHogSessionManager.swift b/PostHog/PostHogSessionManager.swift index 9fb325647..9e5f7d540 100644 --- a/PostHog/PostHogSessionManager.swift +++ b/PostHog/PostHogSessionManager.swift @@ -79,7 +79,7 @@ class PostHogSessionManager { hedgeLog("Session expired - creating new session '\(sessionId!)'") DispatchQueue.main.async { - NotificationCenter.default.post(name: PostHog.didResetSessionNotification, object: sessionId) + NotificationCenter.default.post(name: PostHogSDK.didResetSessionNotification, object: sessionId) } } diff --git a/PostHog/Utils/NSData+PHGGZIP.m b/PostHog/Utils/NSData+PHGGZIP.m index 5be9194c0..569683a93 100644 --- a/PostHog/Utils/NSData+PHGGZIP.m +++ b/PostHog/Utils/NSData+PHGGZIP.m @@ -38,7 +38,7 @@ #pragma clang diagnostic ignored "-Wcast-qual" -void *_Nullable posthog_libzOpen() +void *_Nullable posthog_libzOpen(void) { static void *libz; static dispatch_once_t onceToken; diff --git a/PostHogExample/Api.swift b/PostHogExample/Api.swift index 12d5f1459..b5070cee4 100644 --- a/PostHogExample/Api.swift +++ b/PostHogExample/Api.swift @@ -41,7 +41,10 @@ class Api: ObservableObject { logger.info("Requesting protected endpoint...") let task = URLSession.shared.dataTask(with: url) { data, _, _ in - print("Response", data) + if data == nil { + return + } + print("Response", String(decoding: data!, as: UTF8.self)) } task.resume() diff --git a/PostHogExample/AppDelegate.swift b/PostHogExample/AppDelegate.swift index abbcae3d1..f05dee37d 100644 --- a/PostHogExample/AppDelegate.swift +++ b/PostHogExample/AppDelegate.swift @@ -15,9 +15,9 @@ class AppDelegate: NSObject, UIApplicationDelegate { apiKey: "_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI" ) - PostHog.shared.setup(config) - PostHog.shared.debug() - PostHog.shared.capture("App started!") + PostHogSDK.shared.setup(config) + PostHogSDK.shared.debug() + PostHogSDK.shared.capture("App started!") // DispatchQueue.global(qos: .utility).async { // let task = Api().failingRequest() diff --git a/PostHogExample/ContentView.swift b/PostHogExample/ContentView.swift index 0250429d3..0f5cb8fed 100644 --- a/PostHogExample/ContentView.swift +++ b/PostHogExample/ContentView.swift @@ -22,7 +22,12 @@ class SignInViewModel: NSObject, ObservableObject, ASWebAuthenticationPresentati // Initialize the session. let session = ASWebAuthenticationSession(url: authURL, callbackURLScheme: scheme) { callbackURL, error in - print(callbackURL, error) + if callbackURL != nil { + print("URL", callbackURL!.absoluteString) + } + if error != nil { + print("Error", error!.localizedDescription) + } } session.presentationContextProvider = self session.prefersEphemeralWebBrowserSession = true @@ -38,19 +43,19 @@ class FeatureFlagsModel: ObservableObject { @Published var isReloading: Bool = false init() { - NotificationCenter.default.addObserver(self, selector: #selector(reloaded), name: PostHog.didReceiveFeatureFlags, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(reloaded), name: PostHogSDK.didReceiveFeatureFlags, object: nil) } @objc func reloaded() { - boolValue = PostHog.shared.isFeatureEnabled("4535-funnel-bar-viz") - stringValue = PostHog.shared.getFeatureFlag("multivariant") as? String - payloadValue = PostHog.shared.getFeatureFlagPayload("multivariant") as? [String: String] + boolValue = PostHogSDK.shared.isFeatureEnabled("4535-funnel-bar-viz") + stringValue = PostHogSDK.shared.getFeatureFlag("multivariant") as? String + payloadValue = PostHogSDK.shared.getFeatureFlagPayload("multivariant") as? [String: String] } func reload() { isReloading = true - PostHog.shared.reloadFeatureFlags { _, _ in + PostHogSDK.shared.reloadFeatureFlags { _, _ in self.isReloading = false } } @@ -71,7 +76,7 @@ struct ContentView: View { } func triggerIdentify() { - PostHog.shared.identify(name, userProperties: [ + PostHogSDK.shared.identify(name, userProperties: [ "name": name, ]) } diff --git a/PostHogTests/CaptureTests.swift b/PostHogTests/CaptureTests.swift index a27cf68a2..9b5ced40e 100644 --- a/PostHogTests/CaptureTests.swift +++ b/PostHogTests/CaptureTests.swift @@ -14,7 +14,7 @@ import Quick class CaptureTest: QuickSpec { override func spec() { var harness: TestPostHog! - var posthog: PostHog! + var posthog: PostHogSDK! beforeEach { harness = TestPostHog() diff --git a/PostHogTests/FeatureFlagsTest.swift b/PostHogTests/FeatureFlagsTest.swift index d7ec9d449..339d5b675 100644 --- a/PostHogTests/FeatureFlagsTest.swift +++ b/PostHogTests/FeatureFlagsTest.swift @@ -13,7 +13,7 @@ import Quick class FeatureFlagTests: QuickSpec { override func spec() { var harness: TestPostHog! - var posthog: PostHog! + var posthog: PostHogSDK! beforeEach { harness = TestPostHog() diff --git a/PostHogTests/PostHogTest.swift b/PostHogTests/PostHogTest.swift index c5e492996..c482638ab 100644 --- a/PostHogTests/PostHogTest.swift +++ b/PostHogTests/PostHogTest.swift @@ -6,7 +6,7 @@ import Quick class PostHogTest: QuickSpec { override func spec() { var harness: TestPostHog! - var posthog: PostHog! + var posthog: PostHogSDK! beforeEach { harness = TestPostHog() @@ -46,7 +46,7 @@ class PostHogTest: QuickSpec { let sessionId = posthog.getSessionId() let config = PostHogConfig(apiKey: "test-api-key") - let otherPostHog = PostHog.with(config) + let otherPostHog = PostHogSDK.with(config) let otherAnonymousId = otherPostHog.getAnonymousId() let otherDistinctId = otherPostHog.getDistinctId() diff --git a/PostHogTests/QueueTest.swift b/PostHogTests/QueueTest.swift index 8ea1f513f..26ffd7143 100644 --- a/PostHogTests/QueueTest.swift +++ b/PostHogTests/QueueTest.swift @@ -30,7 +30,7 @@ class QueueTest: QuickSpec { } it("Consumes items from the queue") { - var consumedEvents = [PostHogEvent]() + let consumedEvents = [PostHogEvent]() let expectation = self.expectation(description: "Callback") // queue.consume { payload in @@ -44,7 +44,7 @@ class QueueTest: QuickSpec { queue.add(PostHogEvent(event: "event3", distinctId: "123")) queue.flush() - self.wait(for: [expectation], timeout: 5) + await self.fulfillment(of: [expectation], timeout: 5) expect(consumedEvents.count) == 3 expect(consumedEvents[0].event) == "event1" @@ -54,7 +54,7 @@ class QueueTest: QuickSpec { } it("Returns processing to the queue if failed") { - var consumedEvents = [PostHogEvent]() + let consumedEvents = [PostHogEvent]() let expectation = self.expectation(description: "Callback") // queue.consume { payload in @@ -68,7 +68,7 @@ class QueueTest: QuickSpec { queue.add(PostHogEvent(event: "event3", distinctId: "123")) queue.flush() - self.wait(for: [expectation], timeout: 5) + await self.fulfillment(of: [expectation], timeout: 5) expect(consumedEvents.count) == 3 expect(queue.depth) == 3 } diff --git a/PostHogTests/TestUtils/TestPostHog.swift b/PostHogTests/TestUtils/TestPostHog.swift index 09e1ce2c1..2bd6a299c 100644 --- a/PostHogTests/TestUtils/TestPostHog.swift +++ b/PostHogTests/TestUtils/TestPostHog.swift @@ -11,13 +11,13 @@ import XCTest class TestPostHog { var server: MockPostHogServer! - var posthog: PostHog! + var posthog: PostHogSDK! init() { server = MockPostHogServer() server.start() let config = server.posthogConfig - posthog = PostHog.with(config) + posthog = PostHogSDK.with(config) } func stop() {