Sunday, 2 April 2017

Connection API Call

Step 1:
    var plistconnection:NSURLConnection=NSURLConnection()
    var plistdata:NSMutableData=NSMutableData()

Step 2:
    HUD.hidden=false
            HUD.showAnimated(true)
           
            let loginString = AUTHSTR
            let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
            let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
           
           
            let url = NSURL(string: "\(APISTR)api/api/favourite/postdata/format/json")
            let request1 = NSMutableURLRequest(URL: url!)
            request1.HTTPMethod = "POST"
           
            let bodyData = "user_id=\(userid)&event_id=\(eventid)&is_fav=1"
            request1.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
            request1.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
           
            plistconnection = NSURLConnection(request: request1, delegate: self)!
            plistconnection.start()

Step 3:

/***************************************************/
    // MARK: Connection Method
    /*****************************************************/
    func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
       
        if connection == plistconnection {
           
            plistdata.appendData(data)
           
        }
     
       
    }
    func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
    {
        // Received a new request, clear out the data object
       
       
        if connection == plistconnection {
           
           plistdata.length=0
           
        }
     
       
    }
    func connection(connection: NSURLConnection, didFailWithError connectionData: NSError) {
        HUD.hidden=true
        showAlert(message: "Please check your internet connection")
        return
    }
   
    func connectionDidFinishLoading(connection: NSURLConnection!)
       
    {
       
        if connection == plistconnection
        {
           
            HUD.hidden=true
//            let strdata = String(data: plistdata, encoding: NSUTF8StringEncoding)
//            print(strdata)
            do {
                if let jsonResult = try NSJSONSerialization.JSONObjectWithData(plistdata, options: []) as? NSDictionary {
                    print(jsonResult)
                   
                    let dic = jsonResult as! NSMutableDictionary
                   
                    print(dic)
                   
                    let strsuccess = dic.valueForKey("status") as! Int
                   
                    if strsuccess == 1 {
                       
                       
                        pageno=dic.valueForKey("offset") as! Int
                        let temparr=dic.valueForKey("event_list") as! NSArray
                        let  pagingAry=temparr.mutableCopy() as! NSMutableArray
                        for i in 0 ..< pagingAry.count
                        {
                            popdetailarr.addObject(pagingAry.objectAtIndex(i))
                        }
                        print(popdetailarr)
                        tab.reloadData()
                        tab.hidden=false
                       
                        if popdetailarr.count < 3
                        {
                            self.tab.tableFooterView = UIView(frame: CGRectZero)
                        }
                       
                    }
                    else
                    {
                         self.tab.tableFooterView = UIView(frame: CGRectZero)
//                        let msg = dic.valueForKey("message") as! String
//                        showAlert(message: msg)
                       
                    }
                   
                }
            }catch let error as NSError {
                print(error.localizedDescription)
            }
        }
   
       
    }