Swift 简易封装URLSession

协议:

import Foundation

protocol NetworkProtocol {
    static func postRequest(URLString: String, parameters : [String : Any]?, callback: @escaping (_ result: Any) -> ())
    static func getRequest(URLString: String, parameters : [String : Any]?,callback: @escaping (_ result: Any) -> ())
}

URLSession层:

import Foundation

class BootLayer{
    class func getRequest(urlString: String, callback: @escaping (Any) -> ()){
        let config = URLSessionConfiguration.default
        let url = URL(string: urlString)
        var request = URLRequest(url: url!)
        let session = URLSession(configuration: config)
        let task = session.dataTask(with: request) { (data, response, error) in
            let dictionary = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
            callback(dictionary)
        }
        task.resume()
    }
    
    class func postRequest(urlString: String, callback: @escaping (Any) -> ()){
        let config = URLSessionConfiguration.default
        let url = URL(string: urlString)
        var request = URLRequest(url: url!)
        request.httpMethod = "POST"
        let session = URLSession(configuration: config)
        let task = session.dataTask(with: request) { (data, response, error) in
            let dictionary = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
            callback(dictionary)
        }
        task.resume()
    }
    
}

实现层:

import Foundation

class NetworkManager: NetworkProtocol{
    
    static func postRequest(URLString: String, parameters: [String : Any]?, callback: @escaping (Any) -> ()) {
        BootLayer.getRequest(urlString: URLString) { (dictionary) in
            callback(dictionary)
        }
    }
    
    static func getRequest(URLString: String, parameters: [String : Any]?, callback: @escaping (Any) -> ()) {
        BootLayer.getRequest(urlString: URLString) { (dictionary) in
            callback(dictionary)
        }
    }
    
}

调用:

        NetworkManager.getRequest(URLString: "https://itunes.apple.com/lookup?id=1407674824", parameters: nil) { (data) in
            print("------> DATA:\(data)")
        }

结果:

