Need suggestion to integrate gmail account in iOS developlment

倖福魔咒の 提交于 2019-12-25 07:07:38

问题


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


回答1:


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
    }


}


来源:https://stackoverflow.com/questions/21873323/need-suggestion-to-integrate-gmail-account-in-ios-developlment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!