How to register for an event that reminds app on specified time in iOS

前端 未结 4 1485
-上瘾入骨i
-上瘾入骨i 2021-01-19 05:03

I have requirement to initiate sync on every night at 1 AM or every 2 weeks. How will I achieve this in iOS? Is there a way in iOS that my application can say remind me at t

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-19 05:39

    You can use NSLocalNotification. See an example (taken from here):

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    // Activate in 5 segundos
    notification.fireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:5];
    
    // message to show 
    notification.alertBody = self.nameTextField.text;
    
    // default sound
    notification.soundName = UILocalNotificationDefaultSoundName;
    
    // button title
    notification.alertAction = @"Ahora te lo cuento";
    notification.hasAction = YES;
    
    // activa la notificación
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    

    The point is that the behaviour depends on the application state when the local notification fires. If the app is running on the foreground, you can handle the notification silently. But if the app is not running, or it's in the background, user interaction is required.

提交回复
热议问题