andy9775
andy9775

Reputation: 364

AFNetworking not working

I'm new to swift and iOS development and am having some issues with AFNetworking. I am using the following code taken from here :

class AFNetCmd {


final func go () {
    let manager: AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()


    manager.GET( "http://www.google.ca/",
        parameters: nil,
        success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
            println("JSON: " + responseObject.description)
        },
        failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
            println("Error: " + error.localizedDescription)
    })

    }
}

let test: AFNetCmd = AFNetCmd()
test.go()'

No code ever prints and while I understand that the code block will exit before any request returns as AFNetworking is asynchronous, I was wondering how I can get it to execute?

I've also run it using:

dispatch_sync(dispatch_get_main_queue(), { () -> Void in
            self.go()
        })

and I still get nothing.

EDIT:

As per a comment I tried:

class AFNetCmd {

final func go () {
    let manager: AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()


    manager.GET( "http://www.google.ca/",
        parameters: nil,
        success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
            println("JSON: " + responseObject.description)
        },
        failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
            println("Error: " + error.localizedDescription)
    }).start()

    }
}

let test: AFNetCmd = AFNetCmd()
test.go()'

and the problem still occurs. Ive also tried the .waitUntilFinished() method. Further, I tried sending requests using Alamofire and the same issues arise. Mind you I am creating a OSX cmd line app and its being run in xcode. Could there be some settings that are preventing network connections?

Upvotes: 1

Views: 450

Answers (1)

andy9775
andy9775

Reputation: 364

I hate to answer my own question but for some reason it wouldn't run as a swift command line application. I created an iOS app and ran it in the simulator and it worked fine. Any reasons why it won't run networking code as a command line app?

Upvotes: 1

Related Questions