0

I have a JSON request which gives prayer times. How can I use Objective-C to work out which is next? The JSON looks like this:

{
    address = "<null>";
    city = "<null>";
    country = UK;
    "country_code" = GB;
    daylight = 1;
    for = daily;
    items =     (
                {
            asr = "5:22 pm";
            "date_for" = "2013-7-1";
            dhuhr = "1:01 pm";
            fajr = "2:15 am";
            isha = "11:47 pm";
            maghrib = "9:24 pm";
            shurooq = "4:39 am";
        }
    );
    latitude = "50.9994081";
    link = "http://muslimsalat.com/UK";
    longitude = "0.5039011";
    "map_image" = "http://maps.google.com/maps/api/staticmap?center=50.9994081,0.5039011&sensor=false&zoom=13&size=300x300";
    "postal_code" = "<null>";
    "prayer_method_name" = "Muslim World League";
    "qibla_direction" = "119.26";
    query = "51.000000,0.500000";
    state = "<null>";
    timezone = 0;
    title = UK;
    "today_weather" =     {
        pressure = 1020;
        temperature = 14;
    };
}

My code so far is:

-(CLLocationCoordinate2D) getLocation{
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    return coordinate;
}

//class to convert JSON to NSData
- (IBAction)getDataFromJson:(id)sender {
    //get the coords:
    CLLocationCoordinate2D coordinate = [self getLocation];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

    NSLog(@"*dLatitude : %@", latitude);
    NSLog(@"*dLongitude : %@",longitude);

    //load in the times from the json
    NSString *myURLString = [NSString stringWithFormat:@"http://muslimsalat.com/%@,%@/daily/5.json", latitude, longitude];
    NSURL *url = [NSURL URLWithString:myURLString];
    NSData *jsonData = [NSData dataWithContentsOfURL:url];

    if(jsonData != nil)
    {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        NSArray *jsonArray = (NSArray *)result; //convert to an array
        if (error == nil)
            NSLog(@"%@", result);
            NSLog(@"%@", jsonArray);
            for (id element in jsonArray) {
                NSLog(@"Element asr: %@", [element objectForKey:@"asr"]);
        }
    }
}

How can I get the current time and determine which prayer comes next?

Thank you!

samiles
  • 3,768
  • 12
  • 44
  • 71

3 Answers3

1

Get a list of the dates, then use an NSPredicate to filter that list to dates >= [NSDate date], then sort it ascending. Then the first item in the filtered, sorted array will be the next date.

Wain
  • 118,658
  • 15
  • 128
  • 151
1

You need to get the 'items' dictionary using [result objectForKey:@"items"]. Then convert the string values to NSDate's using an NSDateFormatter. Then, iterate through the new dictionary and find the time with the smallest time interval to now using [currentDate timeIntervalSinceNow]. This is your next prayer.

Benjamin Mayo
  • 6,649
  • 2
  • 26
  • 25
  • Could that not also give the one which happened more recently? So, if the next one is 3 hours away but the previous one was 1 hour ago, wouldn't it give the previous one? – samiles Jul 04 '13 at 23:21
  • That method returns positive and negative numbers depending on whether the event is in the future or past. Looking for the smallest positive number will give you the next upcoming event. – Benjamin Mayo Jul 04 '13 at 23:47
0

Read Apple's Date and Time Programming Guide. You will find methods to convert you string times ("2:15am" etc.) into NSDate objects, methods to get the current date and time (e.g. [NSDate new] return the current date and time), and methods for comparing date/times so you can find the next one.

CRD
  • 52,522
  • 5
  • 70
  • 86