Reputation: 31
Can I create a web API with ASP.NET core to interact with my SQL Server database, and then have my iOS mobile application coded in swift interact with the .net file?
I need to connect my iOS app with an already existing remote database and I'd like to know if this is a viable option. Since I can't do it directly, this seems like the only (well documented) option I have that is free.
Upvotes: 1
Views: 1357
Reputation: 1613
UPDATE: Check out my blog post for more informations on this sepcific usage: https://medium.com/@brstkr3/how-to-connect-your-swift-application-to-an-asp-net-core-back-end-cc0ab9a4fba8
You could use any language/framework for this kind of backend, it does not really matter for which use case it gets deployed, as example for mobile, desktop or web. (NodeJS / ASP.NET Core / ...)
The way to consume REST API's are important at that point.
So you could use ASP.NET Core Web API for that and build the backend with it. Including services like authentication system, data transactions with databases , external API's (Azure, etc.) and so on.
After all to include the specific Http in the Swift application is important. So all Http Request inside the Swift application have to go through the built ASP.NET Core Web API.
It could look like this inside the Swift ViewController
:
func LoginButtonTapped( sender: UIButton) {
//Server URL of the ASP.NET Core Web API "http://hostname:serverport"
var url = "http://localhost:5000/api/login"
//Create a component and add the URL
guard var urlComponents = URLComponents(string: url) else { return }
//Modify the URLSession with the url of the created URLComponent above
URLSession.shared.dataTask(with: urlComponents.url) {
(data, response, error) in ...
}
}
For further usage consider:
Don't forget to decode the returned data from the Server, for example with the
JSONDecoder
.
Also watch out for authentication, considering how you want to achieve this and how its gonna be handled in native with storing credentials/tokens
Also don't forget to get your server running while debugging the mobile application ;)
Upvotes: 2