Thursday, 27 April 2017

Alamofire

Step :1 Install Pod

pod 'AlamofireSwiftyJSON'

pod 'SwiftyStoreKit'

Step :2 Import
import Alamofire

Step: 3
import .pch file
uikit and other framwork

Step 4:

Api with parm ******

 let APISTRingURL: String = "api"
     
        let credentialData = "\(auth)".data(using: String.Encoding.utf8)!
        let base64Credentials = credentialData.base64EncodedString() //base64EncodedStringWithOptions([])
        let headers = ["Authorization": "Basic \(base64Credentials)"]
     
        let param = [
            "user_id":"\(selectlangid)"
        ]
     
        print(param)
     
        Alamofire.request(APISTRingURL, method: .post, parameters: param, headers: headers).validate().responseJSON { (response) in
            print(response.result.value ?? AnyObject.self)
         
            guard response.result.error == nil else {
                // got an error in getting the data, need to handle it
                print("error calling POST on /todos/1")
                print(response.result.error!)
                 self.showAlert(message: "Please chack internet connection.")
                return
            }
            // make sure we got some JSON since that's what we expect
            guard let json = response.result.value as? [String: Any] else {
                print("didn't get todo object as JSON from API")
                print("Error: \(response.result.error)")
                return
            }
         
            print(json)
            // get and print the title
            guard let status = json["status"] as? Int else {
                print("Could not get todo title from JSON")
                return
            }
         
         
         
            let responsesss = json as NSDictionary
         
            print(responsesss)
            self.HUD.isHidden=true
         
            if status == 1
            {
                var temparay:NSArray = NSArray()
                temparay =  responsesss.value(forKey: "category_list") as! NSArray
             
                   
            }
            else
            {
             
            }
        }

********************************************************************
without parem

 let newTodo: [String: Any] = [:] insted param
*******************************************************************
Image upload
**********************************
 HUD.isHidden=false
                HUD.show(animated: true)
               
                let APISTRingURL: String = "\(APISTR)signup/postdata/format/json"
               
                let credentialData = "\(AUTHSTR)".data(using: String.Encoding.utf8)!
                let base64Credentials = credentialData.base64EncodedString() //base64EncodedStringWithOptions([])
                let headers = ["Authorization": "Basic \(base64Credentials)"]
               
                //cat_id  subcat_id offset, tag_id, user_id
               
                let param = [
                    "user_name":fnameTF.text!,
                    "email":emailTF.text!,
                    "password":passTF.text!,
                    "phone":phoneTF.text!
                   
                ]
               
                print(param)
                Alamofire.upload(
                    multipartFormData: { MultipartFormData in
                       
                        for (key, value) in param {
                            MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                        }
                        let imageData = UIImagePNGRepresentation(self.profileIMG.image!)
                       
                        MultipartFormData.append(UIImageJPEGRepresentation(UIImage(data: imageData!)!, 1)!, withName: "user_photo", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
                       
                       
                       
                }, to: APISTRingURL,headers:headers) { (result) in
                   
                    switch result {
                    case .success(let upload, _, _):
                       
                        upload.responseJSON { response in
                            //print(response.result.value)
                           
                            self.HUD.isHidden=true
                            let dic=response.result.value as! NSDictionary
                            let status=dic.value(forKey: "status") as! Int
                            print(dic)
                           
                            if status == 1
                            {
                                let msg = dic.value(forKey: "message") as! String

                               self.navigationController!.popViewController(animated: true)
                               
                                let custdetaildic=dic.value(forKey: "user_detail") as! NSDictionary
                                print(custdetaildic)
                                let custdet=custdetaildic.mutableCopy() as! NSMutableDictionary
                                UserDefaults.standard.setValue(custdet, forKey: "smartstylelogindetail")
                               
                                let logid=custdet.value(forKey: "user_id") as! String
                                UserDefaults.standard.setValue(logid, forKey: "SSUserID")
                                UserDefaults.standard.set(true, forKey: "SSuserLogin")
                               
                                let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                                let viewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewControllersb") as! UINavigationController
                                UIApplication.shared.keyWindow?.rootViewController = viewController;
                                self.title=""
                                self.navigationController!.navigationBar.tintColor = UIColor.white
                               
                            }
                            else
                            {
                                let msg = dic.value(forKey: "message") as! String
                                self.showAlert(message: self.getvalue(message: msg))
 
                            }
                           
                        }
                       
                    case .failure( _): break
                       
                    }
                   
                   
                }
            }