Note: AFNetworking 2.0 Tutorial

Pre-compiled header

open the pre-compiled header Weather-Prefix.pch from the Supporting Files section of the project. Add this line after the other imports:

  #import "AFNetworking.h"

Adding AFNetworking to the pre-compiled header means that the framework will be automatically included in all the project’s source files.

  Build Settings > Apple LLVM 6.0 - Language > Prefix Header : Weather/Weather-Prefix.pch

JSON easier

Note: FYI, an alternative way to make working with JSON results a bit easier than looking up keys in dictionaries or creating special categories like this is to use a third party library like JSONModel.

No XML

Hooking into the Live Service

AFHTTPSessionManager has everything you need to talk to a web API. It will decouple your networking communications code from the rest of your code, and make your networking communications code reusable throughout your project.
Here are two guidelines on AFHTTPSessionManager best practices:

  • Create a subclass for each web service. For example, if you're writing a social network aggregator, you might want one subclass for Twitter, one for Facebook, another for Instragram and so on.
  • In each AFHTTPSessionManager subclass, create a class method that returns a shared singleton instance. This saves resources and eliminates the need to allocate and spin up new objects.

Where To Go From Here?

Think of all the ways you can now use AFNetworking to communicate with the outside world:

  • AFHTTPOperation with AFJSONResponseSerializer, AFPropertyListResponseSerializer, or AFXMLParserResponseSerializer response serializers for parsing structured data
  • UIImageView+AFNetworking for quickly filling in image views
  • Custom AFHTTPSessionManager subclasses to access live web services
  • AFNetworkActivityIndicatorManager to keep the user informed
  • AFHTTPOperation with a AFImageResponseSerializer response serializer for loading images

Links

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容