1

我有一个存储在NSDictionary.

对于下面的示例,我的计划时间为20120 年 1 月 13 日 2:00PM2012 年 1 月 13 日 2:05PM。如何将这两个添加到队列中以自行触发?

构建计划缓存:

-(void) buildScheduleCache
{  
    NSCalendarDate *now = [NSCalendarDate calendarDate];

    NSFileManager *manager = [[NSFileManager defaultManager] autorelease];
    path = @"/var/mobile/Library/MobileProfiles/Custom Profiles";
    theProfiles = [manager directoryContentsAtPath:path];

    myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6] retain];
    keys = [NSArray arrayWithObjects:@"Profile",@"MPSYear",@"MPSMonth",@"MPSDay",@"MPSHour",@"MPSMinute",nil];

    for (NSString *profile in theProfiles) 
    {
        plistDict = [[[NSMutableDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",path,profile]] autorelease];

        [myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:
                                  [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"%@",profile], 
                                   [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"MPSYear"]], 
                                   [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"MPSMonth"]], 
                                   [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"MPSDay"]], 
                                   [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"MPSHour"]], 
                                   [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"MPSMinute"]],
                                   nil]forKeys:keys]];

        profileSched =
        [NSCalendarDate dateWithYear:[plistDict objectForKey:@"MPSYear"]
                               month:[plistDict objectForKey:@"MPSMonth"]
                                 day:[plistDict objectForKey:@"MPSDay"]
                                hour:[plistDict objectForKey:@"MPSHour"]
                              minute:[plistDict objectForKey:@"MPSMinute"]
                              second:01
                            timeZone:[now timeZone]];

        [self rescheduleTimer];
    }

    NSString *testPath = @"/var/mobile/Library/MobileProfiles/Schedules.plist";
    [myPrimaryinfo writeToFile:testPath atomically:YES];
}

安排活动:

