Question

I have piece of good working code that downloads and parses JSON from server and returns result in +JSONRequestOperationWithRequest:success:failure: of AFJSONRequestOperation. success (and also failure)part of method takes block as an argument with id JSON argument in it. If I understand right - this object is for representation of JSON file or it's content. I need to have a possibility to send this JSON file (or it's text representation in every other text format) to email, using MFMailComposeViewController. So, two questions:

  • how can I get this JSON content from this file?
  • do I need register .json filetype to have possibility to send it using MFMailComposeViewController?
Was it helpful?

Solution

how can I get this JSON content from this file?

You can get the JSON data response (before it's converted to Foundation objects by NSJSONSerialization) from

  • operation.responseString (as NSString), or
  • operation.responseData (as NSData).

You'll want the latter if you're going to attach it to an e-mail.

do I need register .json filetype to have possibility to send it using MFMailComposeViewController?

You need to call [MFMailComposeViewController -addAttachmentData:mimeType:fileName:]. You can get the mime-type from the operation.response.allHeaderFields NSDictionary. You can also use the official standard, application/json, or text/json, which is commonly used as well.

OTHER TIPS

The response in the succes if not JSON but the object representation of the JSON. Most likely a NSArray or NSDictionary.

What you need to do is either use a normal HTTP request and send that string to the MFMailComposeViewController or change the JSON object back into a JSON string:

NSError *error = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:JSON options:0 error:&error];
NSString *jsonString = nil;
if (jsonData) {
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
} else {
    NSLog(@"Error creating JSON: %@", error);
}

Then you can pass the jsonString in your MFMailComposeViewController.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top