首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不同时间的本地通知重复

不同时间的本地通知重复
EN

Stack Overflow用户
提问于 2012-03-26 01:18:53
回答 1查看 965关注 0票数 1

我有一个应用程序,生成祈祷时间(每天5次),我想为5个祈祷创建注释,但问题是,根据一些计算,时间每天都会改变。

编辑:

计算是基于GPS位置完成的,因此当用户更改到另一个城市时,时间会相应地更新。我将日期、时区、GPS坐标输入到一个方法中,并获得该给定日期/地点的(HH:mm)格式的祈祷时间值。现在我需要设置通知。我不知道该把它们放在哪里。

以下是代码

#import "PrayerTimeViewController.h"
#import "PrayTime.h"

@implementation PrayerTimeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UITabBarItem *tbi = [self tabBarItem];
        [tbi setTitle:NSLocalizedString(@"PrayerTimes", nil)];
        UIImage *i = [UIImage imageNamed:@"11-clock"];

        [tbi setImage:i];
        [i release];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{   

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Madinah"]];
    self.view.backgroundColor = background;
    [background release];

    locationManager = [[CLLocationManager alloc]init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager startUpdatingLocation];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
    if (t < -180) {
        return;
    }

    PrayTime *prayerTime = [[PrayTime alloc]init];
    [prayerTime setCalcMethod:0];
    [prayerTime setFajrAngle:16];
    [prayerTime setIshaAngle:14];
    [prayerTime setAsrMethod:0];



    NSDate *curentDate = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* compoNents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:curentDate];     
    CLLocationCoordinate2D currLoc = [newLocation coordinate];


    NSMutableArray *prayerCal = [prayerTime getDatePrayerTimes:[compoNents year]  andMonth:[compoNents month] andDay:[compoNents day] andLatitude:currLoc.latitude andLongitude:currLoc.longitude andtimeZone:[[NSTimeZone localTimeZone] secondsFromGMT]/3600];
    [prayerTime release];

    [fajer setText:[prayerCal objectAtIndex:0]];
//    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    NSString *time = [prayerCal objectAtIndex:0];
    NSString *dates = [NSString stringWithFormat:@"%d-%d-%d %@",[compoNents year],[compoNents month],[compoNents day],time];


    NSDateFormatter *dateText = [[NSDateFormatter alloc]init];
    [dateText setDateFormat:@"yyyy-MM-dd HH:mm"];
    [dateText setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:[[NSTimeZone localTimeZone] secondsFromGMT]]];

    NSLog(@"%@",[dateText dateFromString:dates]);

    [shrooq setText:[prayerCal objectAtIndex:1]];

    [duhur setText:[prayerCal objectAtIndex:2]];

    [aser setText:[prayerCal objectAtIndex:3]];

    [maghreb setText:[prayerCal objectAtIndex:5]];

    [isha setText:[prayerCal objectAtIndex:6]];

    [prayerCal release];

}



@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-26 22:51:15

您可以使用repeatInterval参数重复您的五个通知,使它们每天在同一时间出现。不幸的是,如果不运行你的应用程序,就无法调整时间。

你可以在后台运行GPS应用程序,不过仅仅为了设置一些计时器,这会消耗相当多的电池。(此后台进程实际上是为GPS跟踪器应用程序设计的。我不确定苹果会把它用在一个稍微不同的目的上。)

但最简单的方法是在应用程序启动时进行更新。当它启动时,您将获得当前的通知(使用UIApplicationscheduledLocalNotifications属性),如果它们不正确或过期,则取消它们并创建新的通知。每个通知都有一个字典有效负载,您可以使用它来更容易地识别您的警报。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9862261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档