用presentViewController呈现GTMOAuth2ViewControllerTouch:animated:completion

时间:2014-03-20 23:46:28

标签: ios sdk

我需要将google login viewController作为现有viewController的模态打开。没关系,但我不知道如何添加按钮来关闭模态,因为我没有使用UINavigationController。有人有同样的问题吗?

GTMOAuth2ViewControllerTouch *viewController;
        viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:GOOGLE_SCOPE_YOUTUBE
                                                                  clientID:GOOGLE_CLIENT_ID
                                                              clientSecret:GOOGLE_CLIENT_SECRET
                                                          keychainItemName:GOOGLE_KEYCHAIN
                                                                  delegate:self
                                                          finishedSelector:finishedSel];

        NSDictionary *params = [NSDictionary dictionaryWithObject:@"es" forKey:@"hl"];
        viewController.signIn.additionalAuthorizationParameters = params;
        viewController.signIn.shouldFetchGoogleUserProfile = YES;
        NSString *html = @"<html><body style=\"font-family:Arial\"><div style=\"text-align:center;\">Cargando página para iniciar sesión...</div></body></html>";
        viewController.initialHTMLString = html;
        [self presentViewController:viewController animated:NO completion:nil];

感谢

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题并最终使用

[UIView  beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController:detailView animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

但这不是完美的解决方案(非常丑陋的btw和非平坦的)。我想知道为什么Nav Bar没有出现

[self presentViewController:viewController animated:NO completion:nil];

是的,我们可以手动添加导航栏

UINavigationBar *navBar=[etc…

我认为完美的解决方案是使用新的UIViewTransitions(Apple想强迫我们使用它吗?)。

答案 1 :(得分:1)

这是我对他对这个堆栈溢出问题的回答中提供的Imran Khan优秀答案的重新解释:Google Drive iOS SDK: Display Cancel Login Button

去那里并向上投票他的回复,我只是复制并转换。

if (!self.isAuthorized) {
    SEL selectorFinish = @selector(viewController:finishedWithAuth:error:);
    SEL selectorButtonCancel = @selector(buttonCancelTapped:);

    UINavigationController *navController = [[UINavigationController alloc] init];

    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 63)];
    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:<<localised string for title>>];
    UIBarButtonItem *barButtonItemCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:selectorButtonCancel];

    [navigationItem setRightBarButtonItem:barButtonItemCancel];
    [navigationBar setTranslucent:NO];
    [navigationBar setItems:[NSArray arrayWithObjects: navigationItem,nil]];

    [navController.view addSubview:navigationBar];

    GTMOAuth2ViewControllerTouch *authViewController = nil;
    authViewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
                                                                    clientID:kClientID
                                                                clientSecret:kClientSecret
                                                            keychainItemName:kKeychainItemName
                                                                    delegate:self
                                                            finishedSelector:selectorFinish];

    [navController addChildViewController:authViewController];

    [self.parentTVC presentViewController:navController animated:YES completion:nil];
}

为清晰起见buttonCancelTapped:方法如下......

- (IBAction)buttonCancelTapped:(UIBarButtonItem *)sender {
    [self.parentTVC dismissViewControllerAnimated:YES completion:^(void){}];
}

为了清晰,变量parentTVC是公共属性

@property (nonatomic, strong) UITableViewController *parentTVC;

并使用自定义init方法设置,如下所示......

- (id)initWithParentTVC:(UITableViewController *)tvc {
    self = [super init];
    [self setParentTVC:tvc];

    return self;
}

从父视图控制器调用此自定义init方法。

答案 2 :(得分:1)

Swift版本 - 适用于新用户

扩展课程GTMOAuth2ViewControllerTouch

extension GTMOAuth2ViewControllerTouch
{
    public override func viewDidLoad()
    {
        super.viewDidLoad()
        let cancelItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(self.cancelGdriveSignIn))
        self.navigationController?.navigationBar.topItem?.rightBarButtonItem = cancelItem
        self.navigationController?.navigationBar.topItem?.title = "Google Drive"
    }

    func cancelGdriveSignIn()
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

在返回AuthController之前添加导航控制器

private func createAuthController() -> UIViewController {
    let scopeString = scopes.joinWithSeparator(" ")
    let controller = GTMOAuth2ViewControllerTouch(scope: scopeString, clientID: kClientID, clientSecret: nil, keychainItemName: kKeychainItemName, delegate: self, finishedSelector: #selector(ViewController.viewController(_:finishedWithAuth:error:)))
    let navController = UINavigationController(rootViewController: controller)
    return navController
}

像魅力一样。