2

I have problems reading an email from GMAIL.

This is how I get each email:

- (void)displayResultMessageWithTicket:(GTLServiceTicket *)ticket
                    finishedWithObject:(GTLGmailMessage *)email
                                 error:(NSError *)error
{
    if (error == nil) {
        NSMutableString *labelString = [[NSMutableString alloc] init];

        GTLGmailMessagePart* payload=email.payload;
        NSLog(@"payload =%@",payload);

        for(GTLGmailMessagePart* part in payload.parts)
        {
            GTLGmailMessagePartBody* body = part.body;
            NSLog(@"body =%@",body);
            NSLog(@"data =%@",body.data);
        }
        self.output.text = labelString;
    }
    else
    {
        [self showAlert:@"Error" message:error.localizedDescription];
    }
}

I think that body is encoded with something like base64, but i'm not sure.

this is an example of what I get:

Q3J1bmNoIERhaWx5DQoNClRvZGF5J3MgVG9wIFN0b3JpZXMgLy8gSnVsIDYsIDIwMTUgUmVhZCBtb3JlIGF0IHRlY2hjcnVuY2guY29tIDwjPiBJZiB5b3UgZG8gbm90IHdhbnQgdG8gcmVjZWl2ZSB0aGlzLCB5b3UgY2FuIHVuc3Vic2NyaWJlIHdpdGggb25lIGNsaWNrIGhlcmUgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLQjA5YWQ-Lg0KDQpUZWNoQ3J1bmNoIDQxMCBUb3duc2VuZCBTdHJlZXQsIFNhbiBGcmFuY2lzY28sIENBIDk0MTA3DQoNCsKpIDIwMTIgQU9MIEluYy4gQWxsIHJpZ2h0cyByZXNlcnZlZC4gUHJpdmFjeSBQb2xpY3kgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLQ2NiNWE-IFRlcm1zIG9mIFNlcnZpY2UgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLRDRkZjU-DQoNCklmIHlvdSBiZWxpZXZlIHRoaXMgaGFzIGJlZW4gc2VudCB0byB5b3UgaW4gZXJyb3IsIHBsZWFzZSBzYWZlbHkgdW5zdWJzY3JpYmUgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tL29jLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS84YzIyN2ZiNz4u

How can i decode it??

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aarranz
  • 461
  • 6
  • 20
  • http://stackoverflow.com/questions/19088231/base64-decoding-in-ios-7 – Tholle Jul 07 '15 at 15:34
  • I tried it before asking and sometimes I get the email decoded and I can read it, but others, body.data contains info but then, decodedData is nil... and decodedString is @"" – Aarranz Jul 07 '15 at 16:10

4 Answers4

4

Body of the message is base64url encoded. You need to decode that also replace "_" with "/" and "-" with "+" in the encoded string before base64 decoding it.

Furhan S.
  • 1,494
  • 2
  • 13
  • 22
2

Complementing the Furhan's answer

var parts = eachItem.payload.parts
let body: AnyObject? = parts[0].valueForKey("body")
var base64DataString =  body!.valueForKey("data") as! String
base64DataString = base64DataString.stringByReplacingOccurrencesOfString("_", withString: "/", options: NSStringCompareOptions.LiteralSearch, range: nil)
base64DataString = base64DataString.stringByReplacingOccurrencesOfString("-", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil)

let decodedData = NSData(base64EncodedString: base64DataString, options:NSDataBase64DecodingOptions(rawValue: 0))
let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)
println(decodedString)
jose920405
  • 7,982
  • 6
  • 45
  • 71
0

There is a GTL API can do decode OOTB. Here is my working code.

//... get part
let body : GTLGmailMessagePartBody = part.body
let bodyAttachmentId = body.attachmentId
let bodyData : String! = body.data
let bodySize = body.size

//Use GTLDecodeWebSafeBase64
if bodyData != nil
{
    let decodedData = GTLDecodeWebSafeBase64(bodyData)
    let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)

    print("after decoded: \(decodedString)")
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
denny
  • 199
  • 1
  • 7
0
if let bodyPart = message.payload?.parts.filter({ $0.mimeType == "text/html" }).first, let bodyDataEncoded = bodyPart.body?.data {
    logger(bodyPart.body?.size)
    let base64Encoded = bodyDataEncoded.replacingOccurrences(of: "_", with: "/").replacingOccurrences(of: "-", with: "+")
    let data = Data(base64Encoded: base64Encoded)
    logger(data?.count)
}

You can load data in webview

webView.load(data, mimeType:mimeType, textEncodingName: "", baseURL: baseUrl)

See more: https://developers.google.com/gmail/api/v1/reference/users/messages#resource

Conan
  • 101
  • 1
  • 3