脚本宝典收集整理的这篇文章主要介绍了IOS15之swift的Alamofire 5.4框架的网络封装,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
此Alamofire 版本较高,是基于IOS15 和最新的 Alamofire 5.4.4 版本,截止我发稿的时候,最新版本,网上其他框架的版本较低,api早就淘汰了,或者其他写法,(对回调函数所使用到的关键字@escaping)要重点掌握 后台我采用Java 的sPRingBoot进行封装测试的。 涉及swift基础语法,cocoaPods的使用,swift的闭包,回调,swift5.5语法新特性,单例模式的使用等,类方法的定义,Alamofire 的get,post,put ,delete请求,带参数,或者不带参数,都要拿捏的非常准备到位
// // NetworkTools.swift // Alamofire1 // // Created by lujun on 2021/10/2. // import UIKIT import Alamofire enum MethodTyPE { case GET case POST } class NetworkTools{ //类方法 class func requestData(type: MethodType,urlString: String, parameters: [String : Any]? = nil,finishedCallback : @escaping ( _ results : Any) -> ()){ // let type1 = type==;methodType.GET ? ".get" : ".post" if(type==MethodType.GET){ Alamofire.AF.request(urlString).responseJSON { (response) in switch response.result { case .success(let json): finishedCallback(json) break case .failure(let error): print("error:(error)") break } } } if(type==MethodType.POST){ Alamofire.AF.request(urlString, method: .post, parameters: parameters).responseJSON { (response) in switch response.result { case .success(let json): finishedCallback(json) break case .failure(let error): print("error:(error)") break } } } } //类方法 class func requestDataWithParam(type: MethodType,urlString: String, parameters: [String : Any],finishedCallback : @escaping ( _ results : Any) -> ()){ AF.request(urlString, parameters: parameters).responseJSON { (response) in switch response.result { case .success(let json): finishedCallback(json) break case .failure(let error): print("error:(error)") break } } } }
@H_32_512@测试代码如下:
// // ViewController.swift // Alamofire1 // // Created by lujun on 2021/10/2. // import UIKit import Alamofire class ViewController: UIViewController { /* AF.request("https://httpbin.org/get").responseJSON{ (response) in Guard let result = response.value else{ print(response.error ?? "") return } print(result) } */ override func viewDidLoad() { super.viewDidLoad() NetworkTools.requestDataWithParam(type: .GET, urlString: "http://localhost:8081/demo3",parameters: ["sn":"lujun"]) { result in print(result) } } func getRequest(){ let url = "https://httpbin.org/get" Alamofire.AF.request(url).responseJSON { (response) in switch response.result { case .success(let json): print(json) // Success in request and do a async or sync(NOT UI) task here. break case .failure(let error): print("error:(error)") break } } } func postRequest(){ let url = "https://httpbin.org/post" Alamofire.AF.request(url, method: .post, parameters: ["name":"jack","password":"123456"]).responseJSON { (response) in switch response.result { case .success(let json): print(json) break case .failure(let error): print("error:(error)") break } } } }
Java 后台
package com.example.demo.controller; import com.example.demo.DAO.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController public class DemoController { @GetMapping(value = "/demo") public String test2(String sn){ System.out.println(sn); return "test"; } @GetMapping(value = "/demo3") public Object test23(String sn){ Map m1 = new HashMap<String,Object>(); System.out.println(sn); User user = new User("lujun", 18, "123@QQ.COM"); m1.put("u1",user); return m1; } }
项目文件名在我的资源文件下。名称是Alamofire1.zip
以上是脚本宝典为你收集整理的IOS15之swift的Alamofire 5.4框架的网络封装全部内容,希望文章能够帮你解决IOS15之swift的Alamofire 5.4框架的网络封装所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。