visit
To launch the local webserver, we can use Debian or macOS. In this tutorial, I will use Debian on Windows through WSL, though the vast majority of the listed commands are also applicable to the macOS ecosystem.
If you have already installed Debian OS, we also need to install a python interpreter (This procedure is not required for macOS users because macOS already has a python interpreter). We can do this via this command:$sudo apt install python3
$sudo apt install tmux
$brew install tmux (for the macOS users)
Once these operations are finished, you can launch the webserver through the command listed below. For the Debian users, we should start a new
tmux
session with this command:$tmux new-session -s testname
And now, we can create a separate terminal to debug our server. We can do this using CTRL+B hotkey and right after that - press % (I know that's a weird hotkey 🤯).
This will create a new terminal on the right side. In macOS, you can create a new terminal window via CMD+N hotkey.
You can also navigate through
tmux
sessions using CTRL+left key/right key hotkey.Then create a folder named “server” and change the directory to this newly created folder:$mkdir server
$cd server
Right after that, we need to create a json file that will be sent to our mobile app whenever requested. Let’s create this json file with
nano/vim
:$nano teachers.json
{ "success": true,
"body": [
{
"id": "1",
"first_name": "Julia",
"last_name": "Green"
},
{
"id": "2",
"first_name": "Peter",
"last_name": "Sheppard"
}
]}
$python3 -m http.server
$python3 -m http.server 8052
The last thing we need to do on our “backend” side is to verify the reachability of our service. First of all, we need to get our server’s local IP address. You can find it in the
ip address
command for Debian or ifconfig
for macOS. My server’s local IP address is 192.168.1.53, so now I can switch to another device, open a browser and enter the following URL://192.168.1.53:8000/teachers.json
Or you can test it through
telnet
on the right side of the tmux
session. Telnet also can be installed with this command:$apt install telnet
$brew install telnet (for macOS)
As you can see here, I am sending a common HTTP
GET
request on the left session, and as a result, you can see the response on the right side!The last thing is to make sure you can get the response from our iOS app.
So, let’s create a new Xcode project, and in AppDelegate.swift file, enter the following code:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var components = URLComponents()
components.scheme = "http"
components.host = "192.168.1.53"
components.port = 8000
components.path = "/teachers.json"
// Which finally brings us //192.168.1.53:8000/teachers.json
if let url = components.url {
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print(error)
} else if let data = data,
let jsonResponse = try? JSONSerialization.jsonObject(with: data,
options: .allowFragments) {
print(jsonResponse)
}
}.resume()
}
return true
}
And that’s it! We finally got the response from our local server! 🎉🎉🎉
Now we can continue our development process with these mocks, and when the real server side is ready, we can just replace our endpoints!🙂