tbaranes / SwiftyUtils

All the reusable code that we need in each project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can the members that use UIApplication.shared be marked unavailable in an app extension?

Sweeper777 opened this issue · comments

I am trying to use SwiftyUtils in a Today Extensions target, and I get errors in places such as:

extension UIAlertController {

    @objc
    public func show(animated: Bool = true, completion: (() -> Void)? = nil) {
        UIApplication.shared.topViewController()?.present(self, animated: animated, completion: completion)
    }

}

I understand that this is because UIApplication.shared is unavailable in an extension.

I still want to be able to use SwiftyUtils in a today extension, is there some way to kind of conditionally compile the code that uses UIApplication.shared? Like a if #available check or something like that...

I never try it, but I'm not surprised that it doesn't work. I think you will have to add a processor flag IS_EXTENSION (Apple LLVM 6.0 - Preprocessing) in the library and in your appex, then use it to check when the code should be enabled or not:

extension UIAlertController {

#ifndef IS_EXTENSION
    @objc
    public func show(animated: Bool = true, completion: (() -> Void)? = nil) {
        UIApplication.shared.topViewController()?.present(self, animated: animated, completion: completion)
    }
#endif

}

I have no certitude it will work, but the approach I would take to make it work. It would be interesting to add a sample extension app in SwiftyUtils to see how it goes. I will give it a try with #117, let met know if you try meanwhile.

@Sweeper777 Sorry for the huge delay, I ending working on others things and forgot to check this issue. So, I just tried to make a today extension, and I've got no errors when importing SwiftyUtils. Did you find a solution to your issue or could you give more data to reproduce it?

Ok, I understand your issue, you'd like to have a compiler flag telling what you can use or not to avoid the runtime crash?

Since Apple don't do it for their own frameworks (UIKit for example), I don't think we will be able to do it. The best solution would be to use only what's available (meaning elements that are not crashing). I did a few tests, and I confirm that you can still use SwiftyUtils if you are using only things that are available for the today extension (just like when you are using UIKit).

To reproduce it, you have to put the pod 'SwiftyUtils' line in here:

target 'MyTodayExtension' do
  pod 'SwiftyUtils'
end

And even without using anything from SwiftyUtils, the compiler gives a bunch of errors

スクリーンショット 2020-04-26 10 25 44

On the other hand, it seems to work if I wrote inherit! :search_paths instead of pod 'SwiftyUtils'. For some reason, at that time I could not use inherit! :search_paths (I think it's because my main app uses a pod that's not available in an extension or something like that, can't remember the details 😞 ).

Am I not supposed to write pod 'SwiftyUtils' directly in the target 'MyTodayExtension' do block?

Am I not supposed to write pod 'SwiftyUtils' directly in the target 'MyTodayExtension' do block?

I don't think so. When I tried earlier I didn't import SwiftyUtils in the extension, but it was embedded in the app which had the frameworks. It would be interesting to try without adding SwiftyUtils to the extension in your Podfile.

If you have time to try this way, I would be interesting in the result. Otherwise, I will try to make a small project using CocoaPods like yours later.

I just tried not putting anything in the target 'MyTodayExtension' do block (so the pod 'SwiftyUtils' line is only in the target 'MyMainApp' do block), and the same errors appear. Then I tried to uninstall SwiftyUtils in main app, and the errors show up on another pod that I'm using.

So it turns out it's not a SwiftyUtils issue, more of a CocoaPods issue, or it's just my misuse of it...

Alright, thanks for the feedback! Hope you will find your issue.