Develop iOS/iPhone/iPad applications using HTML (Web)

Many people want to develop their own iOS App, right? There are several ways to develop an iOS App. One is the "formal" method, which is Xcode+ObjC. But this requires both learning the ObjC language and setting up an Xcode development environment. After everything is done, it is still very difficult to run the app on your iPhone/iPad!

However, the Safari browser provides a function to add a webpage to the desktop and run it as an independent App. With this feature, great! We can develop iOS App directly with Web language (HTML+CSS+JS), which is very simple.

1. Environment setup

1. Install Linux environment

In order to write apps for the web, we first need to run a web service. There are two methods for this step.

The first one: Find a server/computer and run the web service on it. This method is possible, but it requires a server with a public IP (otherwise you cannot access your App from the outside). However, this method can "install" your App for others. This article does not use this method.

The second: run the web service on the iPad. This method does not require a public IP, but you can only use your own App. This article uses this method.

iOS does not allow apps to access the underlying layer, so web services cannot be run directly on iOS, even though iOS is based on Linux. But we can install the Linux environment first, and then install the Web service in the Linux environment.

How to install the Linux environment?

In fact, you only need to install an app called iSH in the App Store.

Once installed, open the application and you will get a Linux Shell.

2. Install some tools

iSH supports apk package manager. We can install the package with the following command:

apk add 软件包名

Let's install the software we will use today:

apk add vim python3

Note that when installing the software package, keep the App open, otherwise the installation may fail.

3. Start the web service

You may ask: Why install python3? Not using web development?

In fact, today we are going to use the Simple HTTP service that comes with python3.

First create a new directory as the root directory of the web service:

mkdir www
cd www

Then start the HTTP service with Python3:

python3 -m http.server 80

python3 will use the current directory as the root directory of the website. 80 is the port number.

Why not use Nginx/Apache?

iSH is implemented based on Alpine Linux. Alpine Linux is a smaller Linux system and therefore less fully functional. If you run Nginx on iSH, you will not be able to access it. I haven't tried Apache. But to develop some small applications, you don't need such a powerful web server.

4. Stable operation

Python3 is not very stable. Sometimes, you will find that the web server exits with an error.

In order to make it stable, there is a very simple way: add a while true loop directly, so that it will automatically restart after it automatically exits.

while true; do python3 -m http.server 80 > /dev/null 2> /dev/null; done &

Since http.server will automatically output the received HTTP request, in order to prevent it from outputting, add a > /dev/null 2> /dev/null after the command. To make it run in the background without affecting the terminal, add an & after the command.

5.Start automatically after power on

If you close the iSH App (enter the exit command in the terminal, or press the home button twice and close iSH, or restart your iPhone/iPad) and reopen it, the web server will not run. In order to make it run for a long time, it needs to be self-starting (that is, it will run automatically when iSH is turned on).

How to do it?

In fact, you only need to edit the /etc/profile file:

vim /etc/profile

Add these three lines of code at the end:

cd /root/www
while true; do python3 -m http.server 80 > /dev/null 2> /dev/null; done &
cd ~

The commands in /etc/profile will run automatically at boot.

After the modification is completed, enter exit, and iSH will exit automatically. Then reopen the App, and the web server will run automatically.

6. Running in the background

If your iPhone/iPad opens other apps, after 30 seconds, it will not be able to connect to the web server. This is because iOS puts iSH in the background when you open other apps. iOS kills the background very badly. When iSH runs in the background for 30 seconds, it will be suspended by iOS.

what to do?

Or modify /etc/profile and add a line of code at the end:

cat /dev/location > /dev/null &

Then enter exit and reopen iSH.

iSH will ask you for permission to obtain the location. Be sure to select "Always Allow"! (If you don’t have this option, please set it to “Always Allow” in Settings->Privacy->Location)

This will allow it to run in the background.

What does cat /dev/location mean?

/dev/location is a device file that comes with iSH, indicating the current geographical location. If you run cat /dev/location, the latitude and longitude will be output.

Why can this be done in the background?

Because if there is an app that is constantly getting location, iOS will allow it to run in the background.

Guess you like

Origin blog.csdn.net/nnKevi/article/details/128491667