0

I'm working on an iOS app where SoundCloud users log in with OAuth in a web view and then the app makes HTTP requests to the SoundCloud API via Alamofire. I've successfully authenticated the user and stored their token (using ABMSoundCloudAPI), but GET requests to https://api.soundcloud.com/me are failing with a -1005 error, "The network connection was lost." This seems to be a common problem with iOS as discussed here, however resetting the simulator doesn't solve the problem for me and the problem also occurs when using a device. I've also tried:

  • Removing and re-adding the wifi network
  • Retrying the request programmatically if it fails
  • Adding a header with "Connection": "Close"

I see the same error in every case. Are there other headers I should try? I'm using these libraries via Cocoapods:

  • ABMSoundCloudAPI (0.2.1)
  • AFNetworking (2.6.1)
  • AFOAuth2Manager (2.2.0)
  • Alamofire (3.1.2)
  • SwiftyJSON (2.3.1)

Here is my code:

var retryCount = 0

func getUserInfo(token:String) {
    let headers = ["Connection": "Close"]
    Alamofire.request(.GET, "https://api.soundcloud.com/me?oauth_token=\(token)", parameters: ["client_id":clientId], encoding: .JSON, headers: headers)
        .responseJSON { response in

            guard response.result.error == nil else {
                print("error calling GET on /me")
                print(response.result.error)
                if self.retryCount < 2 {
                    if let token = self.defaults.stringForKey("sc_key_token") {
                        self.getUserInfo(token)
                        ++self.retryCount
                    }
                }
                return
            }

            guard let value = response.result.value else {
                print("Error: did not receive data")
                return
            }


            let user = JSON(value)

            print("User info: " + user.description)

    }
}

Error message:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x126248c10 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://api.soundcloud.com/me?oauth_token=USER_TOKEN, NSErrorFailingURLKey=https://api.soundcloud.com/me?oauth_token=USER_TOKEN, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}
Community
  • 1
  • 1
blwinters
  • 1,911
  • 19
  • 40

1 Answers1

0

It seems that this was caused by the request encoding. When I switched from .JSON to .URL, the 1005 error went away.

blwinters
  • 1,911
  • 19
  • 40