Objective-C Requires Chilkat v11.0.0+
Objective-C
Mark a Gmail message as read or archive it
See more GMail REST API Examples
Choose read or archive and supply a Gmail message ID. Marking a message as read removes UNREAD. Archiving removes INBOX while keeping the message available elsewhere in the mailbox. Each run performs the selected action on one message. Removing a label that is already absent is safe to repeat.
Authorize the application with https://www.googleapis.com/auth/gmail.modify. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Objective-C Downloads
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <NSString.h>
#import <CkoStringBuilder.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token LoadFile: @"gmail-token.json"];
if (success != YES) {
NSLog(@"%@",@"Could not read gmail-token.json from the private working directory.");
return;
}
if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"An OAuth access token is required.");
return;
}
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [token StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
NSString *action = @"read";
CkoJsonObject *request = [[CkoJsonObject alloc] init];
if (action == @"read") {
success = [request UpdateString: @"removeLabelIds[0]" value: @"UNREAD"];
if (success != YES) {
NSLog(@"%@",request.LastErrorText);
return;
}
}
else {
if (action != @"archive") {
NSLog(@"%@",@"Choose read or archive.");
return;
}
success = [request UpdateString: @"removeLabelIds[0]" value: @"INBOX"];
if (success != YES) {
NSLog(@"%@",request.LastErrorText);
return;
}
}
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://gmail.googleapis.com/gmail/v1/users/me/messages/"];
[url Append: @"GMAIL_MESSAGE_ID"];
[url Append: @"/modify"];
success = [http HttpJson: @"POST" url: [url GetAsString] json: request contentType: @"application/json" response: resp];
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",@"Gmail did not return an HTTP response.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
NSLog(@"%@%d",@"Gmail HTTP ",[resp.StatusCode intValue]);
success = [reply Load: resp.BodyStr];
if (success == YES) {
NSLog(@"%@",[reply StringOf: @"error.message"]);
}
return;
}
success = [reply Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a Gmail JSON response.");
return;
}
if (([[reply JsonTypeOf: @"id"] intValue] != 1) || ([reply StringOfEquals: @"id" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Expected id in the response.");
return;
}
NSLog(@"%@%@%@%@",@"Completed ",action,@" for message ",[reply StringOf: @"id"]);