Use AFNetworking with Swift

After you have use CocoaPods to integrate AFNetworking in your project like described here open Xcode and:

  1. Create a new header file in your project
  2. Reference it in Build Settings->Objective-C Bridging Header as yourappname/yourbridgefile.h
  3. Import AFNetworking inside the bridging header
    #import <AFNetworking/AFNetworking.h>

Now you can use AFNetworking from Swift:

let manager = AFHTTPRequestOperationManager()  
manager.GET(  
  "http://headers.jsontest.com",
  parameters: nil,
  success: { (operation: AFHTTPRequestOperation!,
              responseObject: AnyObject!) in
      println("JSON: " + responseObject.description)
  },
  failure: { (operation: AFHTTPRequestOperation!,
              error: NSError!) in
      println("Error: " + error.localizedDescription)
  }
)
View Comments
Navigation