-(void) scheduleProfiles
{
    NSFileManager *manager = [[NSFileManager defaultManager] autorelease];
    path = @"/var/mobile/Library/WrightsCS/MobileProfiles/Custom Profiles";
    theProfiles = [manager contentsOfDirectoryAtPath:path error:nil];

    for (NSString *profile in theProfiles) 
    {
        NSMutableDictionary * plistDict = [[[NSMutableDictionary alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",path,profile]] autorelease];

        profileSched =
        [NSCalendarDate dateWithYear:[[plistDict objectForKey:@"MPSYear"] intValue]
                               month:[[plistDict objectForKey:@"MPSMonth"] intValue]
                                 day:[[plistDict objectForKey:@"MPSDay"] intValue]
                                hour:[[plistDict objectForKey:@"MPSHour"] intValue]
                              minute:[[plistDict objectForKey:@"MPSMinute"] intValue]
                              second:01
                            timeZone:[NSTimeZone localTimeZone]];


            NSLog(@"DATE: %@      SCHEDULE: %@      PROFILE: %@",[NSDate date],profileSched,profile);
        if([NSDate date] < profileSched)
        {
            NSLog(@"IGNORING PROFILE: %@     WITH SCHEDULE: %@",profile,profileSched);
        }else{
            //Create the timer from the Cached Array
            schedTimer = [[NSTimer alloc] initWithFireDate:profileSched //[NSDate dateWithTimeIntervalSinceNow: 10]
                                                  interval:0.1f
                                                    target:self
                                                  selector:@selector(fireCustomProfile:)
                                                  userInfo:profile
                                                   repeats:NO];//[[plistDict objectForKey:@"MPSRepeat"] boolValue]];

            MLogString(@"Scheduling Profile: %@",profile);
            [[NSRunLoop currentRunLoop] addTimer:schedTimer forMode:NSDefaultRunLoopMode];
        }
    }
}

触发事件:

-(void)fireCustomProfile:(NSTimer *)timer
{   
    if([[NSDate date] earlierDate:[schedTimer fireDate]])
    {
        NSLog(@"Ignoring Profile: %@",[schedTimer userInfo]);
        return;
    }

    notify_post("com.wrightscs.MobileProfiles.setCustomProfile");
}

示例事件:

<array>
    <dict>
        <key>MPSDay</key>
        <string>13</string>
        <key>MPSHour</key>
        <string>21</string>
        <key>MPSMinute</key>
        <string>15</string>
        <key>MPSMonth</key>
        <string>1</string>
        <key>MPSYear</key>
        <string>2012</string>
        <key>Profile</key>
        <string>Event 1</string>
        <key>Repeat</key>
        <true/>
    </dict>
</array>
<array>
    <dict>
        <key>MPSDay</key>
        <string>13</string>
        <key>MPSHour</key>
        <string>21</string>
        <key>MPSMinute</key>
        <string>20</string>
        <key>MPSMonth</key>
        <string>1</string>
        <key>MPSYear</key>
        <string>2012</string>
        <key>Profile</key>
        <string>Event 2</string>
        <key>Repeat</key>
        <true/>
    </dict>
</array>
4

3 回答 3

4

你确定计时器是你想要的吗?请记住,计时器仅在您的应用程序处于活动状态时才处于活动状态。当应用程序处于非活动状态时,它不起作用。如果你想让你的应用程序在遥远的将来随时做某事,计时器并不能真正解决你的问题,因为每次你退出应用程序时,它的所有计时器都会死掉。

假设(1)您确实只想在应用程序处于活动状态时为事件设置计时器,并且(2)队列中的事件数量总是未知,那么您最大的问题将是如何定位任意数量的事件任意数量的事件的计时器。

幸运的是,计时器可以在其属性中传递任意对象,userInfo因此您希望将每个事件包装在一个对象中并将该对象传递给计时器。然后计时器触发的方法可以提取事件并对其采取行动。

像这样的东西:

// assume a data model with event objects with the attributes and methods shown

- (void) setTimerForEvent:(EventClass *) anEvent{
    [NSTimer timerWithTimeInterval:[anEvent eventTimeFromNow] 
                            target:self 
                          selector:@selector(fireEvent:) 
                          userInfo:anEvent 
                           repeats:NO];

}//------------------------------------setTimerForEvent:------------------------------------

- (void)fireEvent:(NSTimer*)theTimer{
    [self handleEvent:(EventClass *)theTimer.userInfo]; 
    [theTimer invalidate];
}//------------------------------------fireEvent:------------------------------------

-[EventClass eventTimeFromNow]应该返回调用方法和事件时间之间剩余秒数的 NSTimerInterval 。

于 2010-04-16T17:44:55.203 回答
3

您可以使用 UILocalNotification 而不是 NSTimer。限制是没有您的交互,您的应用程序中没有任何操作,但即使应用程序关闭也会触发通知。

关于UILocalNotifications 的Apple 文档:

UILocalNotification 的实例表示应用程序可以安排在特定日期和时间向其用户呈现的通知。操作系统负责在适当的时间传递通知;应用程序不一定要运行才能发生这种情况。

...

当系统传递本地通知时,可能会发生几件事,具体取决于应用程序状态和通知类型。如果应用程序不在最前面且不可见,则系统会显示警报消息、标记应用程序并播放声音(通知中指定的任何内容)。如果通知是警报并且用户点击操作按钮(或者,如果设备被锁定,则拖动打开操作滑块),则启动应用程序。在 application:didFinishLaunchingWithOptions: 方法中,应用程序委托可以使用 UIApplicationLaunchOptionsLocalNotificationKey 键从传入的选项字典中获取 UILocalNotification 对象。委托可以检查通知的属性,如果通知在其 userInfo 字典中包含自定义数据,它可以访问该数据并进行相应的处理。另一方面,如果本地通知仅标记应用程序图标,并且用户响应启动应用程序,则调用 application:didFinishLaunchingWithOptions: 方法,但选项字典中不包含 UILocalNotification 对象。

如果在系统传递通知时应用程序处于最前面且可见,则不会显示警报,不会标记图标,也不会播放声音。但是,如果应用程序委托实现了 application:didReceiveLocalNotification: ,则会调用它。UILocalNotification 实例被传递到此方法中,并且委托可以检查其属性或访问 userInfo 字典中的任何自定义数据。

你可以在这里找到一个很好的教程,主要思想是这样的:

通知设置:

Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
    UILocalNotification *notif = [[cls alloc] init];
    notif.fireDate = [datePicker date];
    notif.timeZone = [NSTimeZone defaultTimeZone];

    notif.alertBody = TEXT;
    notif.alertAction = LAUNCH_BUTTON;
    notif.soundName = UILocalNotificationDefaultSoundName;
    notif.applicationIconBadgeNumber = NOTIFICATIONS_REMAINING;

    NSDictionary *userDict = [NSDictionary dictionaryWithObject:CUSTOM_INFO 
                                            forKey:kRemindMeNotificationDataKey];
    notif.userInfo = userDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:notif];
    [notif release];
}

在应用程序未运行时处理通知(在应用程序委托中):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls) {
        UILocalNotification *notification = [launchOptions objectForKey:
                        UIApplicationLaunchOptionsLocalNotificationKey];

        if (notification) {
            NSString *reminderText = [notification.userInfo 
                        objectForKey:kRemindMeNotificationDataKey];
           [viewController showReminder:reminderText];
        }
    }

    application.applicationIconBadgeNumber = NOTIFICATIONS_REMAINING_LESS_ONE;

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

前台应用程序(在应用程序委托中):

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateInactive) {
        // Application was in the background when notification
        // was delivered.
    }
}
于 2012-01-19T01:08:37.317 回答
0

每个事件都需要一个计时器。也许不要将计时器分配给timer变量。您可以创建这些计时器并在theFireEvent. 您需要将该选择器更改为@selector(theFireEvent)虽然,并确保该方法具有签名:

- (void)timerFireMethod:(NSTimer*)theTimer

于 2010-04-13T20:55:22.040 回答