-2

I have Objective-C code to build a URL. How to write in Swift 3 code?

NSString *urlString = [[NSString alloc]initWithFormat:@"http://smartbaba.in/Familynk/api/registration.php?phone_no=%@&email=%@&password=%@&first_name=%@&last_name=%@",txtmobileno.text,txtemail.text,txtpassword.text,txtfirstname.text,txtlastname.text];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    `"http://smartbaba.in/Familynk/api/registration.php?phone_no=\(txtmobileno.text)&email=\(txtemail.text)&password=\(txtpassword.text)&first_name=\(txtfirstname.text)&last_name=\(txtlastname.text)"` or you could use use `String(format:_)`, but what floats your code – MadProgrammer Aug 01 '19 at 04:45
  • Don't build URLs with `stringWithFormat`. Use `URLComponents` (same in Objective-C too). – rmaddy Aug 01 '19 at 04:57
  • And you should be asking about Swift 5, not Swift 3. – rmaddy Aug 01 '19 at 04:58
  • my question is how post method in swift parameter inclued in url. – Kishan Manvar Aug 01 '19 at 05:00
  • Sounds like you need to [edit] your question with more relevant information. Be sure you post the Swift code you have tried and clearly explain what problems you are having and what help you need. Simply posting a very vague requirement isn't enough information for a Stack Overflow question. – rmaddy Aug 01 '19 at 05:03

4 Answers4

0

Try This

"http://smartbaba.in/Familynk/api/registration.php?phone_no=\(txtmobileno.text)&email=\(txtemail.text)&password=\(txtpassword.text)&first_name=\(txtfirstname.text)&last_name=\(txtlastname.text)"
Ashish
  • 706
  • 8
  • 22
0

Swift conversion can be something like this

// I thing code requires no explanation, its self explanatory
func makeUrl(phoneNo: String, email: String, password: String, firstName: String, lastName: String) {
// first way but not recomended
let urlString = "http://smartbaba.in/Familynk/api/registration.php?phone_no=\(phoneNo)&email=\(email)&password=\(password)&first_name=\(firstName)&last_name=\(lastName)"
print("\(urlString)")

// second way, ASAIK this is good way for constructing URL's
var components = URLComponents()
components.scheme = "http"
components.host = "smartbaba.in"
components.path = "/Familynk/api/registration.php"
components.queryItems = [
    URLQueryItem(name: "phone_no", value: phoneNo),
    URLQueryItem(name: "email", value: email),
    URLQueryItem(name: "password", value: password),
    URLQueryItem(name: "first_name", value: firstName),
    URLQueryItem(name: "last_name", value: lastName)
]
let url = components.url
print(url) // returns URL
print(url?.absoluteString) // returns url path in string

  }
// call function
 makeUrl(phoneNo: "12345", email: "test@gmail.com", password: "12345678", firstName: "test", lastName: "user")
Vikky
  • 914
  • 1
  • 6
  • 16
0

You can simply use String interpolation to create a String with multiple parameters like,

let urlString = "http://smartbaba.in/Familynk/api/registration.php?phone_no=\(txtmobileno.text)&email=\(txtemail.text)&password=\(txtpassword.text)&first_name=\(txtfirstname.text)&last_name=\(txtlastname.text)"

Now, to get the URL using urlString,

if let url = URL(string: urlString) {
    //use url here...
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
-1

post method using URLSession:

 let myUrl = URL(string: "http://smartbaba.in/Familynk/api/registration.php");

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"

        let postString = "firstName=James&lastName=Bond";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                if let parseJSON = json {
                    print(parseJSON)
                }
            } catch {
                print(error)
            }
        }
        task.resume()

If you are using Alamofire then,

 let parameters: Parameters = [
            "Subject": "hallo"
        ]

  let url = "http://mydomain/mydb/mydb_tesT.NSF/api/data/documents/unid/DD026770D91AA23DC1257EF90035E1C4"

  Alamofire.request(url, method:.post, parameters:parameters, headers:headers).responseJSON { response in
        switch response.result {
        case .success:
            debugPrint(response)

        case .failure(let error):
            print(error)
        }

    } 
kannan
  • 354
  • 1
  • 7