title: iOS not getting accurate App version from iTunes
body: <p>Actually Just want to implement functionality like if new version of the app is available on AppStore then end user will be notified for update the App.</p>
  
  <p>There are so many answers on google but look at below my code.</p>
  
  <pre><code>func isUpdateAvailable(completion: @escaping (Bool?, Error?) -&gt; Void) throws -&gt; URLSessionDataTask {
          guard let info = Bundle.main.infoDictionary,
              let currentVersion = info["CFBundleShortVersionString"] as? String,
              let identifier = info["CFBundleIdentifier"] as? String,
              let url = URL(string: "http://itunes.apple.com/in/lookup?bundleId=\(identifier)") else {
                  throw VersionError.invalidBundleInfo
          }
          print(currentVersion)
          let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
              do {
                  if let error = error { throw error }
                  guard let data = data else { throw VersionError.invalidResponse }
                  let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any]
                   if let a = ((json?["results"] as? [Any])?.first) as? [String: Any] {
                  print(a["version"] as? String ?? "")
              }
                  guard let result = (json?["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String else {
                      throw VersionError.invalidResponse
                  }
                  completion(version != currentVersion, nil)
              } catch {
                  completion(nil, error)
              }
          }
          task.resume()
          return task
      }
  </code></pre>
  
  <p>and calling this function </p>
  
  <pre><code> _ = try? appDelegate.isUpdateAvailable { (update, error) in
              if let error = error {
                  print(error)
              } else if let update = update {
                  if update {
                      let alert = UIAlertController(title: "New Version Available", message: "New version of application is available please update now!", preferredStyle: UIAlertControllerStyle.alert)
                      alert.addAction(UIAlertAction(title: "Update", style: .default, handler: { (actionOk) in
  
                          if let url = URL(string: "itms-apps://itunes.apple.com/app/idXXXXXXXXX"),
                              UIApplication.shared.canOpenURL(url){
                              UIApplication.shared.open(url, options: [:])
                          }
                      }))
                      self.present(alert, animated: true, completion: nil)
                  }
              }
          }
  </code></pre>
  
  <p>Problem is that If new version of app is ready to sell and I open old version of app then popup will open and when I click on update button, It will redirect on AppStore. <strong>But on AppStore rather then display "Update" button, it display "Open" button</strong> </p>
  
  <p>So I checked in my code.</p>
  
  <p>When I run below URL manually on browser </p>
  
  <pre><code>http://itunes.apple.com/in/lookup?bundleId=com.mycompany.appname
  </code></pre>
  
  <p>Then download <strong>.txt</strong> file and got accurate App version But In my code <code>print(a["version"] as? String ?? "")</code> it returns wrong version number.</p>
  
  <p>Where I'm going wrong? Please correct me.</p>
  
  <blockquote>
    <p>NOTE: I also used <a href="http://itunes.apple.com/lookup?bundleId=com.mycompany.appname" rel="nofollow noreferrer">http://itunes.apple.com/lookup?bundleId=com.mycompany.appname</a> means removed country code from URL but not worked for me.</p>
  </blockquote>