How to detect month/year change - ios

I am using the UIApplicationSignificantTimeChangeNotification to detect when the day changes:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "dayChanged:", name: UIApplicationSignificantTimeChangeNotification, object: nil)
#objc func dayChanged(notification: NSNotification){
}
Inside the dayChanged handler I want to test if the new day is the first day of a new month or first day of the new year.
I should test if NSCalendar.currentCalendar().components(NSCalendarUnit.Day, fromDate: NSDate()).day is equal to 1, but how to differentiate between month and year ?

Just check if month is first - then you have new year, if not then you have new month.

Related

How to update the dynamic island view after a certain interval of time in App kill state.?

I want to start the dynamic island live activity for count down timer with event title.
Like christmas in 05 days.
I am trying to update it with the help of timer in extension, but not able to update the view.
Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [self] time in
if minute < 1 {
updateActivity(dynamicIslandType: dynamicIslandType, minute: 0)
time.invalidate()
}
I have tried this code in widget extension to update the view, but not able to get any update of the view.

How to run a notification on a specific day of the week, but every other week (Swift + Xcode)?

Basically, I need an app that runs a notification every other Monday. What I have is an app that runs a notification every Monday. Is there a way to set it up so that when the first notification is received the app will begin a two week time interval where it will run the notification again, and have it repeat? For example, if I had a variable called "week" and had it alternate between '1' and '2' every other week, how could I tag it to the notification's trigger?
func notification(hour: Int, minute: Int, weekday: Int, text: String){
let content = UNMutableNotificationContent()
content.title = "Example"
content.body = text
content.sound = UNNotificationSound.default()
let trigger = UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: hour, minute: minute, weekday: weekday), repeats: true)
let request = UNNotificationRequest(identifier: text, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
notification(hour: 12, minute: 0, weekday: 2, text: "Test")
}
You cannot do that for repeated notifications.
Only way to do this is to use non-repeated notifications and setup them on given time (in your case on Mondays) in advance - for example make a for-loop that will setup single notification on next 10 Mondays - you have to manually calculate trigger time for all of them in a loop.
Limitation is that you cannot make them infinitive - but probably if user will not react to those 10 notifications - there is a really small chance you use them later on - at least from my experience.
Best way to do this will be also to setup those notifications each time user closes the application - if we opens the app on 5th Monday for example, you should clean up all notifications and setup next set of 10 notifications again.

iOS 11- User local notification which repeat every x minutes

In iOS 11, how can I implement a local notification which repeats every x minutes?
The repeating interval will be selected from the user. So for example let's say that a user choose to set a notification which will trigger tomorrow at 9:00 AM and from there it triggers every 2 days (or two weeks or 6 months or 10 minutes)
var repeatInterval = Bool()
trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: repeatInterval)
//Schedule the Notification
let identifier = titleNospace
let request = UNNotificationRequest(identifier: identifier!, content: content, trigger: trigger)
self.center.add(request, withCompletionHandler: { (error) in
if let error = error {
print(error.localizedDescription)
}
})
UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler: nil)
With this code I can schedule a notification at a set date. I've been told that from here if I would like to schedule a repeat notification I should use a triggerInterval when the notification is delivered.
But how can I do so? How can I get the value of the repeating time (defined by the user) when the notification is delivered?
Shall I use this?:
func didReceive(_ notification: UNNotification)
I've tried but it seems that it's never called.
I'm new to swift and I've tried and tried but it seems I cannot find a solution.
I've been able to manage the repeating hourly, monthly, daily and yearly.
If I would like to add a custom repeat though I really wouldn't know how to do.
For your information you can't do customise repeat time interval like every 2 min, 10 min or Etc. you must use value of NSCalendarUnit like
How to set repeat frequency in User Notification

What function gets called when loading app from homescreen?

I want to be able to change the background color of my app depending on the time of day. I believe the way to go about this is saying if the hour component is greater than whatever number, set the background to the nighttime background, otherwise it's the daytime background.
To test out what I was worried about, I threw
timeLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .FullStyle)
into viewDidLoad. This shows the time that the app loads. This also obviously keeps updating if I kill the app and reload it completely.
However, if the user goes the the home screen or goes to a different app, then comes back to this the time isn't going to be updated. It will show the time the app was first loaded up. Only if the user completely kills the app, which obviously can't be relied on, will the correct time be shown.
So in my example if my "switch to nighttime time" was 5pm, if the user loads up at the at at 4:30 and then goes to the homescreen, loads up the app at 5pm, nothing will be changed as the stored time will still be 4:30.
I tried throwing the code in viewDidAppear and nothing changed.
What code is run when the app is loaded up from being on the homescreen or coming back from another app?
You want to key off of the UIApplicationDidBecomeActiveNotification event. Try this:
override func viewDidLoad()
{
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "applicationDidBecomeActive:",
name: UIApplicationDidBecomeActiveNotification,
object: nil)
}
func applicationDidBecomeActive(notification: NSNotification)
{
// do something when the app is active again.
timeLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .FullStyle)
}
The method -applicationWillEnterForeground: in your application delegate will be called every time a user enters your app.
You'll want to look at using NSCalendar for this:
let currentDate = NSDate() // You can input the custom as well
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: NSDate())
let currentHour = components.hour // You can play around with the ""components""
The following method is what you're after:
applicationDidBecomeActive: it's called every time a user opens the app.

jQuery UI DatePicker - Disable all days except last day of month

I'm trying to use the jquery UI datepicker to show a calendar with only the last day of the month selectable.
I've successfully used the beforeShowDay event to disable days of the week but not sure how I'd use this to disable everything but the last day of the month.
beforeShowDay is called for every date shown on the calender. When beforeShowDay is called, you need to determine if the date passed to it is the last day of the month.
The following JScript will return the last day of the month for a given year and month.
function LastDayOfMonth(Year, Month)
{
return(new Date((new Date(Year, Month+1,1))-1)).getDate();
}
Use the following code to determine if the beforeShowDay's date is the last of the month:
$('.selector').datepicker({
beforeShowDay: function (date) {
//getDate() returns the day (0-31)
if (date.getDate() == LastDayOfMonth(date.getFullYear(),date.getMonth())) {
return [true, ''];
}
return [false, ''];
}
});
function LastDayOfMonth(Year, Month) {
//set next month first day
//substract one day from it.
return new Date(new Date(Year, Month+1,1)-1).getDate();
}
This will work

Resources