★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xnpwirkl-kv.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目錄:[Swift]通天遁地Swiftios
本文將演示如何使用第三方庫實現網絡請求服務。git
首先確保在項目中已經安裝了所需的第三方庫。github
點擊【Podfile】,查看安裝配置文件。swift
1 source 'https://github.com/CocoaPods/Specs.git' 2 platform :ios, '12.0' 3 use_frameworks! 4 5 target ‘DemoApp’ do 6 pod 'Alamofire', '~> 4.0' 7 end
根據配置文件中的相關配置,安裝第三方庫。服務器
而後點擊打開【DemoApp.xcworkspace】項目文件。微信
在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】網絡
如今開始編寫代碼,訪問一個網絡接口,並在控制檯輸出返回的信息。ide
1 import UIKit 2 //在當前的類文件中,引入已經安裝的第三方類庫 3 import Alamofire 4 5 class ViewController: UIViewController { 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view, typically from a nib. 10 11 //調用網絡操做庫的網絡請求方法,並處理從服務器返回的JSON信息 12 Alamofire.request("https://httpbin.org/get").responseJSON { response in 13 14 //在控制檯輸出:返回的網絡請求對象 15 print("response.request:\(response.request)") 16 //在控制檯輸出:網絡返回對象 17 print("response.response:\(response.response)") 18 //在控制檯輸出:由服務器返回的數據 19 print("response.data:\(response.data)") 20 //在控制檯輸出:返回對象序列化後的結果 21 print("response.result:\(response.result)") 22 23 //輸出結果的值 24 if let JSON = response.result.value 25 { 26 print("JSON: \(JSON)") 27 } 28 } 29 } 30 31 override func didReceiveMemoryWarning() { 32 super.didReceiveMemoryWarning() 33 // Dispose of any resources that can be recreated. 34 } 35 }