I am making an application in swift 3 xcode 8 and they tell me that in order for me to access the web service I need to send two objects, they send me the following example in ajax, how can I do it with xcode 8?
$("#cargar").click(function() {
/*datosRegistro = {
'user' : $("#user").val(),
'pass' : $("#pass").val()
};*/
Datos = {
"usr_username" : "usuario",
"usr_password" : "contrasenia"
};
datosRegistro = {
"Accion" : 2 , //2 es obligatorio para login
"Datos" : Datos
};
$.ajax({
url: 'http://mihost/server/sitio.php',
data: {'datosRegistro': JSON.stringify(datosRegistro) },
type: 'POST',
dataType: 'json',
async: false,
success: function(p, estado, xhr) {
// Para atrapar otros posibles errores
try {
// Asigna el objeto de retorno
objRetorno = JSON.parse(p);
// Valida el error controlado
if ( objRetorno.Error == true ) {
// Informa el error al usuario
// Muestra el mensaje (cambia icono y pone mensaje)
alert(objRetorno.Mensaje);
// Termina el procedimiento
return false;
}
else
{
//aqui escribes el codigo en caso de ser correctos los datos
return true;
}
}
catch(error){
// Notifica el error al usuario
alert( "Excepcion encontrada al recuperar cadena JSON.\n\nDetalles : " + error.message ) ;
}
},
error: function(xhr, estado, errata) {
// Informa el error interno al usuario
alert('La accion no pudo ser procesada correctamente...');
// Termina el procedimiento
return false;
},
dataType: 'html'
}); // Termina la llamada AJAX
})
EDIT:
I modify the question by expanding some details.
(a). THE WEB SERVICE
Swift sends a request to the web service through a URL and it returns the JSON. When you receive it, you treat (parse) it in Swift, as explained in (b).
For the web service to return for example this:
You must know how to build your URL, that will be told to you by whoever manages the web service. In your case, an (imaginary) example of a URL would be:
As you can see, you send two parameters to the web service separated by &, it receives it and uses it to query the data you want and returns a correct JSON and if not an erroneous one.
You can even test if the web service returns what you expect, by putting the URL in any browser. If you write in your browser something like this:
https://reqres.in/api/users/1
you will get this result that you can see on the screen:Notice the elegance of the URL. Through it, what is requested from the web service is that it gives you the data of the user whose id=1.
In your case, an elegant or friendly URL would be:
http://sipot.conanp.gob.mx/server/pasaportes/usuario/878
in which you ask for the passport data of the user whose id=878. But as I said before, this depends on the web service programmer. The important thing is that it works well, send the data you request and it's up to you to process it.(b). Handle data received from web service (or api) in Swift
Received your data, in this case a JSON, you handle it in Swift. In this example the JSON is stored inside the variable
parsedData
.Example:
This may be very helpful: Apple Swift Help
When I make the request, the web service responds:
{"Error":true,"Message":"you are trying to hack us, or you did not send the information correctly","Account":0,"Data":null}
and the person in charge of the web service tells me that he answers that because I am not sending him data and I am not sending it correctly. Therefore, I don't know how to do it in swift, it tells me that I have to send objects like the ones I mentioned above. But I don't know if that can be done with swift. Thank you very much for your help.
The safest thing is that you have a URL to which to make the request, for example
http://webservice.com/file.php and here you have to build the chain with the parameters that they ask you for but the administrators have to tell you how to pass them.
If, for example, you are passing an Id but they are trying to validate it as iduser in their webservice, it will fail.
You will have to build the URL like for example: http://webservice.com/file.php?id=1234&key=2345 (I would advise you to pass the variables in lowercase, instead of Id as you have to pass id). In any case, you will have to ask them, as the colleague commented, how you have to provide them with the data: if they look for a variable by name "idusuario" and you pass it "id" it will always fail, but it is something that they have to clarify for you ;)