iOS 8 extension dependencies issues. Importing one

2019-04-28 20:21发布

问题:

I am working on iOS 8 extension. I read many manuals and all of them just show how simple add extension to your app, and seems that's enough.

But here are many pitfalls:

  1. After adding your extension you will need to import some of your classes to view controller that were created when you added new extension target. The big use here that you will need add all of them and if you have huge project it's not a simple task. Solution can be select extension target then in Build Phases -> Compile Sources press plus button and add all .m files to your target using hot key CMD+A.

  2. After adding all files you can see that some of method wont work, and you can see this error: 'sharedApplication' is unavailable: not available on iOS (App Extension) so the solution can be a macros that check ifndef Extension then we can invoke sharedApplication code.

  3. #import <Foundation/Foundation.h> vs #import <UIKit/UIKit.h>. So I have not figured out with this issue but when I replaced Foundation with UIKit it works for me and all related issues go away.

  4. CocoaPods. All of us are using CocoaPods so if your extension need to use some part of your project code and that code use CocoaPods library then you need to add link_with 'ProjectTarged', 'ExtensionTarget' to Pod file and make pod install again to bind your libraries with new extension target.

So this is a main points I faced with. Maybe someone can suggest how to solve that issue, as I said I just import one needed file to extension view controller. The imported file contain some libraries like AFNetworking, RestKit and other nested classes from the main project. I need this class to invoke few methods with passing data from extension to my backend server. So one file but many issues.

回答1:

you can use this in your Podfile to prevent "Require only App-Extension-Safe API". Just put it to the end of your Podfile.

post_install do |installer_representation|
installer_representation.project.targets.each do |target|
    target.build_configurations.each do |config|
        config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO'
        end
    end
end


回答2:

1) You only need to add files to the extension target that you actually intend on using. I would recommend only pulling over what you need by finding the files and in the File Inspector, adding them to both targets.

2) Yep that's right. You'll need to update libraries that check that for you or fork them and fix them yourself.

3) I think you're referring to the default templates when creating one of the app extensions. Yes, you need to use UIKit not Foundation. Foundation will work for iOS or OS X, but clearly isn't enough if you are making a UIKit application.

4) The link_with command will make all pods in your Podfile link to all of the targets listed. If that's what you need, then, fine, do that. If you just need a small subset of pods for your extension, use the following:

target 'whateverTarget', :exclusive => true do
    pod 'SomePod'
end


回答3:

To remove sharedApplication issue from CocoaPods Libraries you just need to change Build Options within Build Settings for your pod.

Just type to search Require Only App-Extension-Safe API and then change the value to NO as it is on the image below:

Propably you will need to do this for every of your pods.