Use Cocoapods with an App Extension
Asked Answered
H

4

84

I'm trying to build a photo App Extension in Xcode 6 Beta-6 that uses cocoapods libraries. The bridging header that Xcode creates for the photo extension can't see anything from cocoapods.

For example: #import <GPUImage/GPUImage.h> results in the error "GPUImage/GPUImage.h" file not found.

I've tried every conceivable path for the import (with brackets and quotes) and have had almost no success. The exception is that for simple pods like SVProgressHUD, the following ugly terrible hack works: #import "../Pods/SVProgressHUD/SVProgressHUD/SVProgressHUD.h".

But for GPUImage, it walks into the GPUImage.h header and decides it suddenly can't see GPUImageContext.h despite having no issue when this is imported with the bridging header for the normal swift code that is not part of the app extension.

What is different about the compilation of app extensions that is preventing the bridging header from behaving sanely?

Note: I've read every possible permutation of this tutorial and it is not immediately applicable, just in case anyone thinks they have found the answer there.

Also, the problem described in this SO question may be related, but I asked this question anyway in case my issue is specific to app extensions.

Hurling answered 27/8, 2014 at 23:38 Comment(1)
this is the right solution for me [Cocopoads also for widget (or targets)][1] [1]: https://mcmap.net/q/243889/-add-cocoapods-to-tests-target-too-duplicateSpool
D
65

The proper way to do this is to update your podfile to add just 1 line :

link_with 'yourApp', 'yourAppExtension'

and a pod update should resolve the issue.

Diogenes answered 7/3, 2015 at 16:32 Comment(5)
What is the 'youAppExtension' part?Petulancy
the complete name of your extension (can be a Today extension or a Watch extension) :)Diogenes
As @Sti said, this no longer works and returns the error message "Invalid Podfile file: [!] The specification of link_with in the Podfile is now unsupported, please use target blocks instead.."Gsuit
The specification of link_with in the Podfile is now unsupported, please use target blocks instead.. This solution is deprecated.Knavery
Please use target 'My Widget' do end... method it is the only solution.Knavery
H
85

EDIT (2015/03/10)

See new accepted answer. I tried it in a new project and it worked, although my test pods both used sharedApplication which is disallowed in extensions. The fact that they showed those errors means it linked properly. Way to go @LeChatNoir!


Success.

The solution is as follows: Once you have your app extension and it's bridging header in a project using cocoapods and you want to use one of those cocoapods libraries, trying to include the pod like #import <GPUImage/GPUImage.h> will fail with file not found.

First, make sure that you tell the app extension (click on your project file, then on the extension target) to link against libPods.a, much like your app target.

Next, in your actual project (click on your project file, then on the project file again in the "Project" section of the inner sidebar) under the Info tab, set the configuration of the extension for Debug, Release, and Inhouse to use the "Pods" configuration from the dropdown. Pod Install will not do this for you, so you will need to do it yourself.

Lastly, make sure that both the Pods project's targets and your project's targets have all the architectures you need to build for in the Valid Architectures variable or you will get the ever-fun undefined symbols error.

This should work and allow you to import the pods of your choice like normal.


EDIT (2014/10/14): As an aside, since you may also be including Objective-C files from within your own project code in the Swift extension, you should make sure that any .m files you put in the bridging header are also compiled by the extension target. You can either do this from the compile sources menu for the target or in the .m file itself using the "Target Membership" section of the right sidebar in Xcode.

Hurling answered 28/8, 2014 at 18:20 Comment(12)
i have implement above all steps.. but having issue.. Undefined symbols for architecture armv7: "_OBJC_CLASS_$_Location", referenced from: objc-class-ref in TodayViewController.o please help me . how to solve itJacinthe
i have import "Location.h" file & use + class method in today's extension... then i will get above issue.Jacinthe
Did you make sure that the file is a member of the extension target as well? Also check that you are building for the correct architecture.Hurling
i have checked that architecture is correct. but file if not member of extension target. but can't i use files that are already in app?Jacinthe
They have to be members of both targets (extension and app). It is my understanding that extensions are compiled separately and just inserted into the main app's bundle after the fact. So the compilation for the extension needs to compile the source again.Hurling
If i am going to add app's file "Location.h" in extensions -> Build Phases --> Compile Sources.. but still its not working... now both "Location.h" file is member of both targets.. any suggestions on this?Jacinthe
i have checked above flow to add app's file to add in extension.. its working properly. by doing this we can access app's file in extension..Jacinthe
I've tried to make an example project to demonstrate the proper setup, and failed miserably. Apparently I only had the magic touch once. I've emailed the cocoa-dev list and I'll update my answer if I get any help.Hurling
ok, no probs. but i have done steps as you mention in your answer.. and also add app's file in extensions -> Build Phases --> Compile Sources. .. its perfectly working for me. thanksJacinthe
Oh yeah. You can actually do that while looking at each file as well. There is a section in the right sidebar in Xcode titled Target Membership that let's you check all the targets that build that particular file. This accomplishes the same thing on a file-by-file basis.Hurling
If anyone needs visual aid: developer.apple.com/library/ios/recipes/…Stillbirth
I also had to put $(inherited) for OTHER_LD_FLAGS in extension build settings.Morganstein
A
76

The above answers will work, but I only wanted a couple pods in my extension, so I did the following to my Podfile:

target '[Main App Target Name]' do
        pod ...
        pod ...
        pod ...
end

target '[Extension Target Name]' do
        pod ...
end

And then a pod install will do it!

Azaleeazan answered 13/8, 2015 at 13:8 Comment(0)
D
65

The proper way to do this is to update your podfile to add just 1 line :

link_with 'yourApp', 'yourAppExtension'

and a pod update should resolve the issue.

Diogenes answered 7/3, 2015 at 16:32 Comment(5)
What is the 'youAppExtension' part?Petulancy
the complete name of your extension (can be a Today extension or a Watch extension) :)Diogenes
As @Sti said, this no longer works and returns the error message "Invalid Podfile file: [!] The specification of link_with in the Podfile is now unsupported, please use target blocks instead.."Gsuit
The specification of link_with in the Podfile is now unsupported, please use target blocks instead.. This solution is deprecated.Knavery
Please use target 'My Widget' do end... method it is the only solution.Knavery
K
4

enter image description here

link_with, you will not use it anymore.

Invalid Podfile file: [!] The specification of link_with in the Podfile is now unsupported, please use target blocks instead..

Below is the perfect answe, tested also.

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, ‘9.0’
use_frameworks!
inhibit_all_warnings!

target 'DemoTodayWidget' do
    pod 'Reachability',                         '~> 3.2'
end

target 'My Widget' do
    pod 'Reachability',                         '~> 3.2'
end
Knavery answered 20/9, 2017 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.