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

Not able to use the AFNetworking framework insdie App Extension #2321

Closed
logansiva opened this issue Sep 28, 2014 · 21 comments

Comments

Projects
None yet
@logansiva
Copy link

commented Sep 28, 2014

Hi,
I have created my own web service class that is using AFNetworking framework to communicate with server and get the response from there. Now i am planning to provide app extension. Since, i need to use my own web service call to communicate my server and get the response from there and from there my app extension will show the info to my user accordingly. Now, the bis issue was, i am unable to use my own web service call code due to that was using AFNetworking and the AFNetworking was accessing UIApplication class methods and its objects.

MY question is this: Is there a way to use AFNetworking framework in my app extension code?. If yes then how can i disable the accessing of UIApplication class from AFNetworking code?.

@lexar

This comment has been minimized.

Copy link

commented Sep 29, 2014

Hi, this is explained here: #2119
In short, you need to #define AF_APP_EXTENSIONS.
However, as I posted in the issue above, too, I am unsure where to put this #define, especially as new Xcode6 projects don't have a prefix file anymore.
Any help on an appropriate placement of #define AF_APP_EXTENSIONS would be appreciated!

@mattt

This comment has been minimized.

Copy link
Contributor

commented Sep 29, 2014

.@lexar is correct in pointing out the AF_APP_EXTENSIONS macro. Just define that any header included in the target, such as AppDelegate.h.

@mattt mattt closed this Sep 29, 2014

@livyathan

This comment has been minimized.

Copy link

commented Sep 30, 2014

I'm using AFNetworking on a Widget (Today's extension) and I'm having the same issue. I tried to include the macro on every file, but I always get a compiler error.

The problem is, Xcode 6 no longer adds a Prefix Header (pch) file, and widgets apparently don't need an AppDelegate, so by default, there is no reliable place to put the AF_APP_EXTENSIONS macro.

To solve this, I added a pch manually to my Widget's target following this tutorial (read Yedidya's answer):

http://stackoverflow.com/questions/24158648/why-isnt-projectname-prefix-pch-created-automatically-in-xcode-6#answer-24258165

And on the pch file, you can include the required macro, something like this:

#ifndef widgetPrefix_pch
#define widgetPrefix_pch

#define AF_APP_EXTENSIONS

#endif

@d2burke

This comment has been minimized.

Copy link

commented Nov 18, 2014

It's still not clear from this thread where the macro should be placed. Please help? I have both a share extension and a Today extension and I've downloaded the latest repo and placed #define AF_APP_EXTENSIONS in both header files for the two extensions and i'm still receiving the same error.

@aelam

This comment has been minimized.

Copy link

commented Mar 12, 2015

if I use AFNetworking via cocoapods , AFNetworking would be compiled as a static library before AF_APP_EXTENSIONS works? and I've tried to add an precompiled macro in config , it doesn't work.
what can I do ?

@staminajim

This comment has been minimized.

Copy link

commented Mar 12, 2015

+1, We are in the same boat now, but the issue is new to Cocoapods 0.36. If we downgrade to Cocoapods 0.35 it works.

I'm not sure if the solution needs to be made at the Cocoapods end or AFNetworking end :/

@mAu888

This comment has been minimized.

Copy link

commented Mar 12, 2015

+1 Downgrading from CocoaPods 0.36.0 to 0.35.0 solved the extension unavailable issue. Adding the macro did not help with cocoapods 0.36.0.

@aelam

This comment has been minimized.

Copy link

commented Mar 12, 2015

I solve it by pod_install, downgrade would throw another problem for me .
I can share the solution if anybody doesn't know

ÔÚ 2015Äê3ÔÂ12ÈÕ£¬17:23£¬James V notifications@github.com дµÀ£º

+1, We are in the same boat now, but the issue is new to Cocoapods 0.36. If we downgrade to Cocoapods 0.35 it works.

I'm not sure if the solution needs to be made at the Cocoapods end or AFNetworking end :/

¡ª
Reply to this email directly or view it on GitHub.

@staminajim

This comment has been minimized.

Copy link

commented Mar 12, 2015

@aelam Yes please.

@mAu888

This comment has been minimized.

