Combining custom actions with segues in table cell selections.

Triggering custom actions like open an URL with segues in storyboards is not quite obvious in the first attempt.
The trick is to drag a segue from the cell to the view controller icon on the bottom of the scene. This segue is pointing to the same scene as seen in the image below:

Screen Shot 2014-05-19 at 12.40.04

Now just set an identifier for the segue and trigger the correct action according to the identifier in the method -prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        // some other code
    } else if ([[segue identifier] isEqualToString:@"openLink"]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]];
    }
}

Parse JSON date from .net WebService in Objective-C

In my iOS class @FHNW I set the students a task for creating a CRUD application. The backend is an existing .net REST WebService with JSON. Unfourtanatly Microsoft has decided to create their own date formate instead of using a standard like RFC1123. Therefore the solution to display the date correctly or parsing an NSDate is not strait forward.

You can parse the date the the following snipped:

+ (NSDate *)mfDateFromDotNetJSONString:(NSString *)string {
    static NSRegularExpression *dateRegEx = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil];
    });
    NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];

    if (regexResult) {
        // milliseconds
        NSTimeInterval seconds = [[string substringWithRange:[regexResult rangeAtIndex:1]] doubleValue] / 1000.0;
        // timezone offset
        if ([regexResult rangeAtIndex:2].location != NSNotFound) {
            NSString *sign = [string substringWithRange:[regexResult rangeAtIndex:2]];
            // hours
            seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0;
            // minutes
            seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0;
        }

        return [NSDate dateWithTimeIntervalSince1970:seconds];
    }
    return nil;
}

This solution is copied from stack overflow.

To produce a .net WebService compatible date string again the solution is a little bit simpler. The important parts are caring about second/millisecond base and the time zones:

+ (NSString*)dotNetJSONStringFromDate:(NSDate*)date
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"ZZZ"];
    
    NSString* timezone = [formatter stringFromDate:date];
    
    int timezoneOffset = [timezone intValue] * 36;
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    
    unsigned long long time=((timeInterval - timezoneOffset) * 1000);
    NSString* dateTimeTosend= [NSString stringWithFormat:@"/Date(%lld%@)/", time, timezone];
    
    return dateTimeTosend;
}