Search code examples
phpobjective-carraystitaniumnsdictionary

How to parse a NSDictionary Array with PHP


I'm building an app with Appcelerator Titanium. It has an photo upload function so I've build an PHP backend (CodeIgniter + RESTful Server) to process the uploaded files. Since Titanium removes the EXIF data from the photos, I'm using the myMedia module to obtain the EXIF data so I can still post the EXIF data to the PHP backend.

The array which the module returns looks something like this:

{
exif =     {
    ApertureValue = "2.970853567123413";
    BrightnessValue = "5.906054496765137";
    ColorSpace = 1;
    ComponentsConfiguration =         (
        0,
        0,
        0,
        1
    );
    DateTimeDigitized = "2012:12:22 12:59:56";
    DateTimeOriginal = "2012:12:22 12:59:56";
    ExifVersion =         (
        2,
        2,
        1
    );
    ExposureMode = 0;
    ExposureProgram = 2;
    ExposureTime = "0.007936508394777775";
    FNumber = "2.799999952316284";
    Flash = 16;
    FlashPixVersion =         (
        1,
        0
    );
    FocalLenIn35mmFilm = 35;
    FocalLength = "3.849999904632568";
    ISOSpeedRatings =         (
        80
    );
    MeteringMode = 5;
    PixelXDimension = 2592;
    PixelYDimension = 1936;
    SceneCaptureType = 0;
    SensingMethod = 2;
    ShutterSpeedValue = "6.973695755004883";
    SubjectArea =         (
        1295,
        967,
        699,
        696
    );
    WhiteBalance = 0;
};
location =     {
    latitude = "52.51933333333334";
    longitude = "13.40083333333333";
};
path = "assets-library://asset/asset.JPG?id=E5040F0C-C86A-411B-ADA8-36C9EC91A526&ext=JPG";
}

I've done some research on the internet to find out what kind of array this is, and it seems a Plist (XML) to me. I've looked through the myMedia module Classes, and I've found out that it uses the NSDictionary Class to return the EXIF data.

I've tried some PHP Plist parses to process this array, but none of them actually work. I have no idea how to parse this array.

Thanks in advance!


Solution

  • I've found a solution! JSONkit solved my problem. Apple's own JSON library didn't work, it was unable to convert the NSDictionary object to a JSON string.

    I inserted this string in MyMediaModule.M (after line 1473):

    NSString *JSONMeta = [metadata JSONString];
    

    Then I replaced line 1475:

    //[dictionary setObject:dictMeta forKey:@"metadata"];
    [dictionary setObject:JSONMeta forKey:@"JSONMeta"];
    

    Also don't forget to include the JSONkit library:

    #import "JSONKit.h"
    

    Now in Appcelerator Titanium just use event.JSONMeta instead of event.metadata:

    mymedia.openPhotoGallery({
        success:function(event)
        {
            Ti.API.info(event.JSONMeta);
        }
    }
    

    Thank you for all your help! I had never coded in Objective-C so it was quite hard to start, but after all the solution is very simple.