5 minutes to quickly build an iOS App

5 minutes to quickly build an iOS App

[[404179]]

This article is reprinted from the WeChat public account "AirPython", the author is Xing Anguo. Please contact the AirPython public account to reprint this article.

1. Introduction

Hello everyone, I am Anguo!

After writing a crawler in Python, sometimes we need to schedule the crawler in real time on the mobile phone, or display the crawler results in real time.

In this scenario, we can write the crawler logic as an API and deploy it to the server, then write an App on the mobile terminal and directly call the interface through the interface element controls.

In this article, we will talk about how to quickly write an iOS native App.

2. Preparation

To implement native iOS applications, we need to use Xcode on Mac to write and compile

First, set up an Xcode developer account

Open Xcode, select Xcode - Preferences - Accounts in the upper left corner, click the + sign in the lower left corner, and add a developer account

Then, create a project using Xcode

Select iOS App as the template, enter the project name, select "Swift" as the programming language, and click Next to complete the project creation.

PS: Swift has a more concise syntax than OC

Finally, specify the Sign signature for the newly created project

If you have any questions about this part, you can click on the read original text at the end of the article to learn more.

3. Actual combat

In the actual combat part, we will explain it with a simple login page

3-1 Install dependent libraries

Since the project is developed using Swift, it is recommended to use SPM (Swift Package Manager) to install dependencies.

For example, the network request library "Alamofire"

Project address: https://github.com/Alamofire/Alamofire

Installation method: File - Swift Packages - Add Package Dependency - Enter the project address ( Github / Gitee ) - Select the installation version

3-2 Page Layout

Open the "ContentView.swift" file in the project root directory and write the specific view under body

First, use VStack to define a vertical layout box and define the child controls to be displayed horizontally in the center

PS: The three common layout methods of SwiftUI are VStack, HStack, and ZStack, which represent vertical layout, horizontal layout, and depth layout respectively.

  1. import SwiftUI
  2. import Combine
  3.  
  4. struct ContentView: View {
  5.      
  6. ...
  7.      
  8. //Build the page View  
  9. var body: some   View {
  10. VStack(alignment: HorizontalAlignment.center){
  11. ...
  12. }
  13. }
  14. }

Then, the child elements add a local picture, two input boxes, a selection box, and a button in turn.

in,

  • Image control
  • Text input box control TextField
  • Select Box ControlToggle
  • Button
  1. import SwiftUI
  2. import Combine
  3.  
  4. struct ContentView: View {
  5.      
  6. //Build the page View  
  7. var body: some   View {
  8. VStack(alignment: HorizontalAlignment.center){
  9. Image( "WechatIMG5" )
  10.              
  11. TextField( "Username" , text: $username).textFieldStyle(RoundedBorderTextFieldStyle())
  12. .keyboardType(.numberPad)
  13. .padding()
  14.              
  15. TextField( "Password" , text: $pwd).textFieldStyle(RoundedBorderTextFieldStyle())
  16. .keyboardType(.numberPad)
  17. .padding()
  18.              
  19. //Is it a test?
  20. Toggle(isOn: $isFavorited) {
  21. Text( "Test Environment" )
  22. }.padding(.leading, 0.0).frame(width: 140, height: 40)
  23.  
  24. Button( action : {
  25. //Specific operations
  1. }
  2. struct ContentView_Previews: PreviewProvider {
  3. static var previews: some   View {
  4. ContentView()
  5. }
  6. }
  7.      
  8. }

Finally, define variables and control data for two-way binding

  1. struct ContentView: View {
  2.      
  3. @State var username:String = "username"  
  4. @State var pwd:String = "password"  
  5. @State var result:String = "result"  
  6. @State var isFavorited:Bool = false  
  7.          
  8. }

3-3 Network request and result display

Set a click event for the Button control, use Alamofire to make a network request, and finally write the result display to the result control and bind it to the data.

  1. Button( action : {
  2. //Specific operations
  3. print( "start" )
  4.                  
  5. var url = ""  
  6.                  
  7. if(self.isFavorited){
  8. url = "...?username=" + self.username + "&password=" + self.pwd
  9. } else {
  10. url = "...?username=" + self.username + "&password=" + self.pwd
  11. }
  12.                  
  13. print( "Request address:" + url)
  14.                  
  15. AF.request(url).responseJSON { response in  
  16. switch response.result {
  17. case .success(let json):
  18. //Convert to Dictionary
  19. let post_paramsValue = json as ! Dictionary<String, Any >
  20.                          
  21. //__NSCFString
  22. let msg = post_paramsValue[ "msg" ]!
  23. //Convert to String
  24. let msg_pro = msg as ! String
  25.                          
  26. self.result = msg_pro
  27.                          
  28. break
  29. case .failure(let error):
  30. print( "error:\(error)" )
  31. self.result = "Network abnormality, please try again later!"  
  32. break
  33. }
  34. }
  35.                  
  36. }) {
  37. Text( "One-click execution" )
  38. .foregroundColor(Color.white)
  39. .padding(10)
  40. .background(Color.gray)
  41. .cornerRadius(6)
  42. .padding(10)
  43. .frame(alignment: .center)
  44. }
  45.              
  46. TextField( "result area" , text: $result)
  47. .padding()
  48. }

4. Final Thoughts

This article describes the detailed steps of developing an iOS native application through a simple example; in actual applications, different functional modules can be customized and developed based on specific scenarios.

<<:  Create your own US App Store ID

>>:  How to use iPad at work to improve work efficiency

Recommend

Few Tik Tok fans? A practical tutorial on how to increase followers on Douyin!

What if you want to gain 1 million followers in 7...

September marketing promotion hot calendar!

Autumn is the season of harvest. In the upcoming ...

No idea how to make H5? Here is a complete H5 case idea

Friends, do you still remember the NetEase H5 eve...

Providing offline support for mobile apps

Offline support for mobile applications can be un...

How can I improve video quality? How to make high-quality videos?

Improve video quality in four aspects: picture, t...

Python library that can complete Android UI automation

[[217058]] uiautomator2 Android Uiautomator2 Pyth...

The latest practical skills to make money through Tik Tok live streaming!

In fact, it is very simple to judge whether a per...

What are the key points to pay attention to when choosing server hosting?

The hosting server can be as small as possible Ev...