I am having trouble setting up the gmail api. Can anyone tell me where I am going wrong?
Instruction:
Run the following command to download the library using git:
git clone --recursive https://github.com/google/google-api-objectivec-client.git
In the command line I navigated to the root directory of my project and ran that command.
Open Xcode and create a new workspace named "Quickstart".
Using File > Add Files to "Quickstart"..., add the following project to the workspace that you cloned in the previous step:
google-api-objectivec-client/Source/GTLCore.xcodeproj
I did not create a new workspace I simply used the existing one. I clicked file > Add files to "Inbox" and added the GTLCore.xcodeproj
Create a new iOS > Application > Single View Application project named "QuickstartApp", with the organization "com.example". Set the Language to Objective-C, and when saving the project set the Add to and Group fields to "Quickstart".
Since I already have a project I did not do this step.
Add the following frameworks and libraries to the project's Build Phases > Link Binary with Libraries section:
GTL.framework (from GTLiOSCore target)
I clicked on my Inbox project, selected build phases and added the GTL.framework.
Change the following Build Settings:
Add the flag GTL_BUILT_AS_FRAMEWORK=1 to Preprocessor Macros.
I clicked on my Inbox project, clicked build settings, searched for preprocessor macros and added that line of code.
Add the file google-api-objectivec-client/Source/OAuth2/Touch/GTMOAuth2ViewTouch.xib to the project's Supporting Files group.
I selected inboxTests > supporting files, right click add files to inbox however the file is not in that location it is located in
/google-api-objectivec-client/Deps/gtm-oauth2/Source/Touch/GTMOAuth2ViewTouch.xib
Add the following files to the QuickstartApp group:
google-api-objectivec-client/Source/Services/Gmail/Generated/GTLGmail_Sources.m
google-api-objectivec-client/Source/Services/Gmail/Generated/GTLGmail.h
I right click on my inbox folder and selected 'add files to inbox' both of these files were added in my inbox folder.
now at this point try to compile but get an error 'GTLObject.h'file not found
This file is located in the GTLCore.xcodeproj that was added in step 3. Every time I add one of these files it asks for another.
step 4: I created my own files
gmail.h
#ifndef gmail_h
#define gmail_h
#import <UIKit/UIKit.h>
#import "GTL/GTMOAuth2ViewControllerTouch.h"
#import "GTLGmail.h"
@interface gmail : UIViewController
@property (nonatomic, strong) GTLServiceGmail *service;
@property (nonatomic, strong) UITextView *output;
@end
#endif /* gmail_h */
copy and pasted into a new file gmail.m
#import <Foundation/Foundation.h>
#import "gmail.h"
static NSString *const kKeychainItemName = @"Gmail API";
static NSString *const kClientID = @"this is my key";
@implementation gmail
@synthesize service = _service;
@synthesize output = _output;
// When the view loads, create necessary subviews, and initialize the Gmail API service.
- (void)viewDidLoad {
[super viewDidLoad];
// Create a UITextView to display output.
self.output = [[UITextView alloc] initWithFrame:self.view.bounds];
self.output.editable = false;
self.output.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
self.output.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.output];
// Initialize the Gmail API service & load existing credentials from the keychain if available.
self.service = [[GTLServiceGmail alloc] init];
self.service.authorizer =
[GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:kClientID
clientSecret:nil];
}
// When the view appears, ensure that the Gmail API service is authorized, and perform API calls.
- (void)viewDidAppear:(BOOL)animated {
if (!self.service.authorizer.canAuthorize) {
// Not yet authorized, request authorization by pushing the login UI onto the UI stack.
[self presentViewController:[self createAuthController] animated:YES completion:nil];
} else {
[self fetchLabels];
}
}
// Construct a query and get a list of labels from the user's gmail. Display the
// label name in the UITextView
- (void)fetchLabels {
self.output.text = @"Getting labels...";
GTLQueryGmail *query = [GTLQueryGmail queryForUsersLabelsList];
[self.service executeQuery:query
delegate:self
didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
}
- (void)displayResultWithTicket:(GTLServiceTicket *)ticket
finishedWithObject:(GTLGmailListLabelsResponse *)labelsResponse
error:(NSError *)error {
if (error == nil) {
NSMutableString *labelString = [[NSMutableString alloc] init];
if (labelsResponse.labels.count > 0) {
[labelString appendString:@"Labels:\n"];
for (GTLGmailLabel *label in labelsResponse.labels) {
[labelString appendFormat:@"%@\n", label.name];
}
} else {
[labelString appendString:@"No labels found."];
}
self.output.text = labelString;
} else {
[self showAlert:@"Error" message:error.localizedDescription];
}
}
// Creates the auth controller for authorizing access to Gmail API.
- (GTMOAuth2ViewControllerTouch *)createAuthController {
GTMOAuth2ViewControllerTouch *authController;
NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeGmailReadonly, nil];
authController = [[GTMOAuth2ViewControllerTouch alloc]
initWithScope:[scopes componentsJoinedByString:@" "]
clientID:kClientID
clientSecret:nil
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
return authController;
}
// Handle completion of the authorization process, and update the Gmail API
// with the new credentials.
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)authResult
error:(NSError *)error {
if (error != nil) {
[self showAlert:@"Authentication Error" message:error.localizedDescription];
self.service.authorizer = nil;
}
else {
self.service.authorizer = authResult;
[self dismissViewControllerAnimated:YES completion:nil];
}
}
// Helper for showing an alert
- (void)showAlert:(NSString *)title message:(NSString *)message {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
@end
The documentation states that "the static library target also creates a folder with the library's headers to drag into your target's sources". The corresponding directory contains only one header file GTLDefines.h and it apparently makes no reference to other required ones such as GTLObject.h, nor is there a folder that contains only header files. I think it is necessary to manually add references to
other required header files such as Objects/GTLObject.h into the app project.
Try also to look at this link. It states here that he imported headers from the GTL.proj to his project. These include files like GTLBase64.h, GTLBatchQuery.h, GTLBatchResult.h. Without the headers, he gets the error "GTLObject.h file not found." on the import statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With