Search code examples
iosswiftalamofire

Alamofire gives an error on this


I'm trying to get my appversion from appstore using below

func checkfornewVersionofApp () {

    let appId = NSBundle.mainBundle().infoDictionary!["CFBundleIdentifier"]
    let appIdString = appId as! String

    let theitunesUrl  = "http://itunes.apple.com/en/lookup?bundleId=" + appIdString           

    Alamofire.request(.GET, theitunesUrl, parameters: nil).responseJSON { response in    
        switch response.result {
            case .Success(let JSON):
                print("success with json : \(JSON)") 
                break
            case .Failure(let error):
                print("some error occured : \(error.localizedDescription)")
                break
        }
    }
}

it always goes to error and gives the following

The operation couldn’t be completed. JSON could not be serialized. Input data was nil or zero length

what is the reason.hope yur help.


Solution

  • There is the problem with your URL. Check this link Link for iTunes API

    Change your code

    let theitunesUrl = "http://itunes.apple.com/en/lookup?bundleId=" + appIdString

    to

    let theitunesUrl = "http://itunes.apple.com/lookup?bundleId=" + appIdString

    In addition, you can add the country parameter to the query, to get results for a specific country App Store.

    For example:

    http://itunes.apple.com/lookup?bundleId=com.yelp.yelpiphone&country=de

    The description, user rating and other fields might change between different App Store countries.