SBDatePicker Its posssible to set minimum date and maximum date in swift? - ios

I am new for developing the ios application, I have used SBPickerSelector in Cocapods framework for Selecting datePicker.
whether If possible to set the MinumDate and MaximumDate???

Try;
let picker: SBPickerSelector = SBPickerSelector.picker()
//Minimum Date
picker.datePickerView.minimumDate = NSDate(); // A date
//Maximum Date
picker.datePickerView.maximumDate = NSDate(); // An other date ;
//or picker.setMaximumDateAllowed(NSDate())

I do not understand what do you get from using SBDatePicker, it look like they simply wrap the original class UIDatePicker.
The problem of using this kind of wrappers is that they don't always have the full feature set of the original class and you count on their developers to keep up with Apple changes (which they don't).
If you simply use UIDatePicker you can set maximumDate and minimumDate, just like you asked.
Check out the UIDatePicker Class Reference
And here is an example of setting the minimum and maximum dates

Related

XCUITest, UIDatePicker, adjustToPickerWheelValue

I'm writing an XCUITest. I have a view with a UIDatePicker on it (NOT a regular picker).
I'm trying to set the date using adjustToPickerWheelValue.
This crashes with the following error:
caught "NSInvalidArgumentException", "-[XCUIElement(XCUIElementTypePickerWheel) adjustToPickerWheelValue:]_block_invoke can only be called with elements of type XCUIElementTypePickerWheel, not valid for DatePicker."
This implies of course that this method is not available for the UIDatePicker.
I've gone through the apple site and it states pretty clearly that it IS supported.
I can't find any examples of this used for a date picker.
It's possible of course that I'm just not setting it right (I'm not sure how one would pass a string date, for example).
This is the code I'm trying:
XCUIElement *DOB;
XCUIElementQuery *DOBList = app.datePickers;
DOB = [DOBList elementBoundByIndex:0];
NSLog(#"DOB = %#\n", [DOB description]);
[DOB adjustToPickerWheelValue:#"SOME DATE GOES HERE?"];
BTW - description is of a DatePicker.
Printing [DOB value] just returns an empty string.
Any ideas?
For using UIDatePickers, I do like that:
XCUIApplication().datePickers.firstMatch.pickerWheels["CurrentValue"].adjust(toPickerWheelValue: "NewValue")
You have to do that for each picker of the UIDatePicker.

Converting Firestore Timestamp to Date data type

Few days ago, I just updated my firebase pod to the latest version, and in my debugging area, I got message that said that I have to update from timestamp data type to Date (something like that, I forget actually).
and I also have to change the DB settings like below
let db = Firestore.firestore()
let settings = db.settings
settings.areTimestampsInSnapshotsEnabled = true
db.settings = settings
after add the code above, the error message in my debugging area in Xcode disappears.
As far as I remember, I also had to change the data type in my client from Timestamp to Date data type, I haven't changed this because the error message in my debugging area in Xcode have disappeared.
As a result, I get not correct Date in my app.
Could you please share again the conversion step from TimeStamp to Date ? because as far as I remember I had to do some steps to follow. I can't find it in the firebase documentation.
Thank you very much :)
This is the message from the debugger
The behavior for system Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
let db = Firestore.firestore()
let settings = db.settings
settings.areTimestampsInSnapshotsEnabled = true
db.settings = settings
With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example:
// old:
let date: Date = documentSnapshot.get("created_at") as! Date
// new:
let timestamp: Timestamp = documentSnapshot.get("created_at") as! Timestamp
let date: Date = timestamp.dateValue()
Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.

How to save information ( Swift application )

I am developing an iOS application which generate random inspirational quote every day. At the moment when I close the app, open it again and click on the button which generates the daily quote, it shows me a new one.
Can you help me, how can I save the same quote all over the day and when the day is over generate a new quote. I mean at 00:00 o'clock in the morning.
I want to keep 1 quote per day, not 1 quote for every time I open the app.
Okay, I have some time on my hands and I can provide you a small tutorial on how to save data to the user defaults.
You will want to save the date on which you create a quote right after you created it and then each time your app goes to foreground check against that. Obviously you want to only remember the day, not the hours, minutes and seconds. Here's a small function for that:
func makeCleanToday() -> NSDate {
let today = NSDate()
let comps = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: today)
return NSCalendar.currentCalendar().dateFromComponents(comps)!
}
Note that this forcibly unwraps the date on the last line, but this is fine here, since I do nothing that could lead to dateFromComponents returning nil.
The next two lines you will always call right after you created your quote:
let cleanToday = makeCleanToday()
NSUserDefaults.standardUserDefaults().setObject(cleanToday, forKey: "MyAppDateKey")
Obviously you should use a better key to identify this (I suggest defining a constant somewhere for this). This saves your date (only day, month and year) in the app's user defaults.
Next time your app goes into the foreground (use the app delegate for this), then do this here:
if let savedDate:NSDate = NSUserDefaults.standardUserDefaults().objectForKey("MyAppDateKey") as? NSDate {
let cleanToday = makeCleanToday()
if savedDate.earlierDate(cleanToday) == savedDate {
// create new sentence
NSUserDefaults.standardUserDefaults().setObject(cleanToday, forKey: "MyAppDateKey")
}
} else {
// create new sentence
NSUserDefaults.standardUserDefaults().setObject(makeCleanToday(), forKey: "MyAppDateKey")
}
Please note that I added the two NSUserDefaults.standardUserDefaults().setObject.... lines here just to illustrate what happens. You will probably have that code already in your quote creation method, like I told you above to do. Also, the else part of this is for when you run the app the first time. Then you don't have anything saved in the user defaults, so savedDateis nil and you have to create your quote as normal.
Lastly a general remark: Using NSCalendar is relatively expensive (though it is designed for this scenario), so you shouldn't use it, for example, when filling cells in a table or something (because that might negatively affect frame rate during scrolling). In this case it's perfectly fine (relatively being the keyword here), I just wanna let you know before you eventually advance with your coding to a point where you run into this. :) Same is true for date formatters.
Oh, and regarding the comment: No need to say sorry, I just wanted to let you know how things here on SO are and cushion the fall a bit when you see your question getting downvoted. I hope this could help you.

