我在阅读GMAIL的电子邮件时遇到问题。
这就是我收到每封电子邮件的方式:
- (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];
}
}
我认为这个身体是用base64之类的东西编码的,但我不确定。
这是我得到的一个例子:
Q3J1bmNoIERhaWx5DQoNClRvZGF5J3MgVG9wIFN0b3JpZXMgLy8gSnVsIDYsIDIwMTUgUmVhZCBtb3JlIGF0IHRlY2hjcnVuY2guY29tIDwjPiBJZiB5b3UgZG8gbm90IHdhbnQgdG8gcmVjZWl2ZSB0aGlzLCB5b3UgY2FuIHVuc3Vic2NyaWJlIHdpdGggb25lIGNsaWNrIGhlcmUgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLQjA5YWQ-Lg0KDQpUZWNoQ3J1bmNoIDQxMCBUb3duc2VuZCBTdHJlZXQsIFNhbiBGcmFuY2lzY28sIENBIDk0MTA3DQoNCsKpIDIwMTIgQU9MIEluYy4gQWxsIHJpZ2h0cyByZXNlcnZlZC4gUHJpdmFjeSBQb2xpY3kgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLQ2NiNWE-IFRlcm1zIG9mIFNlcnZpY2UgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS9VQnJ0TnFndnVHMEFyVWxLRDRkZjU-DQoNCklmIHlvdSBiZWxpZXZlIHRoaXMgaGFzIGJlZW4gc2VudCB0byB5b3UgaW4gZXJyb3IsIHBsZWFzZSBzYWZlbHkgdW5zdWJzY3JpYmUgPGh0dHA6Ly9saW5rLnRlY2hjcnVuY2guY29tL29jLzU0ZWQ3ZDc2ZmExM2ExYWQyMjhiNTA2ZjJzcGJnLmc4aS84YzIyN2ZiNz4u
我该如何解码??
消息的正文是base64url编码的。在base64解码之前,您需要对编码字符串中的"_"替换为"/",将"-"替换为"+"进行解码。
补充Furhan的答案
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)
有一个GTL API可以解码OOTB。这是我的工作代码。
//... 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)")
}
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)
}
您可以在webview 中加载数据
webView.load(data, mimeType:mimeType, textEncodingName: "", baseURL: baseUrl)
查看更多信息:https://developers.google.com/gmail/api/v1/reference/users/messages#resource