Trying to form an if statement which compares the current date on the left to the date chosen in UIDatePicker on the right, I think this is the right code I was given by a fellow Stack Overflow user, they shouldn't be able to perform this action, and they can't but I need to create a pop-up telling them why not:
//Get the current date
NSDate *pickerDate = [self.datePicker date];
//Unable to set notification for same day
//[datePicker setMinimumDate:[NSDate date]];
self.datePicker.date = [NSDate date];
if ([pickerDate compare:datePicker] == NSOrderedAscending) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Something wasn't right." message:#"That did not work and your alarm was not set." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
I am receiving the following error from implementing this:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDatePicker timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x10b9373a0'
Double check your call to the compare: method. You are comparing against the date picker instead of the date picker's date.
You want:
if ([pickerDate compare:datePicker.date] == NSOrderedAscending) {
Related
I am trying to write a small sample of cordova ios application. One of my requirements is to provide a button/link to allow user to crash the application.
I have tried to raise exception in CDVUIWebViewNavigationDelegate.m as follows,
- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
if([url.path containsString:#"CRASH"])
{
NSLog(#"User crash bookmart with NSException");
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
NSDate *current = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Set date and time styles
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *currentTime = [dateFormatter stringFromDate:current];
[userInfo setObject:#"Crash Time" forKey:currentTime];
NSException *ex = [[NSException alloc] initWithName:#"BookmartCrashException" reason:#"User crashed bookmart!" userInfo:userInfo];
[ex raise];
}
...
}
But when I tried, I saw following log,
2017-09-04 17:09:57.148 HRent[96124:12077045] User crash bookmart with NSException
2017-09-04 17:09:57.149 HRent[96124:12077045] *** WebKit discarded an uncaught exception in the >webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: User crashed bookmart!
The exception has been discarded and app hasn't crash : (
Is there any other way to crash the app for sure? Or with some configuration can I disable WebKit to discard such exception?
Much appreciate for your answers!
Regards
Rachel
Thanks Everyone.
I have tried with all the suggestion except for plugin suggested by Will.
Overall, there are 2 ways to crash the app.
As Michale suggested, use abort() to terminate the app.
Here is the piece of code I used,
- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:
(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
if([url.path containsString:#"CRASH"])
{
abort();
}
...
}
As shebuka's suggested, dispatch the exception on main thread. The trick here is that we can not use accessing nil array or dividing 0 to raise this exception but have to write I post in my question. Otherwise, the app won't crash and no log shown.
Here is the code piece I used,
- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:
(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
if([url.path containsString:#"CRASH"])
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"User crash bookmart with NSException");
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
NSDate *current = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Set date and time styles
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *currentTime = [dateFormatter stringFromDate:current];
[userInfo setObject:#"Crash Time" forKey:currentTime];
NSException *ex = [[NSException alloc] initWithName:#"BookmartCrashException" reason:#"User crashed bookmart!" userInfo:userInfo];
[ex raise];
});
} ...}
I am going to choose solution 2 cause this crashes app with an exception which fits my requirement better.
Thanks everyone.
Try to launch your exception with dispatch on the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
NSInteger asd = 5 / 0; // Simple division by 0
});
If it works then try NSException approach to have all that extra info.
Have you tried abort() as in the default core data implementation example? It causes the application to generate a crash log and terminate.
The hockeyapp plugin provides functionality that allows you to crash an app. You can use this as a temporary solution until you figure out the problem.
hockeyapp.forceCrash():void
Check out the repository https://github.com/Microsoft/cordova-plugin-hockeyapp
I am facing error , when alert box clicked. Error is incompatible array pointer with String .
2015-10-14 12:41:06.235 snadwitch2[1974:56154] -[__NSArrayI length]: unrecognized selector sent to instance 0x7f954e918e60
2015-10-14 12:41:06.239 snadwitch2[1974:56154] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7f954e918e60'
The alert box code is
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Wait" message:# [#"The current date and time is: %#", [NSDate date]] delegate:self cancelButtonTitle:#"Delete" otherButtonTitles:#"Cancel",nil];
[alert show];
You are passing an array instead of a string. You need:
NSString *message = [NSString stringWithFormat:#"The current date and time is: %#", [NSDate date]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Wait"
message:message
delegate:self
cancelButtonTitle:#"Delete"
otherButtonTitles:#"Cancel",nil];
[alert show];
I have this response from my webservice but UIAlertview is giving an strange error
I have printed the respnse and just below I called
2013-08-13 15:40:27.463 Ipad Qld[1459:907] Result {
msg = "Form has been sent successfully.";
status = SUCCESS;
}
2013-08-13 15:40:27.465 Ipad Qld[1459:907] -[__NSDictionaryM isEqualToString:]: unrecognized selector sent to instance 0x1f1168e0
2013-08-13 15:40:27.467 Ipad Qld[1459:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM isEqualToString:]: unrecognized selector sent to instance 0x1f1168e0'
*** First throw call stack:
(0x32dad2a3 0x3ac1197f 0x32db0e07 0x32daf531 0x32d06f68 0x34bb9375 0x34d05c95 0x34d05741 0x6a25b 0x32cfe037 0x336ae2cb 0xa013f 0x9fe35 0x8d1e7 0x336e86fd 0x336281f9 0x33628115 0x32a8a45f 0x32a89b43 0x32ab1fcb 0x32cf374d 0x32ab242b 0x32a1603d 0x32d82683 0x32d81f7f 0x32d80cb7 0x32cf3ebd 0x32cf3d49 0x368aa2eb 0x34c09301 0x43a85 0x3b048b20)
libc++abi.dylib: terminate called throwing an exception
the code is
- (void) contactUsNotificationReceived:(NSNotification *) notification
{
[[ActivityIndicator currentIndicator] hide];
NSDictionary *alertMessage = [notification userInfo];
NSString * alertMessageString = alertMessage[#"NSLocalizedString"];
NSLog(#"Result is waga %#",alertMessageString);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Result" message:alertMessageString delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
Looks like you are passing a NSDictionnary.
A guess, try to change :
NSString * alertMessage; // the object that you think is a NSString
to
NSDictionary * alertMessage;
NSString * alertMessageString = alertMessage[#"msg"];
Good luck.
Your code seems like problem in isEqualToString. The source string is not valid or it is released or its now pointing to Dictionary.
May be your alert string (that you want to describe) it is not NSString but it may be NSDictionary, so check it.
First get your alert message from NSDictionary to NSString...
NSString *alertMessage = [yourDict objectForKey:#"yourKey"];
Now, show your alert Message in Alert...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%#",alertMessage] message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
I hope this will help you.
I have an iPad app (XCode 4.6, Storyboards, iOS 6.2) with several classes. In one class, when I finish posting, I post a notification which I expect another class to catch. Not happening!
Here is the code to register the notifications, which I have in AppDelegate.m:
// add observers for notificaations
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(calendarTapNotification:)
name:#"calendarDateSelected" object:nil ];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(notificationOfAppointmentMade:)
name:#"appointmentMade" object:nil ];
Here is the code where I post a notification for one of the observers:
if(aDate != nil) { // if user tapped outside of valid date range, aDate will be nil
[passedData setObject:aDate forKey:#"selectedDate"];
// post notification CFGregorianDate has been tapped and pass selected date
[[NSNotificationCenter defaultCenter] postNotificationName:#"calendarDateSelected" object:self userInfo:passedData];
}
And this is the code where I handle the "calendarTapNotification" notification which never gets called:
- (void) calendarTapNotification:(NSNotification *) notification {
// was the calendar tapped?
if ([[notification name] isEqualToString:#"calendarDateSelected"]) {
// get the data in the dictiionary
NSDictionary *dict = [notification userInfo];
// NSLog(#"\ndict contents: %#", [dict objectForKey:#"selectedDate"]); // this is NSDate class!
// gets the year, month, and day for selected date
NSDate *selectedDate = [dict objectForKey:#"selectedDate"];
// NSLog(#"\n\ndict-selectedDate: %#", [dict objectForKey:#"selectedDate"]);
NSCalendar *calendar = [NSCalendar currentCalendar]; // get default calendar
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate: selectedDate];
// NSLog(#"\n\ndateFromComponents: %#", components);
// create start and end of date
NSDate *startDate = [self beginningOfDay: [calendar dateFromComponents:components]];
NSDate *endDate = [self endOfDay: [calendar dateFromComponents:components]];
// NSLog(#"\nstartDate: %# endDate: %#", startDate, endDate);
// create the predicate
NSPredicate *predicate = ([NSPredicate predicateWithFormat:#"((aStartTime > %#) AND (aStartTime <= %#))", startDate, endDate]);
// find all appointments with that selected date (AppointmentInfo is NSManagedContext; appointmentInfo is NSArray)
appointmentInfo = [AppointmentInfo MR_findAllWithPredicate:predicate];
SingletonAppointments *sharedInstance = [SingletonAppointments sharedInstance]; // initialize
// if(sharedInstance.globalApptList.count > 0) // <-------------- TODO
// [sharedInstance.globalApptList removeAllObjects]; //removeAllObjects]; // clear out the old stuff
if(appointmentInfo.count > 0) {
for (AppointmentInfo *appt in appointmentInfo) {
sharedInstance.globalApptList = appointmentInfo; // move data to sharedInstance
}
}
else {
sharedInstance.globalApptList = nil;
}
// NSLog(#"\n\nsharedInstance.count: %d", sharedInstance.globalApptList.count);
// re-draw the schedule
[self.subViewData setNeedsDisplay];
}
}
This is the output from the "crash":
2013-04-17 12:58:47.942 saori[11294:c07] -[AppDelegate calendarTapNotification:]: unrecognized selector sent to instance 0x8a25c60
2013-04-17 12:58:47.943 saori[11294:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate calendarTapNotification:]: unrecognized selector sent to instance 0x8a25c60'
* First throw call stack:
(0x2701012 0x19efe7e 0x278c4bd 0x26f0bbc 0x26f094e 0x14b04f9 0x275b0c5 0x26b5efa 0x13e4bb2 0x7de8 0x80eb 0x966cef 0x966f02 0x944d4a 0x936698 0x2b7edf9 0x2b7ead0 0x2676bf5 0x2676962 0x26a7bb6 0x26a6f44 0x26a6e1b 0x2b7d7e3 0x2b7d668 0x933ffc 0x297d 0x28a5 0x1)
libc++abi.dylib: terminate called throwing an exception
I have tried everything I know of, read the two Apple docs on NSNotifications and looked at Google and SO and found nothing)
Why doesn't it handler for notification get called? (which I suppose will lead to what is causing the crash).
Make sure you've implemented
calendarTapNotification:
notificationOfAppointmentMade:
in your AppDelegate.
If you have not implemented the calendarTapNotification: method then implement it, secondly make sure you are using the parameter for your method as you are using :.
When you are adding an observer for a notification then you should be able to listen to it, for listening to the notification, it should be posted. Make sure you are posting the calendarDateSelected notification in your code. I don't see anywhere you have posted.
I want to create a alert from a alert. but it crashes.
I use under code to create a alert:
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:inMessage delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
}
error message is :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSError isEqualToString:]: unrecognized selector sent to instance 0x88b84c0'
it looks like inMessage has been release. so I change inMessage to #"test test". It works but get out wait_fences: failed to receive reply:10004003
How can i do for creating a alert in a alert ?
As others have pointed out in the comments, the issue here is that you have an NSError that is being treated mistakenly as an NSString.
inMessage seems to be your NSError object. This slips through the type system at compile time because userInfo has no knowledge of the types of its elements.
To fix this, you probably want to get the description of the error message and display that instead with something like
NSString *inMessage = [[userInfo objectForKey:#"NSUnderlyingError"] localizedDescription];