Issues with iOS 8 extension dependencies. Import one project file into the extension view controller

I am working on an iOS 8 extension. I have read a lot of tutorials, and all of them just show how easy it is to add an extension to your application, and it seems to be enough.

But there are many traps here:

  • After adding the extension, you will need to import some of your classes to view the controller that was created when you added the new extension target. A great use here is that you will need to add everything, and if you have a huge project, this is not an easy task. The solution can be chosen by the expansion target, and then in the "Phase Assembly" → "Compile Sources" section, click the plus button and add all .m files to your target using the CMD + A hotkey.

  • After adding all the files, you can see that some of the methods do not work, and you can see this error: 'sharedApplication' is unavailable: not available on iOS (App Extension) so the solution can be a macro that checks ifndef Extension , then we can call the sharedApplication code.

  • #import <Foundation/Foundation.h> vs #import <UIKit/UIKit.h> . Therefore, I did not understand this problem, but when I replaced Foundation with UIKit , it works for me, and all the problems associated with it will disappear.

  • CocoaPods. We all use CocoaPods, so if your extension needs to use part of the project code, and this code uses the CocoaPods library, you need to add link_with 'ProjectTarged', 'ExtensionTarget' to the Pod file and do pod install again to link your libraries with a new purpose destination.

So these are the main points that I came across. Maybe someone can suggest how to solve this problem, as I said that I am just importing one necessary file into the extension controller. The imported file contains some libraries, such as AFNetworking , RestKit and other nested classes from the main project. I need this class to call several methods with data transfer from the extension to my server. So there is one file, but a lot of problems.

+6
source share
3 answers

you can use this in your subfile to prevent "Require only an API to extend applications." Just put it at the end of your subfile.

 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 
+5
source

1) You only need to add files to the extension target that you are actually going to use. I would recommend you only what you need to find files in File Inspector, adding them for both purposes.

2) Yes, that’s right. You will need to update libraries that check this for you or fork them and fix them yourself.

3) I think that you refer to the default templates when creating one of the application extensions. Yes, you need to use UIKit not Foundation. The foundation will work for iOS or OS X, but is clearly not enough if you are making a UIKit application.

4) The link_with command will make all the blocks in your link signatures to all specified goals. If this is what you need, then well, do it. If you just need a small subset of containers for your extension, use the following:

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

To remove the sharedApplication problem from CocoaPods Libraries, you just need to change the Assembly Settings inside the Assembly Settings for your module.

Just search for the Require Only App-Extension-Safe API , and then change the value to NO , as shown in the image below:

enter image description here

Simplified, you will need to do this for each of your containers.

+2
source

Source: https://habr.com/ru/post/984928/


All Articles