I tried to use AFNetworking with iOS8 keyboard extensions and got this error
'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead.
AFURLConnectionOperation.m#L220
AFURLConnectionOperation.m#L300
Any ideas ?
Fixed by 5b0638f94738b2996ec0994d70250bebd1c3cdd2:
App Extensions
When using AFNetworking in an App Extension,
#define AF_APP_EXTENSIONSto avoid using unavailable APIs.
Thanks for reporting this, @GuriK!
how can I define it? any example?
I am using AFNetworking in my app and also in my today extension
Hi, I would also like to know where to define this. Xcode 6 has no prefix file in a new project, should I define one? I placed #define AF_APP_EXTENSIONS in the headers and implementations of the extension classes and where I actually use AFNetworking, a shared instance which is not even used by the extension at the moment, but I can't get that compiler error away.
So what would be the appropriate place to put the define?
EDIT: Putting the define right before setShouldExecuteAsBackgroundTaskWithExpirationHandler: lets me compile of course :)
But I would like to know the appropriate way to use the directive...
@lexar , there is a place you can put it: in Project Navigator, select your project -> select your extension target on the left -> Build Settings -> search for "Preprocessor Macros" and add AF_APP_EXTENSIONS to the list.
I still see additional errors in AFNetworkActivityIndicatorManager.m
'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead.
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self isNetworkActivityIndicatorVisible]];
and UIAlertView+AFNetworking
'initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:' is unavailable: not available on iOS (App Extension) - Use UIAlertController instead.
This is how I fixed (was wary of forking & making PR, there are already way too many forks of this project!):
diff --git a/Pods/AFNetworking/UIKit+AFNetworking/AFNetworkActivityIndicatorManager.m b/Pods/AFNetworking/UIKit+AFNetworking/AFNetworkActivityIndicatorManager.m
index c2d855a..4571883 100644
--- a/Pods/AFNetworking/UIKit+AFNetworking/AFNetworkActivityIndicatorManager.m
+++ b/Pods/AFNetworking/UIKit+AFNetworking/AFNetworkActivityIndicatorManager.m
@@ -114,7 +114,9 @@ static NSURLRequest * AFNetworkRequestFromNotification(NSNotification *notificat
}
- (void)updateNetworkActivityIndicatorVisibility {
+#if !defined(AF_APP_EXTENSIONS)
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self isNetworkActivityIndicatorVisible]];
+#endif
}
- (void)setActivityCount:(NSInteger)activityCount {
diff --git a/Pods/AFNetworking/UIKit+AFNetworking/UIAlertView+AFNetworking.h b/Pods/AFNetworking/UIKit+AFNetworking/UIAlertView+AFNetworking.h
index b94f1cb..0ca52ac 100644
--- a/Pods/AFNetworking/UIKit+AFNetworking/UIAlertView+AFNetworking.h
+++ b/Pods/AFNetworking/UIKit+AFNetworking/UIAlertView+AFNetworking.h
@@ -24,7 +24,7 @@
#import <Availability.h>
-#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
+#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && !defined(AF_APP_EXTENSIONS)
#import <UIKit/UIKit.h>
diff --git a/Pods/AFNetworking/UIKit+AFNetworking/UIAlertView+AFNetworking.m b/Pods/AFNetworking/UIKit+AFNetworking/UIAlertView+AFNetworking.m
index b7e2a26..5b9f15d 100644
--- a/Pods/AFNetworking/UIKit+AFNetworking/UIAlertView+AFNetworking.m
+++ b/Pods/AFNetworking/UIKit+AFNetworking/UIAlertView+AFNetworking.m
@@ -22,7 +22,7 @@
#import "UIAlertView+AFNetworking.h"
-#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
+#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && !defined(AF_APP_EXTENSIONS)
#import "AFURLConnectionOperation.h"
@jpmhouston I'm running into the same issue. I think that submitting your PR would be great!
@bejar37 sorry, it turns out our share extension doesn't need AFNetworking after all so I'm busy on other tasks now and I won't be creating this PR. Feel free to use the patch above and do that yourself though.
this can really fix issues
#if !(defined(__has_feature) && __has_feature(attribute_availability_app_extension))
//Not in the extension
#else
//In extension
#end
Hey there!
To resolve this problem in another way, you can use my category and simple condition:
UIApplication *application = [UIApplication rsk_sharedApplication];
if (application) {
// call any method of the app instance
}
I think it can have a much simplified version
发自我的 iPhone
在 2015年3月29日,03:41,Ruslan Skorb [email protected] 写道:
Hey there!
To resolve this problem in another way, you can use my category and simple condition:
UIApplication *application = [UIApplication rsk_sharedApplication];
if (application) {
// call any method of the app instance
}
—
Reply to this email directly or view it on GitHub.
@aelam I'll be glad to see a simplified version.
In case someone's wondering (I was) where to put the #define AF_APP_EXTENSIONS so that it only applies for the extension target if also your container app uses the library, these steps worked for me: http://stackoverflow.com/a/29335471/3949162
What is the right way to define AF_APP_EXTENSIONS, when using cocoapods?
To avoid overwriting after pod install.
为什么定义了AF_APP_EXTENSIONS还是提示同样地错误信息。
我使用了cocoapods来管理AFNetworking,macro里面加了AF_APP_EXTENSIONS=1,判断条件加了!defined(AF_APP_EXTENSIONS),还是报错,
Any ideas?
@ZHANGMRXIN
Add this to your podfile. remember to replace the target name
post_install do |installer_representation|
installer_representation.project.targets.each do |target|
if target.name == "Pods-YOU_EXTENSION_TARGET-AFNetworking"
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'AF_APP_EXTENSIONS=1']
end
end
end
end
@aelam not work for me.
This works: config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO'
Wrong target name? You tell me ^
发自我的 iPhone
在 2015年6月1日,上午2:54,Ivan Vavilov [email protected] 写道:
@aelam not work for me
—
Reply to this email directly or view it on GitHub.
@aelam The __has_feature(attribute_availability_app_extension) way doesn't work. The macro indicates the compiler has the support for compiling application extension, not compiling for application extension.
any idea how this works with cocoapods 0.38.2? Cause for me it isn't working :(
I also can't build with any combination of workarounds and hacks, using cocoapods 0.38.2 and the latest AFNetworking (2.6.0, upgrading from 2.5.4). I've tried the post_install hooks mentioned above and on Stack Overflow, as well as editing individual prefix headers for the today widget target and AFNetworking.
@jeffremer @georgbachmann Some things got updated so check this answer if you still don't have it working http://stackoverflow.com/a/31992480/896334
@thisiscrazy4 I tried everything here and only this one ^ worked :) :+1:
thanks