Copy link

commented Mar 12, 2015

Seems to be related to CocoaPods 0.36.0 settings "Enable Strict Checking of objc_msgSend Calls" to YES on the Pods. When setting to NO for the AFNetworking Pod it works.

@aelam

This comment has been minimized.

Copy link

commented Mar 13, 2015

@staminajim

post_install do |installer_representation|
    installer_representation.project.targets.each do |target|
        if target.name == "Pods-EMToday-AFNetworking"   # EMToday should be the Extension target name
            target.build_configurations.each do |config|
                    config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'AF_APP_EXTENSIONS=1']
            end
        end
end

end

@aelam

This comment has been minimized.

Copy link

commented Mar 25, 2015

I've found this , please see the code .
would it works?
especially when use AFNetworking via cocoa pods

/* for use to document app extension usage */
#if defined(__has_feature) && __has_feature(attribute_availability_app_extension)
    #define __OSX_EXTENSION_UNAVAILABLE(_msg)      __OS_AVAILABILITY_MSG(macosx_app_extension,unavailable,_msg)
    #define __IOS_EXTENSION_UNAVAILABLE(_msg)      __OS_AVAILABILITY_MSG(ios_app_extension,unavailable,_msg)
#else
    #define __OSX_EXTENSION_UNAVAILABLE(_msg)

    #define __IOS_EXTENSION_UNAVAILABLE(_msg)
#endif
@staminajim

This comment has been minimized.

Copy link

commented Mar 25, 2015

@aelam I think we have to wait for the next cocoa pods release to go live. Looks like there's some calls that needed to be wrapped in AF_APP_EXTENSIONS that were missed in the currently released version?

Once that's available I believe your previous podfile code should work.

@suyashb734

This comment has been minimized.

Copy link

commented Apr 14, 2015

It works if you go to the AF Networking PODS Target and add the macro AF_APP_EXTENSIONS in Preprocessor Macros in the Build Settings of the target

@komocode

This comment has been minimized.

Copy link

commented Apr 17, 2015

I've tried all of the solutions in this thread, not working for me. It seems to complain at UIAlertView+AFNetworking.m at line 104

@wuf810

This comment has been minimized.

Copy link

commented Apr 18, 2015

Yep not working for me either. What a pain

@staminajim

This comment has been minimized.

Copy link

commented Apr 20, 2015

We're using a variation of @aelam 's solution which works: (slightly edited because we also set a few more build settings in here, and also set for the Apple Watch extension)

post_install do |add_app_extension_macro|
    add_app_extension_macro.project.targets.each do |target|
        if target.name.include?("Pods-YOUR_APP_EXTENSION_TARGET")
            target.build_configurations.each do |config|
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'AF_APP_EXTENSIONS=1']
            end
        end
    end
end

Put that at the end of the podfile. If it's still not working, I suggest putting some puts statements in there to make sure the if condition is matching correctly for your target name.

@peng90

This comment has been minimized.

Copy link

commented Jun 18, 2015

@suyashb734 's solution also works if you don't wanna put extra stuff in your Podfile.

@Arthraim

This comment has been minimized.

Copy link

commented Jul 30, 2015

For anyone confused as well. Cocoapods 0.38 changed a little bit, it creates only one single libAFNetworking.a for all your targets. For you who don't use use_frameworks!, here is the document.
The hook @aelam provided also has to be changed a little:

post_install do |installer_representation|
    installer_representation.pods_project.targets.each do |target|
        if target.name == "Pods-EMToday-AFNetworking"   # EMToday should be the Extension target name
            target.build_configurations.each do |config|
                    config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'AF_APP_EXTENSIONS=1']
            end
        end
end
@dmhts

This comment has been minimized.

Copy link

commented Dec 12, 2015

One more update for Cocoapods 0.39 if you don't utilize use_frameworks:

if target.name == "AFNetworking"   # You should check for the AFNetworking target instead of an extension
    target.build_configurations.each do |config|
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'AF_APP_EXTENSIONS=1']
    end
end
@yingyangios

This comment has been minimized.

Copy link

commented Nov 1, 2016

@suyashb734 I configure the project as you say . it work well Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.