始终通过将当前时间与objective c ios中的数组匹配来触发通知 [英] Always trigger a notification by matching the current time to that in array in objective c ios

查看:63
本文介绍了始终通过将当前时间与objective c ios中的数组匹配来触发通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是目标 c 的新手,我正在尝试在当前时间匹配时触发通知名为 newarr 的数组的时间我在这个数组中添加了时间.我希望通知应该根据添加到数组中的时间自动触发.现在,每当当前时间与给定数组的时间匹配时,我就做了一个 if 语句,通知正在被触发.但 ap 工作正常,但只有一个通知被触发.请帮助我随后触发通知.

Hello guys I am new to objective c, I am trying to trigger a notification when the current time matches the time of an array named newarr i have added times in this array. I want the notifications should be triggered automatically based on the times added in array. Now I have made an if statement whenever the current time matches the time given an array the notification is being triggered. but the ap is working fine but the only one notification is being triggered. please help me to trigger the notification subsequently.

这是我目前所做的:

   #import "ViewController.h"

    @interface ViewController () {
    NSUserDefaults *defaults;
    NSString *todaysjustcurrentime;
    NSMutableString *todayjusthoursplit;
    NSMutableString *todayjustmintsplit;
    NSInteger days;
    }
   @end

   bool isGrantedNotificationAccess;
   @implementation ViewController

    - (void)viewDidLoad {
     [super viewDidLoad];
    isGrantedNotificationAccess = false;
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNAuthorizationOptions options = UNAuthorizationOptionAlert+UNAuthorizationOptionSound;
    [center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
    isGrantedNotificationAccess = granted;
    }];
    }

    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    }

    - (IBAction)notifyButton:(id)sender {
    if (isGrantedNotificationAccess) {
    NSLog(@"clicked");
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"Notify me";
    content.body = @"Its change of moment in your area";
    content.sound = [UNNotificationSound defaultSound];

    NSArray *newarr =  [[NSArray alloc] initWithObjects:@"18:39",@"18:40",@"18:38",nil];

    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
    // or @"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
    NSLog(@"here is new current date %@",[dateFormatter stringFromDate:[NSDate date]]);
    NSDate* now = [NSDate date];
    NSLog(@"here is now date new %@", now);

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"];
    NSDate *currentDate = [NSDate date];
    NSString *dateString = [formatter stringFromDate:currentDate];
    NSLog(@"here is the new new date %@", dateString);
    NSArray *todayjusttimearray = [dateString componentsSeparatedByString:@":"];
    NSLog(@"here is  today new new hour split %@", todayjusttimearray);
    todayjusthoursplit = todayjusttimearray[0];
    todayjustmintsplit = todayjusttimearray[1];
    NSLog(@"here is the  today new new hour splited new try %@", todayjusthoursplit);
    NSLog(@"here is the  today today new new mint splited nee try %@", todayjustmintsplit);


    NSDateComponents* date = [[NSDateComponents alloc] init];
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"dd-MM-yyyy"];
    NSDate *currentdatehere = [NSDate date];
    NSString *cureentdatestr = [f stringFromDate:currentdatehere];
    NSDate *startDate = [f dateFromString:cureentdatestr];

    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:startDate];
    days = [components day];

    date.day = days; //getting current day of the month
    date.hour = todayjusthoursplit.intValue;
    date.minute = todayjustmintsplit.intValue + 1;
    NSLog(@"here is setup date %@", date);

   if ([newarr containsObject: dateString])
    {
        NSLog(@"here is the current time you know ");
     }
   else {
        NSLog(@"here is the current time you dont know ");

   }

    NSLog(@"this is time right now %@", dateString);

    if ([newarr containsObject: dateString])

    {
        NSLog(@"Yes its time to do something");
        UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
                                                  triggerWithDateMatchingComponents:date repeats:YES];
        UNNotificationRequest* request = [UNNotificationRequest
                                          requestWithIdentifier:@"Notify me" content:content trigger:trigger];
        [center addNotificationRequest:request withCompletionHandler:nil];
    }
    else {

            NSLog(@"here is the current time does not match ");

    }
  }
  }

  @end

推荐答案

对于你的逻辑,我不确定你想要做什么,所以我不会谈论这个.要修复警告,只需按如下方式稍微更改您的代码,然后您将在打印时获得正确的日期:

For your logic, Im not sure what you are trying to do, so I wont talk about that. To fix the warning, simply change your code a little bit as below then you will get correct date when print:

if ([TodayTimeNoteArray containsObject: todaysjustcurrentime])
{

    date.day = days; //getting current day of the month
    date.hour = todayjusthoursplit.intValue;
    date.minute = todayjustmintsplit.intValue;

这篇关于始终通过将当前时间与objective c ios中的数组匹配来触发通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