gpt4 book ai didi

ios - 不同时间的本地通知重复

转载 作者:行者123 更新时间:2023-11-29 11:15:02 25 4
gpt4 key购买 nike

我有一个生成祈祷时间(每天 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

最佳答案

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

可以在后台运行 GPS 应用程序,但仅用于设置一些计时器会非常耗电。 (这个后台进程实际上是为 GPS 跟踪器应用程序设计的。我不确定 Apple 会如何将它用于稍微不同的目的。)

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

关于ios - 不同时间的本地通知重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9862261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com