[转载]ios app登录/注册模块的实现:swift+xcode6+ios8+json

[转载]ios app登录/注册模块的实现:swift+xcode6+ios8+json

原文地址:http://dipinkrishna.com/blog/2014/07/login-signup-screen-tutorial-xcode-6-swift-ios-8-json/

最近我自己在学习swift,看了一些国内的视频教程(幕课网 跟 SwiftV课堂)。但是一直没有找到关于前后台通信的教程,其中最基础的应该就是app的登录注册模块了,除了ui的实现,最重要的是跟后台的交互。

这篇老外的教程就挺好的,而且还给了简单的后台代码。其基本架构就是:

1. swift实现前台ui,向后台发起post请求,并响应后台返回的json数据

2. php + mysql实现后台,处理请求并返回json数据

翻译什么的就算了,由于老外的视频都放youtube上,结果你懂的。所以我在优酷上复制了一份,原文如下:

This tutorial will guide you to create a simple app with a Signup and Login screen which takes username and password from the user and then posts it to an url and parse the JSON response.

We will have three view controllers for this project, Signup, Login and Home.

  1. Create a New project. And add the Screens and Segues/transitions.
  2. Home – Check for an existing session, else goto Login
  3. Login Screen – Post data to URL and parse the JSON response.
  4. Signup Screen – Post data to URL and parse the JSON response.
  5. Add Logout to Home Screen

900x570xxcode6_swfit_login_tutorial_storyboard-1024x648.jpg.pagespeed.ic.FWdVUwjckD

1. Create a New project. And add the Screens and the Segue/transition.


I had issues with Xcode 6.1, not allowing me to create segue between View Controllers. Please see this video for an alternative way to do that:

 

2. Create Classes Properties And Methods:

Add code to viewDidAppear of HomeVC.swift
to check for existing login, if no session is found then show the login screen.

 

signupTapped in SignupVC.swift:

 

signinTapped in LoginVC.swift:


 

logoutTapped in HomeVC.swift


 

Checkout the end result of the tutorial:

Download the full source code using the link below:

It contains the xcode project, php code and the sql file for the whole project.

Xcode Project : Swift Login/Signup App
Php Code : SwiftJsonLoginPHP.zip

Thanks, Hope it helps!

发表评论

电子邮件地址不会被公开。 必填项已用*标注