Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use posthog in an App Extension using Cocoapods #136

Open
BillCarsonFr opened this issue May 21, 2024 · 8 comments
Open

Cannot use posthog in an App Extension using Cocoapods #136

BillCarsonFr opened this issue May 21, 2024 · 8 comments
Labels

Comments

@BillCarsonFr
Copy link

Problem Statement

It is not possible to use posthog in an App Extension (Notification Service).
Build Fails here

for scene in UIApplication.shared.connectedScenes where scene.activationState == .foregroundActive {

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 BillCarsonFr added the enhancement New feature or request label May 21, 2024
@marandaneto
Copy link
Member

@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 marandaneto added the question Further information is requested label May 21, 2024
@BillCarsonFr
Copy link
Author

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.");

@marandaneto
Copy link
Member

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

@BillCarsonFr
Copy link
Author

BillCarsonFr commented May 21, 2024

@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

@marandaneto
Copy link
Member

marandaneto commented May 21, 2024

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).

@BillCarsonFr
Copy link
Author

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

@marandaneto marandaneto added AppExtension and removed question Further information is requested labels May 21, 2024
@marandaneto
Copy link
Member

marandaneto commented May 21, 2024

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 firebase/firebase-ios-sdk#8405 and GetStream/stream-chat-swift#1333

Workaround is to silence all warnings such as CocoaPods/CocoaPods#4423 but I don't think we should do this.

@BillCarsonFr
Copy link
Author

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

@marandaneto marandaneto changed the title Cannot use posthog in an App Extension Cannot use posthog in an App Extension using Cocoapods May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants