构建一个可以触发重复本地通知的iPhone应用程序

时间:2012-04-08 19:43:32

标签: ios uilocalnotification

我正在为iPhone构建一个简单的小应用程序(我不是那么经验丰富)。它所要做的只是显示一个网站链接(是的,就是这样)。但我需要每周同时触发本地通知,提醒用户点击应用中的链接。现在因为我不是很有经验,我真的不知道从哪里开始。我搜索了一下,找到了如何重复本地通知:http://xebee.xebia.in/2011/04/13/local-notifications-in-iphone/。但我甚至不知道在哪里放这个代码?我是否创建了一个基于视图的应用程序,如果是这样,我将上述代码放入哪个方法。如果有人可以给我一个我可以做什么的概述,或者甚至只是给我一些东西来google(关键字等),我可以去并阅读它。这个应用程序是出于必要而构建的,而不是我自己的学习,所以我只需要完成它!任何指针赞赏!

2 个答案:

答案 0 :(得分:2)

使用此计划通知:

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    //setting the fire dat of the local notification. Put the date you want the notification to be sent
    localNotification.fireDate = [[[NSDate alloc] init] autorelease]; 

    //setting the time zone
    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    //setting the message to display
    localNotification.alertBody = @"Notification Body";

    //default notification sound
    localNotification.soundName = UILocalNotificationDefaultSoundName;


    localNotification.alertAction = @"Action!";

    //Saving regionIndex and searchIndex
    NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:@"value", @"key", nil];
    localNotification.userInfo = userInfo;
    [userInfo release];

    //schedule a notification at its specified time with the help of the app delegate
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];    
    [localNotification release];

然后,每次用户加载应用程序时,您都可以使用scheduledLocalNotifications来检查下周的通知是否已经安排。

    NSArray *notifications = [NSArray arrayWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]];
    for (UILocalNotification *notification in notifications) {
        NSDictionary *userInfo = notification.userInfo;
        NSdate *date = notification.fireDate;

        // Here is where you can check if the notification was already scheduled    

    }

答案 1 :(得分:0)

单个视图模板可能是最好的开始。本地通知非常容易使用,首先要了解它们的工作方式以及如何使用它们。您布置的场景听起来适合本地通知。首先看Apple's documentation。有很多例子说明了这一点。