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

How to use SVProgressHUD in an App Extension? #432

Closed
tobihagemann opened this issue May 21, 2015 · 19 comments · Fixed by #991
Closed

How to use SVProgressHUD in an App Extension? #432

tobihagemann opened this issue May 21, 2015 · 19 comments · Fixed by #991

Comments

@tobihagemann
Copy link
Contributor

I've read the short passage about how to use SVProgressHUD in an App Extension, but I'm still getting errors and I have no idea how to properly use the SV_APP_EXTENSIONS macro.

I'm using CocoaPods and I'm not sure where I have to define that macro. Maybe it's easier to just look at this minimal project:

Any idea?

@tobihagemann
Copy link
Contributor Author

Okay... I've got it... first of all it has to be a preprocessor macro (that one was quite clear). More importantly it doesn't have to be defined in the App Extension target, but in the Pods-SVProgressHUD target.

@rickmak
Copy link

rickmak commented Jun 16, 2015

@MuscleRumble But if I manually modify Pods-SVProgressHUD target, it will get erase on next pos install. How you solve this problem.

@tobihagemann
Copy link
Contributor Author

By using the post_install hook of CocoaPods! Just put this in your Podfile:

post_install do |installer|
    installer.project.targets.each do |target|
        if target.name == "Pods-SVProgressHUD"
            target.build_configurations.each do |config|
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SV_APP_EXTENSIONS'
            end
        end
    end
end

@rickmak
Copy link

rickmak commented Jun 16, 2015

Thanks, it works.

@Dids
Copy link

Dids commented Jul 25, 2015

@MuscleRumble Have you managed to get this working using the latest version of Cocoapods?

I'm getting the following when regenerating my workspace:
CocoaPods/CocoaPods#3747

Fixed it by renaming installer.project.targets to installer.pods_project.targets, but even though 'pod update' goes through fine, Xcode is now complaining that extensions can't access sharedApplication, meaning the preprocessor macro didn't register.

@tobihagemann
Copy link
Contributor Author

I haven't tested the new version of CocoaPods yet, but I was also worried about Target Deduplication. It shouldn't affect App Extensions at all, but I haven't tested it yet.

I'll try the latest version of CocoaPods out on my projects and get back to you.

@Dids
Copy link

Dids commented Jul 25, 2015

I actually might've solved it. I was using Pods-MyExtension-SVProgressHUD before as the target name, but I switched back to just using "SVProgressHUD" as the name, which seems to work with the latest Cocoapods.

@tobihagemann
Copy link
Contributor Author

Alright, nice! :D Out of curiosity, do you have two Pod targets for SVProgressHUD or only one now? The thing is: I would like to have two targets, one for the main project, one for the extension. Because I wouldn't want to set SV_APP_EXTENSIONS on the main project. That's why I hope that target deduplication doesn't affect app extensions.

@Dids
Copy link

Dids commented Jul 25, 2015

I've defined a "shared" list of pods that includes SVProgressHUD, which I then include in both the main app target and the extension target (in the Podfile), like so:

def shared
pod 'SVProgressHUD'
end

target 'MyApp' do
pod 'SomePod'
shared
end

target 'MyAppExtension' do
pod 'SomeOtherPod'
shared
end

Previously in the post_install, I was looking for the SVProgressHUD pod that was exclusively built for my extension, then (and only then) setting the preprocessor macro. Now, with the changes to Cocoapods, it doesn't list target-specific pods anymore (no idea why), so I had to "globally" set SV_APP_EXTENSIONS in post_install.

@Dids
Copy link

Dids commented Jul 25, 2015

For some strange reason, I'm now getting duplicate symbols for each pod though, right after updating Cocoapods.

@Dids
Copy link

Dids commented Jul 25, 2015

Okay, so it turns out I just had to remove derived data, and remove any MyApp-PodName includes from Build Settings. Seems to compile now with the latest Cocoapods.
I also moved the post install script inside the extension's target definition, so who knows, maybe that will only set the flag when the extension is compiled.

@tobihagemann
Copy link
Contributor Author

Posted an issue on CocoaPods: CocoaPods/CocoaPods#3951

In short: I don't know how to set the preprocessor macro SV_APP_EXTENSIONS so that it only affects the app extension. Have you figured something out @Dids?

