Reputation: 9148
I have just received a crash log from Apple after my update being rejected.
After reviewing the crash log I have found the offending code, which was related to parsing a date.
When I run this app on my device I do not get this error. I have a feeling that it is an issue with the Local of the device that Apple is testing on.
During my run, the original
string below resolved to 2012-12-04T11:02:29.5600000+0000
. I'm just wondering what other ways it could appear if in a different Locale.
More importantly, how can in test my app in the same environment as Apple so I don't need to wait out the review process to test
Note: I inherited this code, so any suggestions on improving the date parsing function would be greatly appreciated, thanks.
The crash came from the call [original substringToIndex:range.location+3]
in the following function.
+(NSString*)convertDate:(NSDate*)date withFormatter:(NSDateFormatter*)dateFormatter{
NSString *original = [dateFormatter stringFromDate:date];
NSRange range = [original rangeOfString:@"+"];
NSString *substring = [original substringToIndex:range.location+3];
NSString *substring2 = [original substringFromIndex:range.location+3];
return [NSString stringWithFormat:@"%@%@%@",substring,@":",substring2];
}
The call to this function is made in the following context
NSDate *timestamp = [NSDate date];
NSString *dateString = [DateHelper convertDate:timestamp withFormatter:UTCDateFormatter];
Upvotes: 0
Views: 156
Reputation: 3905
NSDateFormatter will format your date using your device date format and timezone.
// Convert the date to current time zone
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = sourceGMTOffset - destinationGMTOffset;
NSDate *destinationDateHere1 = [[NSDate alloc] initWithTimeInterval:interval sinceDate:yourDate];
Upvotes: 0
Reputation: 750
It is entirely possible that saying +3 is causing there to be an out of bounds range exception. Have you tested that using the [NSDate date] will return the same format in other timezones?
Either way you should be checking two things at a minimum - then parse the method multiple date types to confirm that the app will still function:
that the range from the following line has a location:
NSRange range = [original rangeOfString:@"+"];
if (range.location != NSNotFound)
{
//do stuff here
}
That the range.location + the 3 you are putting in does not exceed the total string length
NSRange range = [original rangeOfString:@"+"];
if (range.location != NSNotFound)
{
if ((range.location + 3) <= original.length)
{
//you should also check that ((range.location + 3) + range.length) <= original.length as well
//do stuff here
}
}
while the above does not really solve the issue of potentially displaying incorrect dates/timestamps - it should prevent the application from attempting to seek a range or index out of the bounds of the string length
Hope this helps,
Upvotes: 1