posthog-ios icon indicating copy to clipboard operation
posthog-ios copied to clipboard

Cannot use posthog in an App Extension using Cocoapods

Open BillCarsonFr opened this issue 1 year ago • 8 comments

Problem Statement

It is not possible to use posthog in an App Extension (Notification Service). Build Fails here https://github.com/PostHog/posthog-ios/blob/109e56bd9a96c7ad5ef4a4de0b6610aed5bab646/PostHog/UIViewController.swift#L34

With:

UIViewController.swift:34:44 'shared' is unavailable in application extensions for iOS: Use view controller based solutions where appropriate instead.

Solution Brainstorm

No response

BillCarsonFr avatar May 21 '24 10:05 BillCarsonFr

@BillCarsonFr by App Extension (Notification Service) do you mean a Notification Service that extends UNNotificationServiceExtension? If that's the case, that still works for me, can you provide a minimal reproducible example? Which version of the SDK are you using?

marandaneto avatar May 21 '24 11:05 marandaneto

UNNotificationServiceExtension

Yes UNNotificationServiceExtension

Using: pod 'PostHog', '~> 3.2.5'

It doesn't compile and rightfully?

@property(class, nonatomic, readonly) UIApplication *sharedApplication NS_EXTENSION_UNAVAILABLE_IOS("Use view controller based solutions where appropriate instead.");

BillCarsonFr avatar May 21 '24 11:05 BillCarsonFr

@BillCarsonFr are you trying to init the SDK on the UNNotificationServiceExtension class? can you provide a minimal reproducible example?

marandaneto avatar May 21 '24 11:05 marandaneto

@BillCarsonFr are you trying to init the SDK on the UNNotificationServiceExtension class? can you provide a minimal reproducible example?

Thx for your help Yes I was trying to get a minimal reproducible example. If I use SwiftPM I have no issue, but if I use cocoapod I have the problem. Just create a new project default template, add NSE extension. pod init, add pod 'PostHog', '~> 3.2.5' to podfile then pod install

BillCarsonFr avatar May 21 '24 12:05 BillCarsonFr

That's exactly what I did: https://github.com/PostHog/posthog-ios/tree/main/PostHogExampleWithPods I just added the NSE extension and compiled it normally.

can you do the very same, zip the folder or send me or just create a testing repo under your user on GitHub and send me the link? Could be related to your env. as well (Xcode, pod, etc).

marandaneto avatar May 21 '24 12:05 marandaneto

That's exactly what I did: https://github.com/PostHog/posthog-ios/tree/main/PostHogExampleWithPods I just added the NSE extension and compiled it normally.

can you do the very same, zip the folder or send me or just create a testing repo under your user on GitHub and send me the link? Could be related to your env. as well (Xcode, pod, etc).

https://github.com/BillCarsonFr/NSESimplePosthogReproduce

BillCarsonFr avatar May 21 '24 12:05 BillCarsonFr

Ok, now I can reproduce it.

I could annotate the method with @available(iOSApplicationExtension, unavailable) but then even the setup method is unavailable (because I have to annotate all the callers), which means you cannot use the SDK anyway, but it compiles.

There's no way to detect if it is an iOS app extension via compile flags (unless you define a custom compiler flag but I cannot do this as an SDK). Most SDKs just tag the methods and make them unavailable such as https://github.com/firebase/firebase-ios-sdk/pull/8405 and https://github.com/GetStream/stream-chat-swift/pull/1333/

Workaround is to silence all warnings such as https://github.com/CocoaPods/CocoaPods/issues/4423 but I don't think we should do this.

marandaneto avatar May 21 '24 14:05 marandaneto

Yes I came up with the same conclusion as you. I have switched to swiftPM as a workaround. Thx @marandaneto for looking at this so fast

BillCarsonFr avatar May 22 '24 07:05 BillCarsonFr