@tobihagemann
Copy link
Contributor Author

Just to keep everybody in the loop who's interested. I'm using now this pre_install hook from CocoaPods/CocoaPods#3794 (comment) to enforce duplication on SVProgressHUD.

pre_install do |installer|
    pod_targets = installer.pod_targets.flat_map do |pod_target|
        pod_target.name == "SVProgressHUD" ? pod_target.scoped : pod_target
    end
    installer.aggregate_targets.each do |aggregate_target|
        aggregate_target.pod_targets = pod_targets.select do |pod_target|
            pod_target.target_definitions.include?(aggregate_target.target_definition)
        end
    end
end

After that you can use the post_install hook, which has been discussed in the beginning, to add the preprocessor macro SV_APP_EXTENSIONS.

I think this is the best solution for now. It would be great if that "duplication enforcement" would be part of the Podfile DSL in future, but this is a CocoaPods issue. ;)

@honkmaster
Copy link
Member

Hi, we are aware of a problem regarding the Podfile (e.g. #449). It seems that the SVProgressHUD.bundle is not copied to the "Copy Bundle Resources" Build Phase. Due to this continuing problem we are searching for someone that could check and help with this problem. We are not using Cocoapods on our own and are happy for pull request.

100mango referenced this issue in m1entus/MZFormSheetPresentationController Apr 7, 2016
[Some APIs Are Unavailable to App
Extensions](https://developer.apple.com/library/ios/documentation/Genera
l/Conceptual/ExtensibilityPG/ExtensionOverview.html#//apple_ref/doc/uid/
TP40014214-CH2-SW6)

According To Apple Doc:

> Because of its focused role in the system, an app extension is
ineligible to participate in certain activities. An app extension
cannot:

> Access a sharedApplication object, and so cannot use any of the
methods on that object

Test in my own app.We need the `NS_EXTENSION_UNAVAILABLE_IOS` make it
available in App extension
@NSFish
Copy link

NSFish commented Dec 20, 2017

In case anyone come across this issue, now you can simply do

post_install do |installer|
    installer.project.targets.each do |target|
        if target.name == "SVProgressHUD-Pods-MyExtensionTargetName"
            target.build_configurations.each do |config|
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SV_APP_EXTENSIONS'
            end
        end
    end
end

Cocoapods will generate 2 seperated targets in Pods project, one with "SV_APP_EXTENSIONS" setup and one without it.

@pro0810
Copy link

pro0810 commented Jul 3, 2018

Hello everybody!

First thank you for tobihagemann help!

By the way, if I use above configuration, spinner is running in app side, but not running in share extension side. Of course, in share extension side, any issue not happened, but spinner not running even though I configured additionally call setViewForExtension: from share extensions view controller with self.view.

If I use only post_instll, spinner is only running in extension side, not running in app side.

Below is my podfile:

pre_install do |installer|
    pod_targets = installer.pod_targets.flat_map do |pod_target|
        pod_target.name == "SVProgressHUD" ? pod_target.scoped : pod_target
    end
    installer.aggregate_targets.each do |aggregate_target|
        aggregate_target.pod_targets = pod_targets.select do |pod_target|
            pod_target.target_definitions.include?(aggregate_target.target_definition)
        end
    end
end

target 'myTest' do
      pod 'SVProgressHUD', '2.1.2'
end

target 'share' do
     pod 'SVProgressHUD', '2.1.2'
end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        puts target.name
        if target.name == "SVProgressHUD-Pods-share"    
            target.build_configurations.each do |config|
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SV_APP_EXTENSIONS'
            end
        end
    end    
end

I want to know how to showing spinner both of app and share extension side.
Thank you.

@pablogeek
Copy link

Hello guys, since XCode 10, I'm getting duplicated issue in this solution. Do we have another solution for this?

@himanshujamnaniopenxcell
Copy link

himanshujamnaniopenxcell commented May 8, 2019

post_install do |installer|
installer.pods_project.targets.each do |target|
puts target.name
if target.name == "SVProgressHUD-Pods-share"
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SV_APP_EXTENSIONS'
end
end
end
end

Not working for me guys :(
Xcode : 10.2

@fukemy
Copy link

fukemy commented Mar 28, 2021

any new solution here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants