Wednesday, August 22, 2018

iOS usefull code

1) Navigating view controller
    Let consider you want to navigate to Dashboard view controller use below code.


 let nextVC = storyboard?.instantiateViewController(withIdentifier: "Dashboard") as! Dashboard
 self.navigationController?.pushViewController(nextVC, animated: true)


2) Table view custom cell.

   

 let cell =  tableView.dequeueReusableCell(withIdentifier: "Cell") as! MyCustomCell

3) Chek Network Connectivity use MB

   Add below line to bridging header

#import "Reachability.h"
#import "MBProgressHUD.h"

  if Connectivity.isConnectedToInternet {
            print("Connected")
            
            let prog = MBProgressHUD.showAdded(to: self.view, animated: true)
            prog?.labelText = "Please wait..."
            
            
            DispatchQueue.main.async {
                self.CallYourAPI()
            }
        } else {
            print("No Internet")
            var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
            alert.show()
        }



4) Alamofir



 func CallYourAPI(totalSize : Int) -> Void {

        
        
        let parameters = [
            "para1":"",
            "para2":""
            ] as [String : Any]
        
        
        let headers = [
            "Token": TOKEN_KEY,
            "Content-Type": "application/x-www-form-urlencoded"
        ]
        
        let url = BASE_URL + "/getData"
        
        
        print("url => ",url)
        print("parameters => ",parameters)
        
        
        Alamofire.request(url, method:.post, parameters:parameters,encoding: JSONEncoding.default,headers: headers).responseJSON { response in
            print("Resposnse from server => ", response)
            switch response.result {
                
                
            case .success:
                print(response)
                guard let data = response.data else {return}
                let json = JSON(data : data)
                
                MBProgressHUD.hideAllHUDs(for: self.view, animated: true)
                
                
                if(json["code"] == 200)
                {
                    
                    let dataFromServer=json["data"]
                    print(dataFromServer);
                    
                    let hotelData = dataFromServer["hotels"].arrayObject
                    
                    
                    print("total size =>", hotelData?.count)
                    
                    if(hotelData != nil)
                    {
                        
                        if(hotelData!.count > 0)
                        {
                            
                            for receivedArray in hotelData as! [[String:Any]]
                            {
                                
                            
                                var para1 = receivedArray["para1"] as? Array<AnyObject>
                                var para1 = receivedArray["para1"] as? Array<AnyObject>
                                
                                var para1 = receivedArray["para1"] as! String
                                var para1 = receivedArray["para1"] as! String
                                var description = receivedArray["description"] as! String
                                
                                
                                
                              
                                
                                self.model += [Model(XYZ:"",
                                                  PQR:"",
                                                   ))
                                ]
                                
                                
                            }
                            
                            
                              self.caseStudyTableView.reloadData()
                            
                        }else{
                           
                        }
                        
                        
                        
                    }
                }else{
                    print("message ==> ",json["message"])
                    var refreshAlert = UIAlertController(title: "Message", message: json["message"].stringValue, preferredStyle: UIAlertControllerStyle.alert)
                    
                    //                refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
                    //                    print("Handle Ok logic here")
                    //                }))
                    
                    refreshAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { (action: UIAlertAction!) in
                        print("Handle Cancel Logic here")
                        print("Executed here")
                        
                    }))
                    
                    self.present(refreshAlert, animated: true, completion: nil)
                    
                }
                
                
            case .failure(let error):
                
                MBProgressHUD.hideAllHUDs(for: self.view, animated: true)
                
                print(response)
                print( response.data)
            }
        }
    }



1 comment:

  1. Casinos and Gambling | Microgaming, Microgaming, Playtech
    Microgaming has betmove built on their successful partnership with Playtech, 토큰룰렛 a 안전 바카라 leading software provider 포커페이스 to the world's leading 킹스포커 online gaming software industry.

    ReplyDelete