Search code examples
iosobjective-cjsonafnetworkingmfmailcomposeviewcontroller

get JSON file or info from AFNetworking


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?

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.