------> DATA:Optional({
    resultCount = 1;
    results =     (
                {
            advisories =             (
            );
            appletvScreenshotUrls =             (
            );
            artistId = 1407674823;
            artistName = "\U519b \U8881";
            artistViewUrl = "https://apps.apple.com/us/developer/%E5%86%9B-%E8%A2%81/id1407674823?uo=4";
            artworkUrl100 = "https://is1-ssl.mzstatic.com/image/thumb/Purple125/v4/cd/d6/40/cdd64041-cbce-02b9-3a9b-5dbe40262f17/source/100x100bb.jpg";
            artworkUrl512 = "https://is1-ssl.mzstatic.com/image/thumb/Purple125/v4/cd/d6/40/cdd64041-cbce-02b9-3a9b-5dbe40262f17/source/512x512bb.jpg";
            artworkUrl60 = "https://is1-ssl.mzstatic.com/image/thumb/Purple125/v4/cd/d6/40/cdd64041-cbce-02b9-3a9b-5dbe40262f17/source/60x60bb.jpg";
            averageUserRating = "4.9451200000000001821831574488896876573";
            averageUserRatingForCurrentVersion = "4.9451200000000001821831574488896876573";
            bundleId = "com.runbey.rbjk";
            contentAdvisoryRating = "4+";
            currency = USD;
            currentVersionReleaseDate = "2021-09-08T00:21:47Z";
            description = "\U5143\U8d1d\U9a7e\U8003\U9a7e\U7167\U5b66\U8f66\U5b9d\U5178\Uff0c99\U5b66\U8f66\U8282\U706b\U70ed\U6765\U88ad\Uff01\U5168\U56fd\U8fd1\U4e07\U6240\U77e5\U540d\U9a7e\U6821\U5df2\U5165\U9a7b\Uff0c\U4e25\U9009\U9a7e\U6821\U4fbf\U5b9c\U62a5\Uff0c\U73b0\U5728\U5b66\U8f66\U4f18\U60e0\U591a\U3002\n2021\U9a7e\U8003\U9a7e\U7167\U6cd5\U89c4\U5feb\U901f\U8fc7\U5173\Uff0c\U5341\U4f59\U5e74\U4e13\U4e1a\U9a7e\U57f9\U7ecf\U9a8c\Uff0c\U63d0\U4f9b\U5c0f\U8f66\U3001\U8d27\U8f66\U3001\U5ba2\U8f66\U3001\U6469\U6258\U8f66\U7b49\U8f66\U578b\U8003\U8bd5\U771f\U9898\U3002\n\U4e0b\U8f7d\U5373\U53ef\U67e5\U770b\U9a7e\U7167\U8003\U8bd5\U65b0\U6280\U5de7\Uff0c\U540c\U6b65\U66f4\U65b0\U8003\U8bd5\U65b0\U9898\U5e93\Uff0c\U738b\U724c\U6559\U7ec3\U5458\U63a8\U8350\U5b66\U8f66APP\Uff0c\U503c\U5f97\U4fe1\U8d56\Uff01\n\U5168\U56fd\U901a\U7528\U9a7e\U8003\U9884\U7ea6\U7406\U8bba\U5b9d\U5178\Uff0c\U5e7f\U53d7\U6b22\U8fce\U7684\U9a7e\U6821\U5b66\U8f66\U4e00\U70b9\U901a\Uff0c\U767e\U4e07\U8f66\U8f6e\U9a7e\U9a76\U5458\U5b66\U8f66\U4fe1\U8d56APP\U3002\n\U7279\U9080\U8d44\U6df1\U6559\U7ec3\U5458\U548c\U9a7e\U9a76\U5458\U57f9\U8bad\U884c\U4e1a\U4e13\U5bb6\U6df1\U5ea6\U5256\U6790\U4ea4\U89c4\U8bb2\U89e3\U9a7e\U7167\U8003\U8bd5\U6280\U5de7\Uff0c\U9a7e\U8003\U79d1\U76ee\U4e00\U3001\U79d1\U76ee\U56db\U7406\U8bba\U6587\U660e\U9a7e\U9a76\U5185\U5bb9\U3002\n\U5feb\U901f\U719f\U6089\U9a7e\U9a76\U6280\U80fd\U3001\U5b66\U8f66\U6280\U5de7\Uff0c\U8003\U9a7e\U7167\U66f4\U6709\U628a\U63e1\Uff01\U5feb\U901f\U63d0\U9ad8\U9a7e\U8003\U6210\U7ee9\Uff0c\U5b66\U8f66\U8f7b\U677e\U9ad8\U5206\U901a\U5173\U3002\n\n\U2014\U2014\U4ea7\U54c1\U4eae\U70b9\U2014\U2014\n\U9898\U5e93\U65b0\Uff1a\U5728\U7ebf\U5b9e\U65f6\U66f4\U65b0\Uff0c\U540c\U6b65\U8003\U8bd5\U9898\U5e93\Uff1b\n\U4fdd\U969c\U9ad8\Uff1aAI\U667a\U80fd\U51fa\U8bd5\U9898\Uff0c\U63d0\U524d\U719f\U6089\U8bd5\U5377\Uff1b\n\U4e13\U4e1a\U5f3a\Uff1a\U5341\U5e74\U4e13\U6ce8\U9a7e\U8003\Uff0c\U53e3\U7891\U5b9e\U529b\U4fdd\U969c\Uff1b\n\U53e3\U7891\U68d2\Uff1a\U9a7e\U6821\U5408\U4f5c\U54c1\U724c\Uff0c\U738b\U724c\U6559\U7ec3\U63a8\U8350\U3002\n\n\U2014\U2014\U4ea7\U54c1\U7279\U8272\U2014\U2014\n1\U3001\U4e1a\U754c\U6807\U51c6\Uff1a\U91c7\U75282021\U65b0\U6807\U51c6\Uff0c\U9a7e\U7167\U8003\U8bd5\U9898\U5e93\U79bb\U7ebf\U7248\Uff0c\U9a7e\U8003\U795e\U5668\Uff0c\U652f\U6301\U79bb\U7ebf\Uff0c\U9002\U7528\U4e8e\U5404\U7c7b\U9a7e\U7167\U8003\U8bd5\U5b66\U4e60\U3002\n2\U3001\U6d41\U7a0b\U5168\U9762\Uff1a\U9a7e\U6821\U7406\U8bba\U6a21\U62df\U8003\U8bd5\U3001\U79d1\U76ee\U4e00\U3001\U79d1\U76ee\U4e8c\U3001\U79d1\U76ee\U4e09\U3001\U79d1\U76ee\U56db\Uff0c\U8003\U9a7e\U7167\U6d41\U7a0b\Uff0c\U4e00\U4e2aapp\U5168\U90fd\U641e\U5b9a\U3002\n3\U3001\U7406\U8bba\U79d8\U7c4d\Uff1a\U9010\U9898\U89e3\U6790\U77e5\U8bc6\U70b9\Uff0c\U5de7\U5999\U8bb0\U5fc6\U4ea4\U901a\U6807\U5fd7\U3001\U4ea4\U8b66\U624b\U52bf\Uff0c\U66f4\U6709\U79d1\U76ee\U4e00\U3001\U79d1\U76ee\U56db\U8003\U8bd5\U6280\U5de7\U4e0e\U6ee1\U5206\U7ecf\U9a8c\U5206\U4eab\U3002\n4\U3001\U5168\U771f\U6a21\U62df\Uff1a\U5168\U771f\U9898\U5e93\U8003\U8bd5\Uff0c\U771f\U5b9e\U518d\U73b0\U7406\U8bba\U8003\U8bd5\U573a\U666f\Uff0c\U8def\U8003\U89c6\U9891\U6559\U7a0b\Uff0c\U5b66\U4e60\U66f4\U6709\U6548\Uff0c\U8003\U9a7e\U7167\U66f4\U8f7b\U677e\U3002\n5\U3001\U9898\U76ee\U8bc4\U8bba\Uff1a\U6c42\U52a9\U3001\U5410\U69fd\U3001\U6652\U9a7e\U7167\Uff0c\U5168\U56fd\U5404\U5730\U5b66\U5458\U4e00\U8d77\U4ea4\U6d41\U9898\U76ee\U7ecf\U9a8c\U3001\U8ba8\U8bba\U8003\U9a7e\U7167\U5fc3\U5f97\Uff0c\U5b66\U8f66\U4e0d\U5bc2\U5bde\Uff0c\U582a\U79f0\U5b66\U8f66\U754c\U201c\U9a7e\U8003\U4e00\U70b9\U901a\U201d\U3002\n\n\U2014\U2014\U8054\U7cfb\U6211\U4eec\U2014\U2014\n\U5fae\U4fe1\U516c\U4f17\U53f7\Uff1a\U5143\U8d1d\U9a7e\U8003\n\U5b98\U65b9\U7f51\U7ad9\Uff1awww.ybjk.com\n\U5546\U52a1\U5408\U4f5c\[email protected]\n\U7528\U6237\U53cd\U9988\Uff1a025-58679968";
            features =             (
                iosUniversal
            );
            fileSizeBytes = 170125312;
            formattedPrice = Free;
            genreIds =             (
                6017,
                6006
            );
            genres =             (
                Education,
                Reference
            );
            ipadScreenshotUrls =             (
                "https://is2-ssl.mzstatic.com/image/thumb/PurpleSource125/v4/0f/d1/25/0fd12516-adb9-9382-8f82-9cb285badcba/81fb6fbf-dd48-4113-9d82-72fda73bbd92_iPad_0.jpg/576x768bb.jpg",
                "https://is4-ssl.mzstatic.com/image/thumb/Purple115/v4/7f/d1/54/7fd154d9-4e1a-47e4-7681-dd18e27498ff/7ba4a9c3-a12f-4faf-9a6d-74f369d87246_iPad_1.jpg/576x768bb.jpg",
                "https://is4-ssl.mzstatic.com/image/thumb/Purple125/v4/3b/cd/30/3bcd3071-ed46-a2a7-4f43-1cb17a096cde/09b58dc6-6c6e-4d8e-aba9-5baac2e5ab98_iPad_2.jpg/576x768bb.jpg",
                "https://is5-ssl.mzstatic.com/image/thumb/Purple115/v4/20/d8/bd/20d8bdc2-8738-4957-bf65-46d6248587ca/b7dcba1d-4fec-45cd-b31d-1c7d0c207d4c_iPad_3.jpg/576x768bb.jpg",
                "https://is5-ssl.mzstatic.com/image/thumb/Purple125/v4/1d/20/31/1d203140-b41a-c125-49fd-1d27ab86ab0a/158e8fb8-3e1c-4226-8e34-884913ae630b_iPad_4.jpg/576x768bb.jpg",
                "https://is2-ssl.mzstatic.com/image/thumb/Purple125/v4/47/79/18/477918a1-d46f-909f-03f5-200f5105282b/a7b96b75-052d-4943-acb2-74b86d79af4e_iPad_5__U53bb_U4ef7_U683c.jpg/576x768bb.jpg"
            );
            isGameCenterEnabled = 0;
            isVppDeviceBasedLicensingEnabled = 1;
            kind = software;
            languageCodesISO2A =             (
                ZH
            );
            minimumOsVersion = "9.0";
            price = 0;
            primaryGenreId = 6017;
            primaryGenreName = Education;
            releaseDate = "2018-07-31T22:22:59Z";
            releaseNotes = "99\U5b66\U8f66\U8282\Uff0c\U5f00\U5b66\U5609\U5e74\U534e\Uff0c\U4e25\U9009\U9a7e\U6821\U4fbf\U5b9c\U62a5\n\n-\U65b0\U589e\U201c\U4e24\U5ba2\U4e00\U5371\U201d\U9898\U5e93\n-\U4f18\U5316\U5b66\U8f66\U4e4b\U65c5\U4f7f\U7528\U4f53\U9a8c\n-\U4fee\U590d\U4e86\U4e00\U4e9b\U5176\U4ed6\U95ee\U9898\n\n\U6211\U4eec\U7684\U70b9\U6ef4\U66f4\U65b0\U4f18\U5316\Uff0c\U90fd\U5e0c\U671b\U6709\U60a8\U7684\U8ba4\U53ef\Uff0c\U795d\U60a8\U62ff\U8bc1\U987a\U5229\Uff01";
            screenshotUrls =             (
                "https://is3-ssl.mzstatic.com/image/thumb/PurpleSource125/v4/e2/e4/cb/e2e4cb06-bedf-b1b6-b104-c08cdb963874/28a034b5-2073-4043-bb7b-fec0c0a73dc9_iPhone_0.jpg/392x696bb.jpg",
                "https://is4-ssl.mzstatic.com/image/thumb/Purple115/v4/9f/c6/1b/9fc61ba3-9f31-a459-0ce1-84406fb39a8e/2ed43a0d-40e5-4f39-aef7-4ba5cf5aaa0e_iPhone_1.jpg/392x696bb.jpg",
                "https://is3-ssl.mzstatic.com/image/thumb/Purple125/v4/1f/d7/2d/1fd72d0f-5a0a-5eff-4294-f49f1e1d0871/8e04840e-0300-4d4e-acf1-011999b94909_iPhone_2.jpg/392x696bb.jpg",
                "https://is2-ssl.mzstatic.com/image/thumb/Purple125/v4/6a/9b/fc/6a9bfc6c-0ec5-fb34-c082-1ec673e6b45f/4dbc50be-22bf-4bb9-a3ee-063870d5c139_iPhone_3.jpg/392x696bb.jpg",
                "https://is5-ssl.mzstatic.com/image/thumb/Purple115/v4/11/68/eb/1168eb59-8e49-39ee-237e-fb9555efdc15/241a554e-e91d-45ac-9113-68b63f066f24_iPhone_4.jpg/392x696bb.jpg",
                "https://is1-ssl.mzstatic.com/image/thumb/Purple115/v4/a7/29/1f/a7291fb6-d8a2-708e-3ab5-bcc07e00d82b/d5e7a840-d28e-4cc3-ad9c-ee9b6be682e7_iPhone_5__U53bb_U4ef7_U683c.jpg/392x696bb.jpg"
            );
            sellerName = "\U519b \U8881";
            supportedDevices =             (
                "iPhone5s-iPhone5s",
                "iPadAir-iPadAir",
                "iPadAirCellular-iPadAirCellular",
                "iPadMiniRetina-iPadMiniRetina",
                "iPadMiniRetinaCellular-iPadMiniRetinaCellular",
                "iPhone6-iPhone6",
                "iPhone6Plus-iPhone6Plus",
                "iPadAir2-iPadAir2",
                "iPadAir2Cellular-iPadAir2Cellular",
                "iPadMini3-iPadMini3",
                "iPadMini3Cellular-iPadMini3Cellular",
                "iPodTouchSixthGen-iPodTouchSixthGen",
                "iPhone6s-iPhone6s",
                "iPhone6sPlus-iPhone6sPlus",
                "iPadMini4-iPadMini4",
                "iPadMini4Cellular-iPadMini4Cellular",
                "iPadPro-iPadPro",
                "iPadProCellular-iPadProCellular",
                "iPadPro97-iPadPro97",
                "iPadPro97Cellular-iPadPro97Cellular",
                "iPhoneSE-iPhoneSE",
                "iPhone7-iPhone7",
                "iPhone7Plus-iPhone7Plus",
                "iPad611-iPad611",
                "iPad612-iPad612",
                "iPad71-iPad71",
                "iPad72-iPad72",
                "iPad73-iPad73",
                "iPad74-iPad74",
                "iPhone8-iPhone8",
                "iPhone8Plus-iPhone8Plus",
                "iPhoneX-iPhoneX",
                "iPad75-iPad75",
                "iPad76-iPad76",
                "iPhoneXS-iPhoneXS",
                "iPhoneXSMax-iPhoneXSMax",
                "iPhoneXR-iPhoneXR",
                "iPad812-iPad812",
                "iPad834-iPad834",
                "iPad856-iPad856",
                "iPad878-iPad878",
                "iPadMini5-iPadMini5",
                "iPadMini5Cellular-iPadMini5Cellular",
                "iPadAir3-iPadAir3",
                "iPadAir3Cellular-iPadAir3Cellular",
                "iPodTouchSeventhGen-iPodTouchSeventhGen",
                "iPhone11-iPhone11",
                "iPhone11Pro-iPhone11Pro",
                "iPadSeventhGen-iPadSeventhGen",
                "iPadSeventhGenCellular-iPadSeventhGenCellular",
                "iPhone11ProMax-iPhone11ProMax",
                "iPhoneSESecondGen-iPhoneSESecondGen",
                "iPadProSecondGen-iPadProSecondGen",
                "iPadProSecondGenCellular-iPadProSecondGenCellular",
                "iPadProFourthGen-iPadProFourthGen",
                "iPadProFourthGenCellular-iPadProFourthGenCellular",
                "iPhone12Mini-iPhone12Mini",
                "iPhone12-iPhone12",
                "iPhone12Pro-iPhone12Pro",
                "iPhone12ProMax-iPhone12ProMax",
                "iPadAir4-iPadAir4",
                "iPadAir4Cellular-iPadAir4Cellular",
                "iPadEighthGen-iPadEighthGen",
                "iPadEighthGenCellular-iPadEighthGenCellular",
                "iPadProThirdGen-iPadProThirdGen",
                "iPadProThirdGenCellular-iPadProThirdGenCellular",
                "iPadProFifthGen-iPadProFifthGen",
                "iPadProFifthGenCellular-iPadProFifthGenCellular",
                "iPhone13Pro-iPhone13Pro",
                "iPhone13ProMax-iPhone13ProMax",
                "iPhone13Mini-iPhone13Mini",
                "iPhone13-iPhone13",
                "iPadMiniSixthGen-iPadMiniSixthGen",
                "iPadMiniSixthGenCellular-iPadMiniSixthGenCellular",
                "iPadNinthGen-iPadNinthGen",
                "iPadNinthGenCellular-iPadNinthGenCellular"
            );
            trackCensoredName = "\U5143\U8d1d\U9a7e\U8003-2021\U627e\U9a7e\U6821\U7406\U8bba\U8003\U8bd5\U5b66\U4e60\U9898\U5e93";
            trackContentRating = "4+";
            trackId = 1407674824;
            trackName = "\U5143\U8d1d\U9a7e\U8003-2021\U627e\U9a7e\U6821\U7406\U8bba\U8003\U8bd5\U5b66\U4e60\U9898\U5e93";
            trackViewUrl = "https://apps.apple.com/us/app/%E5%85%83%E8%B4%9D%E9%A9%BE%E8%80%83-2021%E6%89%BE%E9%A9%BE%E6%A0%A1%E7%90%86%E8%AE%BA%E8%80%83%E8%AF%95%E5%AD%A6%E4%B9%A0%E9%A2%98%E5%BA%93/id1407674824?uo=4";
            userRatingCount = 164;
            userRatingCountForCurrentVersion = 164;
            version = "3.8.1";
            wrapperType = software;
        }
    );
})

你可能感兴趣的:(iOS,swift,ios,http)