2012-03-25 59 views
1

我有一个应用程序可以产生祷告时间(一天5次),我想为5次祷告创建通知,但问题是每天根据一些计算来改变时间。不同时间的本地通知重复

编辑:

的计算是基于GPS的位置,这样当用户切换到另一个城市的时间将相应地更新完成。我将日期,时区,GPS坐标输入到一个方法中,并且获得给定日期/位置的(HH:mm)格式的祈祷时间值。现在我需要设置Notfications。我不确定在哪里设置它们。

这里是代码

#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 
+0

显示一些代码(即计算) – keyser 2012-03-25 17:20:38

+0

只是添加5个通知 – NeverBe 2012-03-25 17:22:17

+0

问题是如何计算时间,或如何安排'UILocalNotification'? – 2012-03-25 17:36:22

回答

2

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

可以在后台运行一个GPS应用程序,虽然这将是相当耗费电池只是为了设置一些定时器。 (这个背景过程实际上是为GPS跟踪器应用程序设计的,我不确定苹果公司使用它的目的略有不同。)

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

+0

问题是,这些时间并不是最终结果,例如有一天它将是下一个4:32的下午4点30分以后一个月它会增加更多,然后在一年中的某个时刻,它会减少它与日出日落和阳光的位置有关,因此日常变化。 – M7ammed 2012-03-26 15:23:20

+0

我把你介绍给我的最后一段。在iOS中,无法在后台运行任意应用程序。或者,您可以考虑推送通知(即从服务器),但是您会使用GPS中的更改来告诉服务器手机在哪里。当然,这可能与隐私有关。 – 2012-03-26 15:35:32

+0

好的,所以我会更新它的超时应用程序启动,所以我应该在视图中设置通知负载?如何确保每次用户进入应用程序时都不要设置通知?不会有通知已经设置? – M7ammed 2012-03-26 15:49:51