How to Call API using AFNetwork in Swift

  •  Install Pod file on your Project
    1. Write this code on your Project and Used it. 
    import UIKit
    
    class AppHandler: NSObject 
    {
     class var sharedInstance: AppHandler {
            struct Static {
                static var onceToken: Int = 0
                static var instance = AppHandler()
            }
            return Static.instance
        }
     let manager = AFHTTPRequestOperationManager();
     
     func checkForNewUpdates(){
            let parameters = NSMutableDictionary();
           
            parameters.setObject("1", forKey: "U_ID" as NSCopying);  
            
            AppHandler.sharedInstance.manager.post( "Your URL Type HERE",
                parameters: parameters,
                success: { (operation,responseObject) in
                    
                    Z.log("JSON: " + (responseObject as AnyObject).description)
                    
                    if let errorResponse = (responseObject as AnyObject).value(forKeyPath: "responseState") as? NSDictionary{
                        if let errorCode = errorResponse.value(forKey: "status") as? Int{
                            if errorCode == 0{
                                let postsFromResponse = (responseObject as AnyObject).value(forKeyPath: "responseData") as! NSDictionary;
                                let isUpdateAvailable = postsFromResponse.value(forKey: "update_available") as! Bool;
                            }
                        }
                    }else{
                        print("Error: No records found")                 
                    }
            }) { (operation,error) in
                self.handleError(1  ,  message: NSString(), error: error! as NSError );
            }
        }
    }
     
    I Hop you Enjoy this tutorial keep touch in this blog for more update 

    Comments

    Popular posts from this blog

    Windows Keys

    Important extensions while create a new App in swift

    S.O.L.I.D. Principles with Example (Swift & Dart)