NSDate as function parameter in Swift

I'm facing an issue if I pass NSDate as a function parameter.
My code is:
self.PassDate(responseDate) ; // Response date value = 2015-05-15T00:00:00
func PassDate(date:NSDate) {
// Here ideally date value should be same as responseDate value.. but date is coming as 2015-05-14T07:00:00 PDT
}
Why passing NSDate is changing the timezone/values?
Nothing is changing. Different ways of viewing dates present them using different time zones. The underlying date is not changed. If you view the date in the debugger it (usually) shows it in UTC.
Try logging the date outside your function and inside using println() calls. You should see the same value in both places if you view it the same way each time.

MonoTouch DateTime.ToLocalTime does not update when time zone is changed

When I change the time zone on my computer (when testing on the simulator) or on the phone (when testing directly on the phone), DateTime.ToLocalTime() does not return updated results.
I register and successfully receive notification events for UIApplication.Notifications.ObserveSignificantTimeChange. In it I call TimeZoneInfo.ClearCachedData() which allows me to detect the time zone has in fact changed but this has no effect on ToLocalTime(). The DateTime I am converting is definitely of kind Utc (tried DateTime.UtcNow for example).
I tried calling CultureInfo.CurrentCulture.ClearCachedData() as suggested in this question: SetTimeZoneInformation does not update DateTime.Now for another .NET Application. That only causes the application to crash when the CurrentCulture is nulled.
Digging a bit deeper into the implementation of ToLocalTime(), it seems to use the object TimeZone.CurrentTimeZone. This object is reset only when the difference between DateTime.GetNow() and TimeZone.timezone_check. I suspect that TimeZone.timezone_check is not getting updated when TimeZoneInfo.ClearCachedData() is called.
Is there any way I can force ToLocalTime() to take into consideration the time zone change?
You can use DateTimeOffset for this. See also this question.
Edit - What I have working is this:
var dateTimeUtc = new System.DateTime(2013, 2, 4, 21, 30, 0,
System.DateTimeKind.Utc);
var dateTimeOffsetLocal = new System.DateTimeOffset(dateTimeUtc.ToLocalTime());
var formattedLocalTime = string.Format("{0:HH:mm}", dateTimeOffsetLocal);
On my phone my formattedLocalTime changes whenever I change my TimeZone on it, without clearing any cache. I do have to reload the view, but no other tricks.
To display/verify the local TimeZone I use:
var localTimeZone = System.TimeZoneInfo.Local.StandardName;
I hope this somehow points you in the right direction. I don't have any better than this "works on my machine(s)"-anwer.

Resources