Fork me on GitHub

在xcode中用 swift 进行网络服务请求

  xcode集成开发环境是运行于Mac苹果电脑上用于开发swift应用程序的工具,利用xcode可以很方便、直观的开发OS X和iOS系统所支持的应用程序。

1 开发环境:

   Mac OS 10.11

   Xcode 7.3.1

2 用Xcode创建一个swift项目

这里选择Single View application,后续可以用[editor]菜单进行调整。

3 添加导航栏

在main.storyboard中选中默认的viewcontroller,然后单击菜单[Editor]->[Embed in]->[Navigation Controller],如下图所示:

操作成功后,设计器中就会出现2个有关联的view,如下所示:

这时候,我们就可以拖动相关控件到View Controller上。

拖放好控件后,可以用xcode将UI上的控件拖放到后台源码中进行前后台关联,如下图:

如果遇到无法进行关联,可能是由于UI和后台文件没有正确的关联,或者当前两个文件不匹配。

4 导入第三方库

   先下载Alamofire源码包,然后将Alamofire整个文件拖放到上面创建的项目根目录中,然后将Alamofire.xcodeproj拖放到主项目下

关联后,可以用command+B j进行编译,看有无错误。

这里用同样的方法导入swiftjson等库

5 代码逻辑编写

//
//  MyWebViewController.swift
//  swiftapp
//
//  Created by Jackwang on 16/8/12.
//  Copyright © 2016年 Jackwang . All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import ObjectMapper
import SCLAlertView;

class MyWebViewController: UIViewController {

    @IBOutlet weak var btnReLoad: UIBarButtonItem!
    @IBOutlet weak var btnBack: UIBarButtonItem!
    @IBOutlet weak var btnHome: UIBarButtonItem!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        btnHome.title = "首页";
        
        //https://github.com/Alamofire/Alamofire
        
        Alamofire.request(.GET, "http://192.168.180.159:9888/MicroServiceAPI.ashx", parameters: ["uname": "admin","upwd":"EAS","api":"api.getUsers"])
            .validate()
            .responseJSON { response in
                switch response.result {
                case .Success:
                    print("Validation Successful")
                    if let strJSON = response.result.value {
                        print("JSON: \(strJSON)")
                        
                        //https://github.com/SwiftyJSON/SwiftyJSON
                        let json = JSON(strJSON)
                        print(json["Code"])
                        print(json["IsSuccess"])
                        print(json["Message"])

                        print(json["DTData"][0]["Name"]) //null
                        
                        
                        if json["IsSuccess"].int !=  nil {
                            print("Login Sucess")
                            // Get started
                            SCLAlertView().showInfo("Login Sucess", subTitle: "welcome to app")
                        }
                        if json["DTData"].string !=  nil {
                             SCLAlertView().showInfo("Login data", subTitle: "welcome to app")
                        }
                        //SCLAlertView().showInfo("ok", subTitle: "welcome to app")
                        
                    }
                    
                case .Failure(let error):
                    print(error)
                }
        }

        
        
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func btnHomeTapped(sender: UIBarButtonItem) {
        
        print("GoHome")
    }

    @IBAction func btnReloadTapped(sender: UIBarButtonItem) {
         print("ReLoad")
    }
}

6 编译运行

首次运行,如果没有配置info.plist,会报错误.这里需要打开info.plist文件,单击[+],然后添加

App Transport Security Settings

在其子项目下设置Allow Arbitrary Loads为YES.

 

  

posted @ 2016-08-13 09:08  JackWang-CUMT  阅读(1823)  评论(0编辑  收藏  举报