Search code examples
iosiphoneobjective-cgmail

Need suggestion to integrate gmail account in iOS developlment


I am new to iOS programming, and just want to do something fun. I want to develop a mail client app for iPhone, it can add email accounts such as gmail and Yahoo and so on. I searched online for a while and also find some answers, before I dive into the details I just want someone who has similar experience give me some suggestions about which method is the best.

thanks


Solution

  • I have recently implemented gmail api to fetch gmail contacts and their email in my tableview. Gmail api is depricated, thats why you might have not got any proper documentation for that.

    To implement gmail use libGDataTouchStaticLib.a library, with Gdata headers (search on google for that otherwise send me your email i will send you its zip).

    The code to get gmail details are as follows

    - (void)getGoogleContacts {
    
        GDataServiceGoogleContact *service = [self contactService];
        GDataServiceTicket *ticket;
    
        BOOL shouldShowDeleted = TRUE;
    
        // request a whole buncha contacts; our service object is set to
        // follow next links as well in case there are more than 2000
        const int kBuncha = 2000;
    
        NSURL *feedURL = [GDataServiceGoogleContact contactFeedURLForUserID:kGDataServiceDefaultUser];
    
        GDataQueryContact *query = [GDataQueryContact contactQueryWithFeedURL:feedURL];
        [query setShouldShowDeleted:shouldShowDeleted];
        [query setMaxResults:kBuncha];
    
        ticket = [service fetchFeedWithQuery:query
                                    delegate:self
                           didFinishSelector:@selector(contactsFetchTicket:finishedWithFeed:error:)];
    
        [self setContactFetchTicket:ticket];
    }
    
    - (void)setContactFetchTicket:(GDataServiceTicket *)ticket {
    
        mContactFetchTicket = ticket;
    }
    
    - (GDataServiceGoogleContact *)contactService {
    
        static GDataServiceGoogleContact* service = nil;
    
        if (!service) {
    
            service = [[GDataServiceGoogleContact alloc] init];
    
            [service setShouldCacheResponseData:YES];
            [service setServiceShouldFollowNextLinks:YES];
        }
    
        // update the username/password each time the service is requested
        NSString *username = [txtUserName text];
        NSString *password = [txtPasswrod text];
    
        [service setUserCredentialsWithUsername:username
                                       password:password];
    
        return service;
    }
    
    
    // contacts fetched callback
    - (void)contactsFetchTicket:(GDataServiceTicket *)ticket
               finishedWithFeed:(GDataFeedContact *)feed
                          error:(NSError *)error {
    
        if (error) {
    
            NSDictionary *userInfo = [error userInfo];
            NSLog(@"Contacts Fetch error :%@", [userInfo objectForKey:@"Error"]);
            if ([[userInfo objectForKey:@"Error"] isEqual:@"BadAuthentication"]) {
    
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                                    message:@"Authentication Failed"
                                                                   delegate:self
                                                          cancelButtonTitle:@"Ok"
                                                          otherButtonTitles:nil, nil];
                [alertView show];
    
            } else {
    
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                                    message:@"Failed to get Contacts."
                                                                   delegate:self
                                                          cancelButtonTitle:@"Ok"
                                                          otherButtonTitles:nil, nil];
                [alertView show];
            }
    
        } else {
    
            NSArray *contacts = [feed entries];
            NSLog(@"Contacts Count: %d ", [contacts count]);
            [mutAryGoogleContacts removeAllObjects];
            for (int i = 0; i < [contacts count]; i++) {
    
                NSMutableDictionary *aDictContactDetails=[NSMutableDictionary dictionary];
    
    
                GDataEntryContact *contact = [contacts objectAtIndex:i];
                // Email
                GDataEmail *email = [[contact emailAddresses] objectAtIndex:0];
                NSString* ContactEmail = [email address];
                if (ContactEmail) {
                    [aDictContactDetails setObject:ContactEmail forKey:@"email"];
    
    
    
    
                // Name
                NSString *ContactName = [[[contact name] fullName] contentStringValue];
                if (ContactName) {
                    [aDictContactDetails setObject:ContactName forKey:@"friendName"];
    
                }
                [mutAryGoogleContacts addObject:aDictContactDetails];
    
                }
    
            }
    
           //Push to next vc or do whatever you want
        }
    
    
    }