资讯专栏INFORMATION COLUMN

IOS-Swift开发基础——网络通信

yintaolaowanzi / 492人阅读

摘要:需要设置如下是个第三方的语言的网络开发工具包,基于,但是更娇便于使用。

NSURLSession
var imageView = UIImageView(frame: CGRectMake(40, 40, 200, 200))
var curTime = NSDateFormatter()

override func viewDidLoad() {
    super.viewDidLoad()

    curTime.dateFormat = "HH:mm:ss"
    print(curTime.stringFromDate(NSDate()))
    
    imageView.contentMode = UIViewContentMode.ScaleAspectFit
    self.view.addSubview(imageView)
    
    let url = "http://pic1.nipic.com/2008-12-29/2008122993940613_2.jpg"
    let imageURL = NSURL(string: url)
    let urlRequest = NSURLRequest(URL: imageURL!)
   
    // let session = NSURLSession.sharedSession() // # 1
    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) // # 2
    let task = session.dataTaskWithRequest(urlRequest, completionHandler: {(data, response, error) -> Void in
        let imgData = NSData(data: data!)
        self.imageView.image = UIImage(data: imgData)
    })
    task.resume()
    
    print(curTime.stringFromDate(NSDate()))
}

IOS9需要设置如下:

Alamofire

Alamofire

Alamofire是个第三方的Swift语言的HTTP网络开发工具包,基于NSURLSession,但是更娇便于使用。

import Alamofire

// # 1
Alamofire.request(.GET, "https://httpbin.org/get").responseJSON { (response) -> Void in
    print(response.result)
    print(response.result.value)
}

// # 2
Alamofire.request(.GET, "https://www.baidu.com").responseJSON { (response) -> Void in
    print(response.result.value)
}

// # 3
let url = "http://pic1.nipic.com/2008-12-29/2008122993940613_2.jpg"
    
Alamofire.request(.GET, url).responseData { (response) -> Void in
    self.imageView.image = UIImage(data: NSData(data: response.result.value!))
}

和SwiftyJSON一起用:

Alamofire.request(.GET, url).validate().responseJSON { response in
    switch response.result {
    case .Success:
        if let value = response.result.value {
          let json = JSON(value)
          print("JSON: (json)")
        }
    case .Failure(let error):
        print(error)
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/16167.html

相关文章

  • IOS-Swift开发基础——检测网络状态

    摘要:检测网络状态可以用的例子里的类,拷贝到我们的项目里,然后导给用。另外,上也有个版本的将里面的扒过来,加入我们的工程中。然后如下使用连接类型连接类型移动网络连接类型没有网络连接当前连接为当前连接为蜂窝数据当前无连接 检测网络状态可以用Apple-Reachability的例子里的Reachability类(objective-c),拷贝到我们的项目里,然后导给Swift用。 另外,git...

    jzman 评论0 收藏0
  • IOS-Swift开发基础——通知

    摘要:是专门供程序中不同类间的消息通信的。使用它为我们代码降低耦合。 NSNotificationCenter NSNotificationCenter是专门供程序中不同类间的消息通信的。使用它为我们代码降低耦合。 自定义数据监听 注册监听: // addObserver 4个参数分别是:接受者对象,接受者处理函数,消息名称,发送者对象(通常设为nil) NSNotificationCent...

    April 评论0 收藏0
  • IOS-Swift开发基础——使用相机拍照

    摘要:下面例子来示范使用相机拍照并显示出我们的照片。分别绑定然后添加下面代码到,来使用相机拍照先要判断相机是否可用允许拍摄图片后编辑继承,为我们实现了拍照后的响应分别是点击完成后处理照片,点击取消。 UIImagePickerController 是系统提供的用来获取图片和视频的接口。 下面例子来示范使用相机拍照并显示出我们的照片。 先设置ViewController继承UIImagePic...

    guyan0319 评论0 收藏0
  • IOS-Swift开发基础——文件管理

    摘要:默认情况下,每个沙盒含有个文件夹和。创建文件或创建文件夹文件检查移动重命名拷贝目录下所有文件会遍历到所有子目录删除文件 FileManager IOS应用只能访问自己应用目录下的文件。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。 let homeDir = NSHomeDirector...

    Michael_Ding 评论0 收藏0
  • IOS-Swift开发基础——JSON处理

    摘要:与比较的方法解析张三生成使用起来还是不太爽,下面对比下这个代码基本看不懂了啊就这么简单取到了。 SwiftyJSON 与NSJSONSerialization比较 NSJSONSerialization的方法: //解析Json let jsonStr = {usr:张三,info:{number:188,address:51load}} let jsonData = jsonStr....

    Betta 评论0 收藏0

发表评论

0条评论

yintaolaowanzi

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<