在ios 10中将事件添加到Google日历

时间:2016-12-13 11:28:26

标签: ios objective-c xcode google-calendar-api ios10

我正在尝试将事件添加到Google日历中,但在ios 10中,即使我在设置中登录我的Gmail帐户,我也会收到以下错误 - >日历。enter image description here

我正在使用以下代码

    GTLServiceCalendar *_calendarService = [[GTLServiceCalendar alloc] init];

    _calendarService.authorizer = [GTMOAuth2ViewControllerTouch
                                   authForGoogleFromKeychainForName:kGoogleAPIKeychainItemName
                                   clientID:kGoogleAPIClientID
                                   clientSecret:nil];

    if (!_calendarService.authorizer.canAuthorize)
        //if([auth canAuthorize])
    {
        [self launchGoogleAuthenticationView];
    }
    else {


        [self addEventToGoogleCalendar];
    }
  - (void)launchGoogleAuthenticationView {
_didCancelGoogleAuthentication = NO;

GTMOAuth2ViewControllerTouch *authController;

// If modifying these scopes, delete your previously saved credentials by
// resetting the iOS simulator or uninstall the app.
NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeCalendar, nil];

authController = [[GTMOAuth2ViewControllerTouch alloc]
                  initWithScope:[scopes componentsJoinedByString:@" "]
                  clientID:kGoogleAPIClientID
                  clientSecret:nil
                  keychainItemName:kGoogleAPIKeychainItemName
                  delegate:self
                  finishedSelector:@selector(googleAuthenticationViewController:finishedWithAuth:error:)];

UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

[closeButton setTitle:@"Cancel" forState:UIControlStateNormal];

[closeButton setBackgroundColor:appredcolor];

[closeButton addTarget:self
                action:@selector(didTapCloseButton:)
      forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *closeButtonItem = [[UIBarButtonItem alloc]
                                    initWithCustomView:closeButton];

[closeButtonItem setTintColor:appredcolor];

[authController.navigationItem setLeftBarButtonItem:closeButtonItem];

UINavigationController *navController = [[UINavigationController alloc]
                                         initWithRootViewController:authController];

 [myappDelegate.navCont.viewControllers.lastObject presentViewController:navController animated:YES completion:nil];
 }
- (void)addEventToGoogleCalendar {

NSString *appdatestr =[[CTManager sharedInstance] getAppointmentForId:cardict.carsIdentifier];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"dd-MMM-yy, hh:mm a"];

_calendarEvent = [[GTLCalendarEvent alloc] init];

[_calendarEvent setSummary:@"Fathik"];
[_calendarEvent setDescriptionProperty:@"Adding event in UCR"];

NSDate *startDate = [outputFormatter dateFromString:appdatestr];
NSDate *endDate = [startDate dateByAddingTimeInterval:60*60];

if (endDate == nil) {
    endDate = [startDate dateByAddingTimeInterval:(60 * 60)];
}

GTLDateTime *startTime = [GTLDateTime dateTimeWithDate:startDate
                                              timeZone:[NSTimeZone systemTimeZone]];

[_calendarEvent setStart:[GTLCalendarEventDateTime object]];
[_calendarEvent.start setDateTime:startTime];

GTLDateTime *endTime = [GTLDateTime dateTimeWithDate:endDate
                                            timeZone:[NSTimeZone systemTimeZone]];

[_calendarEvent setEnd:[GTLCalendarEventDateTime object]];
[_calendarEvent.end setDateTime:endTime];


GTLQueryCalendar *insertQuery = [GTLQueryCalendar queryForEventsInsertWithObject:_calendarEvent
                                                                      calendarId:kGoogleAPICalendarID];
//[self showAlertWithTitle:nil
//     andMessage:NSLocalizedString(@"Adding Event…", nil)];

[_calendarService executeQuery:insertQuery
             completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
                 if (error == nil) {
                     NSLog(@"event added");
                     [myappDelegate.navCont.view makeToast:@"Event added to your Calender"
                                                  duration:2.0
                                                  position:CSToastPositionBottom
                                                     title:nil];
                 } else {
                     NSLog(@"event added failed --- %@",[error description]);
                 }
             }];


}

请提出任何想法。谢谢提前

2 个答案:

答案 0 :(得分:1)

在viewDidLoad()中添加以下几行代码 我遇到了同样的问题,经过一个很好的追求问题,我得到了以下给定的解决方案。 *感谢Adnan Ameen先生*

//设置默认使用者

NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

答案 1 :(得分:0)

Google已更新其对OAuth流量的安全限制。他们不会允许原生网络视图启动OAuth流程,而是鼓励人们使用操作系统浏览器这样做。在您的情况下,您可能需要等待Google日历SDK更新其代码以遵守新推荐的流程。有关详细信息,请参阅Google blog