Update calendar 'start of week' from phones system calendar - ios

A user is able to set their custom start day of the week in their phone like this. Coupled with this different cultures have different days to start the week:
Australia, UK: Sunday, Monday, ..., Saturday
China: Monday, Tuesday, ..., Sunday
This means that when developing a calendar functionality we might have many different days of the week to start our calendar from (left to right) to make the user experience optimal.
We can customise a calendar to have a specific starting day of the week:
var customCalendar = Calendar(identifier: .gregorian)
customCalendar.firstWeekday = 3
The problem comes with updating my custom calendar to the user's system calendar.
Does this happen automatically when I use Calender (pulling from the user's phone) or do I need to do something custom?
Currently there is very little information that explicitly says whether something manual needs to be done to account for this. Thanks for any help anyone can give.

If you use Calendar.current and don't explicitly change its locale or its firstWeekday property then the calendar's firstWeekday property will automatically be appropriate for the user's device settings.
Code your logic based on that property value and you will be showing the correct results (assuming no bugs in your code of course).

Related

how to create DateAndTime picker in swift 4 which so that user is able to select date and time both

I have implemented the iOS date and time picker is my project like this
but the problem here is that is user want to select year and month then there is no option. how to solve this I can not find any library.
I want a date and time picker from which user can select date year, month,day and time hh:mm ?
You could use the UIPicker class to implement your own date an time picker. It would have year, month, day, hour, minute, and possibly AM/PM sections. You'd need to build some intelligence around changing the available days depending on the month (different months have different numbers of days, and on leap years February has a different number of days than on other years.)

NSCalendar firstWeekday always 1 regardless of user preference

I am attempting to get the firstWeekday as set in the user's system preferences, using [[NSCalendar currentCalendar] firstWeekday], however the value returned is always 1 (Sunday) regardless of what is set in the system preferences. Also tried the autoupdatingCurrentCalendar with the same results.
From the docs, it states [NSCalendar currentCalendar] returns a calendar formed from the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified in System Preferences. From that description it sounds like I should be able to access the firstWeekday set in the user settings.
Anyone have any ideas as to why this is not returning the correct firstWeekday?
Here's the deal…
Changing the first weekday in Settings > Calendar > First Weekday ONLY adjusts the day weeks start on in Apple's Calendar app. It does NOT adjust underlying NSCalendar objects. To do that you'll have to change your phone's region under Settings > General > Date & Time > Language & Region > Region to something like Germany to test a calendar with the start of the week as Monday, or US for Sunday, etc.
(To anyone setting out to work on calendar based code: thank you for caring about localization and also here be dragons. Good luck!)

Best way to store the day of the week using NSDate and NSDateFormatter

I'm trying to reproduce some of the functionality of the default Clock where I let users select a repeat frequency for an alarm. The problem is that different NSCalendar settings will give you different names for the days of the week. How do I store the selected days of the week in such a way that if the user changes their calendar the frequency always falls on the right day of the week?
One solution would be to check if the calendar has changed every time the app comes into foreground, and if so, make appropriate changes from there. Probably not the most elegant solution, nor the best practice, but it could get the job done.
Take (long) [dateYouWantToRemember timeIntervalSince1970] and store that in a property file or some such. (You could use timeIntervalSinceReferenceDate, but the 1970 number is the "UNIX epoch date" and more standardized -- you can find converters online to check it.)
Later, under the (potentially) different calendar, do [NSDate dateWithTimeIntervalSince1970:theLongYouStored] and you will reconstruct the date, in the new calendar.
Now use NSCalendar/NSDateComponents to find out the new ordinal day of week of that date, and use [NSDateFormatter weekdaySymbols] to fetch a list of the weekday names. Index the list with the ordinal to fetch the new day name. Use NSDateComponents to do arithmetic on you dates to select the next date that is that day of the week, as needed for your app.

Can Year Constraint removed from the native DatePicker in ios

I am Building an app and I want DatePicker to only Show Month and Day, but not the year. Since Native DatePicker shows Day, Month, Year. Can year be removed from the native DatePicker or is there any way by which we can let the DatePicker show only day and month and not year.Somebody's having any Suggestions? Thanks in advance.
Currently the only available date picker modes are
typedef NS_ENUM(NSInteger, UIDatePickerMode) {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer
};
So no, if you want to select an actual date the year component always shows. You could however, try to use a regular UIPickerView and update the amount of days in the first component whenever the month changes, although I would not recommend doing so.
What do you need the date picker without a year for, after all?

UIdatepicker showing weekdays

I would like to make a datepicker that shows weekdays, date and month.
I got a working datepicker showing month, date and year but I would really like it to show month, day, date
How is this possible? I know I might have to make a custom UIpicker, but how to I make this refer to dates?
You need to create your own picker with a UIPickerView. Create an NSDateFormatter. You can then access the month names and the weekday names from the date formatter. An NSCalendar can be used to obtain the maximum number of days for a month.
The real trick is updating the picker view components as the user selects a value from one of the components. For example, if the user picks a month, you want to update the number of days shown in the day component (or, like a UIDatePicker), grey out invalid days. This is complicated by the fact that you don't have a year so there is no way to properly handle February.
And what do you do when a user picks a given weekday? How should this affect the selected day? Just things to consider.

Resources