0

How to update date in fscalender initially while launching an app. i am updating the date while scrolling fscalender but my doubt initially loading app how to send date to Post service Call?

This is my api for getting dates from server.

let now2 = NSDate() 
let dateFormatter2 = DateFormatter() 
dateFormatter2.dateFormat = "yyyy" 
let nameOfyear = dateFormatter2.string(from: now2 as Date)
print("nameOfyear",nameOfyear)
let urlString = Fetch_Parent_Dashboard_Calendar_URL+"&year="+nameOfyear+"&mo‌​nth="+nameOfMonth+"&‌​branch_id=" + Userbranchid!

i am taking date value from user defaults sending to server when user scrolling fscalender.Following is the code for sending date to server when scrolling fscalender.

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) 
{
   let dateFormatter2 = DateFormatter() 
   dateFormatter2.dateFormat = "dd/MM/yyyy" 
   let nameOfDate = dateFormatter2.string(from: date as Date)
   print("nameOfDate",nameOfDate) 
   UserDefaults.standard.set(nameOfDate, forKey: "nameOfDate")
}

My doubt is initially loading the app i have to send current date and scrolling time have to send that user defaults time how to handle date at this case

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
siddu
  • 11
  • 3
  • Can you be more specific? – Ravi Nov 21 '17 at 06:10
  • How to update date in fscalender. this is my api for getting dates from server.`let now2 = NSDate() let dateFormatter2 = DateFormatter() dateFormatter2.dateFormat = "yyyy" let nameOfyear = dateFormatter2.string(from: now2 as Date) print("nameOfyear",nameOfyear) let UrlStraing = Fetch_Parent_Dashboard_Calendar_URL+"&year="+nameOfyear+"&month="+nameOfMonth+"&branch_id=" + Userbranchid!`. here i am sending current date – siddu Nov 21 '17 at 06:36
  • i am taking date value from user defaults sending to server when user scrolling fscalender.this is my code for sending date to server when scrolling fscalender.` func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { let dateFormatter2 = DateFormatter() dateFormatter2.dateFormat = "dd/MM/yyyy" let nameOfDate = dateFormatter2.string(from: date as Date) print("nameOfDate",nameOfDate) UserDefaults.standard.set(nameOfDate, forKey: "nameOfDate") } – siddu Nov 21 '17 at 06:37
  • my doubt is initially loading the app i have to send current date and scrolling time have to send that user defaults time how to handle date at this case. – siddu Nov 21 '17 at 06:37

1 Answers1

0

If i understood correctly you have to do like this.

Make generic function to call web service with the given date

func callWebServiceWithDate(date:Date)
{
  let dateFormatter = DateFormatter() 
  dateFormatter.dateFormat = "yyyy" 
  let nameOfyear = dateFormatter.string(from: date)
  dateFormatter.dateFormat = "MM"
  let nameOfMonth = dateFormatter.string(from: date)
  let urlString = Fetch_Parent_Dashboard_Calendar_URL+"&year="+nameOfyear+"&mo‌​nth="+nameOfMonth+"&‌​branch_id=" + Userbranchid!
}

Now call this method from viewDidLoad or viewWillAppear method with Date() as input parameter for initial call.

override func viewDidLoad()
{
   super.viewDidLoad()
   self.callWebServiceWithDate(date: Date())
}

And call the same method from FSCalendars delegate method

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) 
{
   self.callWebServiceWithDate(date: date)
}
Ravi
  • 2,441
  • 1
  • 11
  • 30
  • Hi raki thank you for quick solution let me try this solution . – siddu Nov 21 '17 at 07:27
  • but i am calling Service function in another class and using that function with shared instance . so how can i Make generic function to call web service with the given date. – siddu Nov 21 '17 at 07:34
  • write the function in that class (Just copy from here and paste it there) – Ravi Nov 21 '17 at 09:44