始终通过将当前时间与目标 c ios 中的数组匹配来触发通知

Posted

技术标签:

【中文标题】始终通过将当前时间与目标 c ios 中的数组匹配来触发通知【英文标题】:Always trigger a notification by matching the current time to that in array in objective c ios 【发布时间】:2018-04-05 07:00:20 【问题描述】:

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

这是我到目前为止所做的:

   #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

【问题讨论】:

为什么将[NSDate date]TodayTimeNoteArray 中的那些时间进行比较?只需循环TodayTimeNoteArray中的值并设置触发时间? @Tj3n 我怎么知道什么时候触发下一个,所以我正在做请给出一个答案,因为我想提前谢谢 我也收到了这些类型转换警告,正在研究但找不到任何帮助 【参考方案1】:

根据您的逻辑,我不确定您要做什么,所以我不会谈论这个。要修复警告,只需将您的代码更改如下,然后您将在打印时获得正确的日期:

if ([TodayTimeNoteArray containsObject: todaysjustcurrentime])


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

【讨论】:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 0 beyond bounds for empty array' 打印所有数组并确保它们不为零 数组没有被打印但是我可以打印它的下标 [0] 或 1 2 3 现在一切正常数组有计时数组不是零应用程序没有崩溃但通知不会触发

以上是关于始终通过将当前时间与目标 c ios 中的数组匹配来触发通知的主要内容,如果未能解决你的问题,请参考以下文章

SimpleSAML_Error_Error: UNHANDLEDEXCEPTION -- 响应中的目标与当前 URL 不匹配

将 MySQL 内爆数组中的项目与查询中的 PHP 数组匹配

仅在与当前用户数据匹配时才获取用户数据

使用 url iOS 目标 c 附加数组

如何在iOS(目标c)中发送带有字典和数组的JSON?

javascript - 将字符串与正则表达式数组匹配