美文网首页
网络请求带缓存

网络请求带缓存

作者: 白色天空729 | 来源:发表于2018-07-14 19:34 被阅读19次
    //- post请求
    //- parameter _url:      post的地址
    //- parameter _dic:      要传参数字典
    //- parameter _complete: 闭包方法
    func RZ_POST(isPOST _isPOST : Bool
        ,requestUrl _requestUrl: String,
         resultDic _dic:Dictionary<String,Any>?,
         returnArr _returnArr:@escaping (_ data:String)->()) {
        
        let url = URL(string: _requestUrl)
        
    //    let request = NSMutableURLRequest(url: url!)
    /// 重点这句话
        let request = NSMutableURLRequest.init(url: url!, cachePolicy: RZ_netFun() ? .reloadIgnoringCacheData : .returnCacheDataDontLoad, timeoutInterval: 10.0)
        request.httpMethod = _isPOST ? "POST" : "GET"
        request.addValue("application/x-www-form-urlencoded;text/html;charset=UTF-8", forHTTPHeaderField: "Content-Type")
        
        //设置请求体
        //拼接URL
        var i = 0
        var address: String = ""
        
        if let paras = _dic {
            
            for (key,value) in paras {
                
                if i == 0 {
                    address += "\(key)=\(value)"
                } else {
                    address += "&\(key)=\(value)"
                }
                
                i += 1
            }
        }
        
        
        //把拼接后的字符串转换为data,设置请求体
        request.httpBody = address.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
        
        let session = URLSession.shared
        
        DispatchQueue.main.async {
            
            
            /// 显示提示框
            let task:URLSessionDataTask = session.dataTask(with: request as URLRequest, completionHandler: { (result, response, error) -> Void in
                if result == nil {return}
                if let str = String(data: (result)!, encoding: String.Encoding.utf8) {
                    _returnArr(str)
                }
                //            _returnArr(WHC.deserialize(from: str)!)
                
            })
            task.resume()
        }
        
    }
    

    支持判断网络的文件:

    //
    //  Reachability.swift
    //  TestModel2
    //
    //  Created by 郑东喜 on 2018/7/14.
    //  Copyright © 2018 郑东喜. All rights reserved.
    //
    
    import UIKit
    
    /*
     Copyright (c) 2014, Ashley Mills
     All rights reserved.
     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions are met:
     1. Redistributions of source code must retain the above copyright notice, this
     list of conditions and the following disclaimer.
     2. Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     POSSIBILITY OF SUCH DAMAGE.
     */
    import SystemConfiguration
    import Foundation
    
    
    func RZ_netFun()-> Bool
    {
        var boo:Bool = true
        let reachability =  Reachability()
        // 准备获取网络连接信息
        
        if reachability!.isReachable
        {
            // 判断网络连接状态
            print("网络连接:可用")
            boo = true
            if reachability!.isReachableViaWiFi
            { // 判断网络连接类型
                print("连接类型:WiFi")
                boo = true
            }
            else if reachability!.isReachableViaWWAN
            {
                print("连接类型:移动网络",reachability!.isReachableViaWWAN)
                boo = true
            }
        }else{
            print("网络连接:不可用")
            boo = false
        }
        return boo
    }
    
    
    
    public enum ReachabilityError: Error {
        case FailedToCreateWithAddress(sockaddr_in)
        case FailedToCreateWithHostname(String)
        case UnableToSetCallback
        case UnableToSetDispatchQueue
    }
    public let ReachabilityChangedNotification = NSNotification.Name("ReachabilityChangedNotification")
    func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {
        guard let info = info else { return }
        let reachability = Unmanaged<Reachability>.fromOpaque(info).takeUnretainedValue()
        DispatchQueue.main.async {
            reachability.reachabilityChanged()
        }
    }
    public class Reachability {
        public typealias NetworkReachable = (Reachability) -> ()
        public typealias NetworkUnreachable = (Reachability) -> ()
        public enum NetworkStatus: CustomStringConvertible {
            case notReachable, reachableViaWiFi, reachableViaWWAN
            public var description: String {
                switch self {
                case .reachableViaWWAN: return "Cellular"
                case .reachableViaWiFi: return "WiFi"
                case .notReachable: return "No Connection"
                }
            }
        }
        public var whenReachable: NetworkReachable?
        public var whenUnreachable: NetworkUnreachable?
        public var reachableOnWWAN: Bool
        // The notification center on which "reachability changed" events are being posted
        public var notificationCenter: NotificationCenter = NotificationCenter.default
        public var currentReachabilityString: String {
            return "\(currentReachabilityStatus)"
        }
        public var currentReachabilityStatus: NetworkStatus {
            guard isReachable else { return .notReachable }
            if isReachableViaWiFi {
                return .reachableViaWiFi
            }
            if isRunningOnDevice {
                return .reachableViaWWAN
            }
            return .notReachable
        }
        fileprivate var previousFlags: SCNetworkReachabilityFlags?
        fileprivate var isRunningOnDevice: Bool = {
            #if targetEnvironment(simulator)
            return false
            #else
            return true
            #endif
        }()
        fileprivate var notifierRunning = false
        fileprivate var reachabilityRef: SCNetworkReachability?
        fileprivate let reachabilitySerialQueue = DispatchQueue(label: "uk.co.ashleymills.reachability")
        required public init(reachabilityRef: SCNetworkReachability) {
            reachableOnWWAN = true
            self.reachabilityRef = reachabilityRef
        }
        public convenience init?(hostname: String) {
            guard let ref = SCNetworkReachabilityCreateWithName(nil, hostname) else { return nil }
            self.init(reachabilityRef: ref)
        }
        public convenience init?() {
            var zeroAddress = sockaddr()
            zeroAddress.sa_len = UInt8(MemoryLayout<sockaddr>.size)
            zeroAddress.sa_family = sa_family_t(AF_INET)
            guard let ref: SCNetworkReachability = withUnsafePointer(to: &zeroAddress, {
                SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
            }) else { return nil }
            self.init(reachabilityRef: ref)
        }
        deinit {
            stopNotifier()
            reachabilityRef = nil
            whenReachable = nil
            whenUnreachable = nil
        }
    }
    public extension Reachability {
        // MARK: - *** Notifier methods ***
        func startNotifier() throws {
            guard let reachabilityRef = reachabilityRef, !notifierRunning else { return }
            var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
            context.info = UnsafeMutableRawPointer(Unmanaged<Reachability>.passUnretained(self).toOpaque())
            if !SCNetworkReachabilitySetCallback(reachabilityRef, callback, &context) {
                stopNotifier()
                throw ReachabilityError.UnableToSetCallback
            }
            if !SCNetworkReachabilitySetDispatchQueue(reachabilityRef, reachabilitySerialQueue) {
                stopNotifier()
                throw ReachabilityError.UnableToSetDispatchQueue
            }
            // Perform an intial check
            reachabilitySerialQueue.async {
                self.reachabilityChanged()
            }
            notifierRunning = true
        }
        func stopNotifier() {
            defer { notifierRunning = false }
            guard let reachabilityRef = reachabilityRef else { return }
            SCNetworkReachabilitySetCallback(reachabilityRef, nil, nil)
            SCNetworkReachabilitySetDispatchQueue(reachabilityRef, nil)
        }
        // MARK: - *** Connection test methods ***
        var isReachable: Bool {
            guard isReachableFlagSet else { return false }
            if isConnectionRequiredAndTransientFlagSet {
                return false
            }
            if isRunningOnDevice {
                if isOnWWANFlagSet && !reachableOnWWAN {
                    // We don't want to connect when on 3G.
                    return false
                }
            }
            return true
        }
        var isReachableViaWWAN: Bool {
            // Check we're not on the simulator, we're REACHABLE and check we're on WWAN
            return isRunningOnDevice && isReachableFlagSet && isOnWWANFlagSet
        }
        var isReachableViaWiFi: Bool {
            // Check we're reachable
            guard isReachableFlagSet else { return false }
            // If reachable we're reachable, but not on an iOS device (i.e. simulator), we must be on WiFi
            guard isRunningOnDevice else { return true }
            // Check we're NOT on WWAN
            return !isOnWWANFlagSet
        }
        var description: String {
            let W = isRunningOnDevice ? (isOnWWANFlagSet ? "W" : "-") : "X"
            let R = isReachableFlagSet ? "R" : "-"
            let c = isConnectionRequiredFlagSet ? "c" : "-"
            let t = isTransientConnectionFlagSet ? "t" : "-"
            let i = isInterventionRequiredFlagSet ? "i" : "-"
            let C = isConnectionOnTrafficFlagSet ? "C" : "-"
            let D = isConnectionOnDemandFlagSet ? "D" : "-"
            let l = isLocalAddressFlagSet ? "l" : "-"
            let d = isDirectFlagSet ? "d" : "-"
            return "\(W)\(R) \(c)\(t)\(i)\(C)\(D)\(l)\(d)"
        }
    }
    fileprivate extension Reachability {
        func reachabilityChanged() {
            let flags = reachabilityFlags
            guard previousFlags != flags else { return }
            let block = isReachable ? whenReachable : whenUnreachable
            block?(self)
            self.notificationCenter.post(name: ReachabilityChangedNotification, object:self)
            previousFlags = flags
        }
        var isOnWWANFlagSet: Bool {
            #if os(iOS)
            return reachabilityFlags.contains(.isWWAN)
            #else
            return false
            #endif
        }
        var isReachableFlagSet: Bool {
            return reachabilityFlags.contains(.reachable)
        }
        var isConnectionRequiredFlagSet: Bool {
            return reachabilityFlags.contains(.connectionRequired)
        }
        var isInterventionRequiredFlagSet: Bool {
            return reachabilityFlags.contains(.interventionRequired)
        }
        var isConnectionOnTrafficFlagSet: Bool {
            return reachabilityFlags.contains(.connectionOnTraffic)
        }
        var isConnectionOnDemandFlagSet: Bool {
            return reachabilityFlags.contains(.connectionOnDemand)
        }
        var isConnectionOnTrafficOrDemandFlagSet: Bool {
            return !reachabilityFlags.intersection([.connectionOnTraffic, .connectionOnDemand]).isEmpty
        }
        var isTransientConnectionFlagSet: Bool {
            return reachabilityFlags.contains(.transientConnection)
        }
        var isLocalAddressFlagSet: Bool {
            return reachabilityFlags.contains(.isLocalAddress)
        }
        var isDirectFlagSet: Bool {
            return reachabilityFlags.contains(.isDirect)
        }
        var isConnectionRequiredAndTransientFlagSet: Bool {
            return reachabilityFlags.intersection([.connectionRequired, .transientConnection]) == [.connectionRequired, .transientConnection]
        }
        var reachabilityFlags: SCNetworkReachabilityFlags {
            guard let reachabilityRef = reachabilityRef else { return SCNetworkReachabilityFlags() }
            var flags = SCNetworkReachabilityFlags()
            let gotFlags = withUnsafeMutablePointer(to: &flags) {
                SCNetworkReachabilityGetFlags(reachabilityRef, UnsafeMutablePointer($0))
            }
            if gotFlags {
                return flags
            } else {
                return SCNetworkReachabilityFlags()
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:网络请求带缓存

          本文链接:https://www.haomeiwen.com/subject/dfoypftx.html