objective-c - AFNetworking 包括 header

标签 objective-c afnetworking-2

我正在尝试从 ASIHttpRequest 转换为 AFNetworking,但我似乎在我的类(class)中的以下行中遇到“使用未声明的标识符 AFURLSessionManager”错误。

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

我在标题中包含了以下内容:

#import "AFNetworking.h"
#import "AFURLSessionManager.h"

这一定是很明显的事情,但现在脑子不好放屁。

最佳答案

AFURLSessionManager.h 中的这行代码简单地解释了该行为。

#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090)

AFURLSessionManager 使用 NSURLSession,它在 iOS 7(和 OSX 10.9)上可用。

如果您的目标是 iOS 6,AFURLSessionManager 将无法使用,它会在编译时被删除。这就是您收到错误的原因。

将 iOS 7 设置为最低部署目标将修复编译错误,即使它可能不符合您的需求。


也就是说,我的建议是使用 CocoaPods 来管理第三方依赖项,例如 AFNetworking

AFNetworking 2.0 是一个模块化框架,这意味着您可以根据需要选择使用哪些模块。核心支持iOS 6,但部分模块只支持iOS 7,比如NSURLSession模块就是AFURLSessionManager所属的模块。

模块化是通过使用 CocoaPods subspecs 实现的.为了导入 AFNetworkingNSURLSession 模块,您只需要执行类似

的操作
platform :ios, '7.0'

pod 'AFNetworking',              '~> 2.0'
pod 'AFNetworking/NSURLSession', '~> 2.0'

您仍然可以针对 iOS 6,但您不会获得 CocoaPods 文档中指定的不兼容子模块。

A subspec can restrict the platform of the parent specification. In this case it will be inherited by the parent specification only if the target of the podfile supports it.

关于objective-c - AFNetworking 包括 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100759/

相关文章:

ios - 如何在 UITableView 静态单元格中动态更改单元格高度

ios - 导轨 : Error occurred while parsing request parameters.(HTTP PUT)

ios - Swift:GM Xcode 6.1 和 AFNetworking AFJSONResponseSerializer

ios - 使用 AFNetworking 进行图像缓存

objective-c - NSTextView圆角并绘制背景

objective-c - 适用于 NSArray 中包含的字符串数量

objective-c - 可以 stringEncodingForData :encodingOptions:convertedString:usedLossyConversion: return NSUTF16StringEncoding or NSUTF32StringEncoding?

ios - AFNetworking 2.0 - "unacceptable content-type: text/plain"

ios - AFNetworking : The Operation couldn't completed.(NSURLErrorDomain error -1004)

objective-c - 真正的大数字和 Objective-C