关于AFNetworking的介绍就不多说了,这是一个时下比较流行的ios和mac os网络库,不过遗憾的现在只有objective-c版本。 1. 安装CocoaPods $ sudo gem install cocoapods 安装过程比较慢,你可以用$ sudo gem install cocoapods -V来观察后台的执行过程。 另外,由于GFW的原因,RubyGems资料源可能被墙,可以将gem的源换成淘宝的RubyGems镜像。 2. 编辑Podfile 在项目目录下添加podfile文件,编辑内容: source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' pod 'AFNetworking', '~> 2.5.0' 在终端中执行: $ pod install 然后会在项目目录下生成<YourProjectName>.xcworkspace文件,之后就使用该xcworkspace文件来打开项目。 3. 在项目中引用AFNetworking 双击上面生成的xcworkspace文件,打开xcode 3.1 在项目目录中添加一个iOS Header File,可以命名为<YourProjectName>BridgingHeader.h #ifndef AFNetworkingTest_BridgingHeader_h #define AFNetworkingTest_BridgingHeader_h #import <AFNetworking/AFNetworking.h> #endif 3.2 在项目的Build Settings中,搜索Objective-C Bridging Header设置,定义为YourProjectName/YourBridgingHeader.h,如下图所示 3.3 现在,可以在你的swift代码中添加一句 let manager = AFHTTPRequestOperationManager() 编译通过! 4. 使用方法
[转载]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. Create a New project. And add the Screens and Segues/transitions. Home – Check for an existing session, else goto Login Login Screen – Post data to URL and parse the JSON response. Signup Screen – Post data to URL and parse the JSON response. Add Logout to Home Screen 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. override func viewDidAppear(animated: Bool) { super.viewDidAppear(true) let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults() let isLoggedIn:Int = prefs.integerForKey("ISLOGGEDIN") as Int if (isLoggedIn != 1) { self.performSegueWithIdentifier("goto_login", sender: self) } else { self.usernameLabel.text = prefs.valueForKey("USERNAME") as NSString } } signupTapped in SignupVC.swift: @IBAction func signupTapped(sender : UIButton) { var username:NSString = txtUsername.text as NSString var password:NSString = txtPassword.text as NSString var confirm_password:NSString = txtConfirmPassword.text as NSString if ( username.isEqualToString("") || password.isEqualToString("") ) { var alertView:UIAlertView = UIAlertView() alertView.title = "Sign Up Failed!" alertView.message…