2012-03-25 30 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