zoukankan      html  css  js  c++  java
  • 在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.

     

      

  • 相关阅读:
    Webpack中publicPath设置
    忘记Mysql的root密码怎么办?
    Visual Studio 2015上安装Entity Framework Power Tools
    Ubuntu下安装中文输入法
    Ubuntu如何选择更新源
    Orchard中如何配置远端发布
    .Net缓存管理框架CacheManager
    全新的membership框架Asp.net Identity(2)——绕不过的Claims
    全新的membership框架Asp.net Identity(1)——.Net membership的历史
    泛型使用中,解决类型转换问题
  • 原文地址:https://www.cnblogs.com/isaboy/p/xcode_swift_web_json_Alamofire.html
Copyright © 2011-2022 走看看