Local Notification repetation at different times - ios

I have an app that generates prayer times (5 times a day) i want to create notfication for the 5 prayers but the issue is the times change everyday based on some calculations.
Edit:
The calculations are done based on GPS location so when the user changes to another city the times will be updated accordingly. I input the date,timezone, GPS coordinates into a method and I get prayer time values in (HH:mm) format for that given day/location. Now I need to setup the Notfications. I'm not sure where to set them up.
here is the code
#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

You can use the repeatInterval parameter to repeat your five notifications, making them appear at the same time every day. Unfortunately there's no way to adjust the time without running your app.
You can run a GPS app in the background, though that would be quite a drain on the battery just for setting some timers. (This background process is really designed for GPS tracker apps. I'm not sure what Apple would make of using it for a slightly different purpose.)
But the easiest way would be just to update when the app is launched. When it launches you would get the current notifications (using the scheduledLocalNotifications property of UIApplication), cancel them if they're incorrect or out of date and create new ones. Each notification has a dictionary payload that you can use to make it easier to identify your alarms.

Related

iOS Can not find the iBeacon again in fore ground

Hi~ I'm learning to use CLLocationManager to detect iBeacon. I read this article:
http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html
It says that startRangingBeaconsInRegion will make system scan beacon every second. I test and it's right.
But a problem happens if the program only execute startMonitoringForRegion without startRangingBeaconsInRegion.
My program can find the beacon first time I start a beacon hardware, and after I stop beacon the founction didExitRegion is called. But after I start the beacon second time, program cannot find it(execute didEnterRegion) at all. I have wait for 1 hour.
The Hardware I use for test are iPhone 5s with iOS 8.1.2 and radBeacon USB.
Here is my code.
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#interface ViewController () <CLLocationManagerDelegate>
#property (retain, nonatomic) IBOutlet UITextView *textView;
#property (strong, nonatomic) NSMutableString *myLog;
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) CLBeaconRegion *beaconRegion;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startBeaconMonitoring];
}
- (void)startBeaconMonitoring {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
[_locationManager performSelector:#selector(requestAlwaysAuthorization)];
[self userGeofencingPreferencesWereUpdatedWithNotification:nil];
[self updateLogWithString:#"start the app"];
}
- (void) userGeofencingPreferencesWereUpdatedWithNotification: (NSNotification *) notification
{
if (1) {
NSUUID *proximityUUID = [[NSUUID UUID] initWithUUIDString:#"EEF45689-BBE5-4FB6-9E80-41B78F6578E2"];
_beaconRegion = [[CLBeaconRegion alloc]
initWithProximityUUID:proximityUUID
identifier:#"1"];
_beaconRegion.notifyEntryStateOnDisplay = YES;
[_locationManager startMonitoringForRegion:_beaconRegion];
//[_locationManager startRangingBeaconsInRegion:beaconRegion];
//[_locationManager stopUpdatingLocation];
}
}
- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self updateLogWithString:#"enter"];
NSLog(#"enter");
}
- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self updateLogWithString:#"exit"];
NSLog(#"exit");
}
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(CLBeaconRegion *)region {
//NSLog(#"range");
}
- (void)dealloc {
[_textView release];
[_myLog release];
[_locationManager release];
[_beaconRegion release];
[super dealloc];
}
- (NSMutableString *)myLog {
if (!_myLog) {
_myLog = [[NSMutableString alloc] init];
}
return _myLog;
}
- (void) updateLogWithString:(NSString*)newLog {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString * logWithTime = [NSString stringWithFormat:#"%#---%#\n",[dateFormatter stringFromDate:now], newLog];
[self.myLog appendString:logWithTime];
self.textView.text = self.myLog;
[dateFormatter release];
}
#end
I found something by accident and this solve my problem. When I use a iOS APP named Broadcaster to simulate a iBeacon, it triggers iBeacon detection APP very quickly which run on another iOS device no matter in foreground, background or screen off.
When I use Rad Beacon USB, still failure. I test all my 4 Rad Beacon USBs and same thing happens.
It seems that the BLE message sent by Rad Beacon USB is a little different from which simulated by iOS(I just tried the APP Broadcaster). And iOS8 treats them
different in some situations.
My Rad Beacon USBs are version 2.0.

Updating UILabel with NSTimer stops after switching UIViewControllers

I have two view controllers, both connected via Segue and using Storyboard.
In view controller 1 I have an NSTimer counting up and updating a UILabel.
When I switch to view controller 2 and back to 1 the uilabel is no longer updated.
Here is some code:
headerfile
NSString *timerTicksForCounter;
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateTimerLabel];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self updateTimerLabel];
}
- (void) startLastConUpdater
{
lastCTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
}
-(void) updateTimerLabel
{
NSLog(#"timer: %#", timerTicksForCounter);
if (timerTicksForCounter) {
NSLog(#"timer not null");
mainTimerLabel.text = timerTicksForCounter;
}
}
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:stopDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm:ss"];
timerTicksForCounter = [dateFormatter stringFromDate:timerDate];
[self updateTimerLabel];
}
What do you mean it's no longer updated ? Does this mean you lose what was displayed before switching or it doesn't update anymore. If it's not updating anymore it's because you don't start the timer in the appropriate method. You could do something like :
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self startLastConUpdater];
}
This should solve both issues I mentioned above.

Local push notification by GPS position

I have a simple question about iOS, GPS Location and push notification.
Is possible in iOS to send a local push notification when the device is near of a specific GPS position?
Following code for ViewController.h file
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>{
CLLocationManager *locationManager;
NSString* lastNotification;
}
#end
Following code for ViewController.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
lastNotification = #"";
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -User Actions
- (IBAction)startLocaingMe:(id)sender{
[self startLocationReporting];
}
#pragma mark - Location Metods
- (void)startLocationReporting {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;//or whatever class you have for managing location
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
[self showNotificationIfDistanceIs100:location];
}
}
// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"locationManager:%# didFailWithError:%#", manager, error);
}
- (CLLocationDistance)distanceBetweenTwoPoints:(CLLocation*) location1 andSecond:(CLLocation*) location2{
CLLocationDistance distance = [location1 distanceFromLocation:location2];
return distance;
}
-(CLLocation*)House{
CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.030064 longitude:72.546193];
return loc;
}
-(CLLocation*)passportOffice{
CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.032034 longitude:72.549999];
return loc;
}
-(CLLocation*)ldCollageBusStand{
CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.032515 longitude:72.549307];
return loc;
}
-(void)showNotificationIfDistanceIs100:(CLLocation*) location{
if ([self distanceBetweenTwoPoints:location andSecond:[self House]] <= 100) {
[self setLocalNotificaion:#"Your are 100 meter form House"];
}else if ([self distanceBetweenTwoPoints:location andSecond:[self passportOffice]] <= 100){
[self setLocalNotificaion:#"Your are 100 meter form Passport Office"];
}else if ([self distanceBetweenTwoPoints:location andSecond:[self ldCollageBusStand]] <= 100){
[self setLocalNotificaion:#"Your are 100 meter form Ld Collage Bus Stand"];
}
}
-(void)setLocalNotificaion:(NSString*)msg{
if ([lastNotification isEqualToString:msg] != YES) {
lastNotification = msg;
UILocalNotification *futureAlert;
futureAlert = [[UILocalNotification alloc] init];
[futureAlert setAlertBody:msg];
futureAlert.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
futureAlert.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:futureAlert];
}
}
#end
You can change location Long/Lati and getting the Localnotification
May this thinks lot helpful
-------EDITED---------
You can implement LocationManager instance and get notified about location updates in the following method:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// CLLocation object will be the content of the array locations
}
You can post UILocalNotification based on your requirements.

iOS 7 background location service stops

I'm building an iOS app that works in the background, and posts the location of the user to the server every 3 minutes (as that is the maximum background execution time on iOS 7).
However, there is one problem, the background service terminate at randomly times. So sometimes it can run for 2 hours in the background, sometimes for 7 hours, then 3 hours and randomly so forth.
The code below produces the error.
I have been able to detect when it will terminate, and that is when [UIApplication sharedApplication].backgroundTimeRemaining is below 10 seconds.
Can anyone point in some direction, or explain why it is terminating? My guess is that [self.locationManager startUpdatingLocation] is not 100 % safe? (and the method that makes [UIApplication sharedApplication].backgroundTimeRemaining unlimited?)
Here is the "normal" log I receive on my server every 3 minutes;
iPhone 5: 21:06:45 backgroundTimeRemaining before startUpdatingLocation: 11.014648
iPhone 5: 21:06:45 backgroundTimeRemaining after startUpdatingLocation: 999.000000
iPhone 5: 21:06:48 backgroundTimeRemaining before stopUpdatingLocation: 999.000000
iPhone 5: 21:06:48 backgroundTimeRemaining after stopUpdatingLocation: 999.000000
The code:
#import "BetaLocationController.h"
#import "AppDelegate.h"
#import <Parse/Parse.h>
#import "CommunicationController.h"
#interface BetaLocationController () <CLLocationManagerDelegate>
#property UIBackgroundTaskIdentifier bgTask;
#property (strong, nonatomic) CLLocationManager *locationManager;
#property BOOL locationStarted;
#property (strong, nonatomic) CLLocation *myLocation;
#end
#implementation BetaLocationController
- (id)init
{
self = [super init];
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
self.locationManager.activityType = CLActivityTypeOther;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
self.myLocation = location;
// NSLog(#"Location: %f, %f", location.coordinate.longitude, location.coordinate.latitude);
}
- (void)didEnterBackground:(NSNotification *) notification
{
[self runBackgroundTask:10];
}
-(void)runBackgroundTask: (int) time{
//check if application is in background mode
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
//create UIBackgroundTaskIdentifier and create tackground task, which starts after time
__block UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// [self runBackgroundTask:5];
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:#selector(startTrackingBg) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
}
-(void)startTrackingBg{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
localNotification.fireDate = now;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HHmm"];
int timeofDay = [[formatter stringFromDate:[NSDate date]] intValue];
localNotification.applicationIconBadgeNumber = timeofDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
//set default time
int time = 170;
//if locationManager is ON
if (self.locationStarted == TRUE ) {
//Here I can detect the error
if([UIApplication sharedApplication].backgroundTimeRemaining < 10){
[CommunicationController logToParse:[NSString stringWithFormat:#"%# : Detected error, trying to restart locationservice", [UIDevice currentDevice].name]];
}
//log
[CommunicationController logToParse:[NSString stringWithFormat:#"%#: %# bgTime before stopUpdatingLocation: %f", [UIDevice currentDevice].name, [NSDate date], [self getBackgroundTime]]];
[self.locationManager stopUpdatingLocation];
self.locationStarted = FALSE;
//log
[CommunicationController logToParse:[NSString stringWithFormat:#"%#: %# bgTime after stopUpdatingLocation: %f", [UIDevice currentDevice].name, [NSDate date], [self getBackgroundTime]]];
}else{
//start updating location
//log
[CommunicationController logToParse:[NSString stringWithFormat:#"%#: %# bgTime before startUpdatingLocation: %f", [UIDevice currentDevice].name, [NSDate date], [self getBackgroundTime]]];
[self.locationManager startUpdatingLocation];
self.locationStarted = TRUE;
//Time how long the application will update your location
time = 3;
//log
[CommunicationController logToParse:[NSString stringWithFormat:#"%#: %# bgTime after startUpdatingLocation: %f", [UIDevice currentDevice].name, [NSDate date], [self getBackgroundTime]]];
}
[self runBackgroundTask:time];
}
-(float)getBackgroundTime{
float bgTime = [[UIApplication sharedApplication] backgroundTimeRemaining];
//If bgtime is over 180, it returns unlimited. In iOS 7, only 3 minutes backgroundtime is available
return bgTime > 180 ? 999 : bgTime;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[CommunicationController logToParse:[NSString stringWithFormat:#"%#: %# LocationManagerDidFail %#: %f", [UIDevice currentDevice].name, [NSDate date], error, [[UIApplication sharedApplication] backgroundTimeRemaining]]];
NSLog(#"Error in locationManager. It Failed: %#", error);
}
-(void)dealloc {
[CommunicationController logToParse:[NSString stringWithFormat:#"%#: %# got dealloc'ed", [UIDevice currentDevice].name, [NSDate date]]];
NSLog(#"Dealloc in location manager is called");
}
#end
You can in general not be sure your app will be kept alive. A user might want to terminate it actively or the system might be in need of the resources your app is using, and therefore terminate it. I guess you are experiencing this problem due to resource constraints.
You might get around it by using the the Background fetch introduced in iOS7. The OS will at regular interval allow your app to start up again.
Try reading this excellent document describing the feature: http://devstreaming.apple.com/videos/wwdc/2013/204xex2xvpdncz9kdb17lmfooh/204/204.pdf
You can, however, not prevent the user from force your app to close.
You cannot start location services in the background in iOS7. That is why it is inconsistent. You need to start it while the app is in the foreground and leave it on.
Follow these steps to get the locations in the background mode:
Add a "UIBackgroundModes" to a string "locations" key in the info.plist file in your app
Edit/Add your code:
if(self.location==nil){
self.locationManager = [[CLLocationManager alloc] init];
}
[self.locationManager setDelegate:self];
if( [self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
self.locationManager.distanceFilter = 10.0f;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.pausesLocationUpdatesAutomatically=NO;
self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
[self.locationManager requestAlwaysAuthorization];
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
self.locationManager.allowsBackgroundLocationUpdates = YES;
}
[self.locationManager startUpdatingLocation];
App will move on suspended mode that's why you'll not get the location after that. Please check below URL. it'll help you to get location more then 18 min. even app is in suspended mode.
nice post related to background service

Xcode: Invalid parameter not satisfying

I keep running into a pretty frustrating error in Xcode after implementing a date picker. The error in the debugger is: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date"
I've been going through my code for hours now, and can't find the issue. It may be because I'm not checking for nil, there is no date the first time the app installs and launches, so that may be causing the crash. If it is, how do I check for nil in this code? I'm still very new at programming, any help would be much appreciated. Here is the code:
#import "DatePickerViewController.h"
#implementation DatePickerViewController
#synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm'/'dd'/'yyyy"];
NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"DatePickerViewController.selectedDate"];
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Tomorrow!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
return YES;
}
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:#"DatePickerViewController.selectedDate"];
[self.datePicker setDate:storedDate animated:NO];
}
- (IBAction)dateChanged:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:#"DatePickerViewController.selectedDate"];
}
You don't check if date is null, before using it, in ex.
(void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:#"DatePickerViewController.selectedDate"];
// add this check and set
if (storedDate == nil) {
storedDate = [NSDate date];
}
// ---
[self.datePicker setDate:storedDate animated:NO];
}
I've find a Debug way may help you debug where is the exception occurred and may help someone to debug with exception.
navigate to breakpoint
Click the add button, and choose exception Breakpoint
Add breakpoint and make exception type to Objective-C
Run the code
It will stop at the line which make crash happened!
Hope this help~

Resources