-2

I've got the following code:

let reachUrl = "https://helpers.pythonanywhere.com/load-data/announcements?key=\(key)&data=[\"\(nameTF.text!)\", \"\(infoTF.text!)\", \"\(pickerView.selectedRow(inComponent: 0))\"]"
        
UIApplication.shared.open(URL(string: reachUrl.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!)!)

The error I get is

Failed to open URL https%3A%2F%2Fhelpers%2Epythonanywhere%2Ecom%2Fload%2Ddata%2Fannouncements%3Fkey%3D3409298423076318kk%2B8i%26data%3D%5Bff%2C%2 ... C%201%5D: Error Domain=NSOSStatusErrorDomain Code=-50 "invalid input parameters" UserInfo={NSDebugDescription=invalid input parameters, _LSLine=234, _LSFunction=-[_LSDOpenClient openURL:options:completionHandler:]}

The error is only shown in console, nothing is done within the app itself.

I'm wondering what's the matter. Thanks for any help

aadev151
  • 141
  • 1
  • 8
  • 1
    Print `URL(string: reachUrl.addingPercentEncoding(withAllowedCharacters: .alphanumerics).absoluteString`, I don't think that's what you wanted. I see `https%3A%2F%2F` instead of `https://` (etc.). Tthat's where you failed. – Larme Apr 09 '21 at 17:45
  • really, thanks a lot, it works now :) – aadev151 Apr 09 '21 at 17:46
  • Basically don’t use `URL(string:)`. That’s no way to form a URL. If you had used URLComponents, which exists for exactly this purpose, this would all have worked fine and the code would have been far cleaner. – matt Apr 10 '21 at 05:10

1 Answers1

0

I see what the problem is, can you please try the code below and let me know if it works.

let reachUrl = "https://helpers.pythonanywhere.com/load-data/announcements?key=\(key)&data=[\"\(nameTF.text!)\", \"\(infoTF.text!)\", \"\(pickerView.selectedRow(inComponent: 0))\"]"
        
UIApplication.shared.open(URL(string: reachUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!)

Thanks, Happy Coding.

Prateek Varshney
  • 1,114
  • 2
  • 12
  • 29