Google Drive iOS SDK:显示取消loginbutton
我在iOS应用程序上工作,我使用Google驱动器来访问我的文件,login和列表文件工作正常,但我只是问我可以如何添加一个取消buttonlogin界面提供的谷歌驱动器sdk看图像怒吼
因为,你看到没有办法做一个cancel
或go back
button。
这是我的代码
// verify if the user is already connected or not - (void)checkIfIsConnected { // Check for authorization. GTMOAuth2Authentication *auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kClientID clientSecret:kClientSecret]; if ([auth canAuthorize]) { [self isAuthorizedWithAuthentication:auth]; }else { [self ConnectToDrive]; } } - (GTLServiceDrive *)driveService { static GTLServiceDrive *service = nil; if (!service) { service = [[GTLServiceDrive alloc] init]; // Have the service object set tickets to fetch consecutive pages // of the feed so we do not need to manually fetch them. service.shouldFetchNextPages = YES; // Have the service object set tickets to retry temporary error conditions // automatically. service.retryEnabled = YES; } return service; } -(void) ConnectToDrive{ SEL finishedSelector = @selector(viewController:finishedWithAuth:error:); GTMOAuth2ViewControllerTouch *authViewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive clientID:kClientID clientSecret:kClientSecret keychainItemName:kKeychainItemName delegate:self finishedSelector:finishedSelector]; [self.fileManagementViewController presentModalViewController:authViewController animated:YES]; } // Action executed after finishing the Authentication - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error { [self.fileManagementViewController dismissModalViewControllerAnimated:YES]; if (error == nil) { [self isAuthorizedWithAuthentication:auth]; } } - (void)isAuthorizedWithAuthentication:(GTMOAuth2Authentication *)auth { [[self driveService] setAuthorizer:auth]; self.isAuthorized = YES; [self loadDriveFiles]; }
那么什么错误?
请按照步骤 –
转到 – > GTLSource->Common->OAuth2->Touch-->GTMOAuth2ViewControllerTouch.m
> GTLSource->Common->OAuth2->Touch-->GTMOAuth2ViewControllerTouch.m
-(void)viewDidLoad { [self setUpNavigation]; [self.navigationController.navigationBar setTitleTextAttributes:@ {NSForegroundColorAttributeName : [UIColor blueColor]}]; self.navigationController.navigationBar.translucent = NO; UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 63)]; [self.view addSubview:naviBarObj]; UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Cancel", nil)] style:UIBarButtonItemStyleBordered target:self action:@selector(cancelGdriveSignIn:)]; UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:@"Google Drive"]; navigItem.rightBarButtonItem = cancelItem; naviBarObj.items = [NSArray arrayWithObjects: navigItem,nil]; } -(void)cancelGdriveSignIn:(id)sender { [self dismissViewControllerAnimated:YES completion:^(void){}]; } -(void)setUpNavigation // Default Method Available { rightBarButtonItem_.customView = navButtonsView_; self.navigationItem.rightBarButtonItem = rightBarButtonItem_; }
一旦在GTMOAuth2ViewControllerTouch.m中添加上述更改并运行它。 你会得到像这样的取消button –
快乐编码……!
改变来源 – 糟糕的方式。 这是我的解决scheme。 适用于iPhone和iPad
GTMOAuth2ViewControllerTouch *authViewController = [GTMOAuth2ViewControllerTouch controllerWithScope:kGTLAuthScopeDrive clientID:GoogleDriveClientID clientSecret:GoogleDriveClientSecret keychainItemName:GoogleDriveKeychainItemName completionHandler:authCompletionHandler]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:authViewController]; navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [rootController presentViewController:navigationController animated:YES completion:nil]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil) style:UIBarButtonItemStylePlain target:self action:@selector(didCanceledAuthorization)]; authViewController.navigationItem.rightBarButtonItem = nil; authViewController.navigationItem.leftBarButtonItem = cancelButton; authViewController.navigationItem.title = @"Google Drive"; });
Swift版本 – 对于新用户 :这不会隐藏导航栏下的Google徽标
扩展类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 }
像魅力一样工作。
Immi的回答很好。 但对于那些有iPad问题的人来说:
你应该使宽度正确,所以replace:
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0., 0., 320., 63.)];
有了这个
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0., 0., [[UIScreen mainScreen] bounds].size.width, 63.)];
此外,Google标题被导航栏隐藏。 不好。 那么下一步:在
- (void)moveWebViewFromUnderNavigationBar
改变
CGRect webFrame = CGRectMake(0., 63., self.view.frame.size.width, self.view.frame.size.height);
和在
- (void)viewWillAppear:(BOOL)animated
注释掉方法调用[self isNavigationBarTranslucent]
:
// if ([self isNavigationBarTranslucent]) { [self moveWebViewFromUnderNavigationBar]; // }
UPD。 面向界面为了使navbar更改由于界面方向,我build议使用NSNotification中心,看到这个答案 。 至于这种情况,你应该做下一个:
- 在GTMOAuth2ViewControllerTouch.m的@interface中实现您的NavBar
- 把通知监听器放在ViewDidLoad中。
- 使navBar更改。
那么,我们走吧:
-
正确的下
@interface
把@property (nonatomic, strong) UINavigationBar *naviBarObj;
-
在ViewDidLoad中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChangeNotification 🙂 name:UIDeviceOrientationDidChangeNotification object:nil];
-
最后:
-(void)deviceOrientationDidChangeNotification:(NSNotification*)note { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { self.naviBarObj.frame = CGRectMake(0., 0., [[UIScreen mainScreen] bounds].size.width, 63.0); } else if (orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft) { self.naviBarObj.frame = CGRectMake(0., 0., [[UIScreen mainScreen] bounds].size.height, 63.0); } }
PS不要忘记,你现在应该使用self.naviBarObj
到处使用naviBarObj
。 在naviBarObj
中的ViewDidLoad
之前删除UINavigationBar
UPD 2.0
- (CGRect) setNavBarWidth { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { return CGRectMake(0., 0., [[UIScreen mainScreen] bounds].size.width, 63.0); } else if (orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft) { return CGRectMake(0., 0., [[UIScreen mainScreen] bounds].size.height, 63.0); } return CGRectMake(0., 0., [[UIScreen mainScreen] bounds].size.width, 63.0); }
并将其称为self.naviBarObj = [[UINavigationBar alloc] initWithFrame:[self setNavBarWidth]];
从viewDidLoad
,以及
self.naviBarObj.frame = [self setNavBarWidth];
从deviceOrientationDidChangeNotification
方法然后:)
我同意@ nab0y4enko – 更改SDK是不好的。
1.当前的GTMOAuth2ViewControllerTouch添加2个button到导航栏。 所以我不得不创build新的类“CustomAuthViewController”inheritance自GTMOAuth2ViewControllerTouch并覆盖setUpNavigation没有实现。这个实现不会添加这2个button
@implementation CustomAuthViewController - (void)setUpNavigation { // Don't call super becasue it is adding ugly Back / Forwad image buttons } @end
2.我们可以设置取消button而不用声明和改变导航宽度,而不用调度asynchronous线程。 我们可以通过编辑navigationBar.topItem而不是导航项来完成。
UIViewController* navController = [self createAuthNavigationController]; [self.originViewController presentViewController:navController animated:YES completion:nil]; - (UIViewController *)createAuthNavigationController { // Create CustomViewController which inherited from GTMOAuth2ViewControllerTouch CustomAuthViewController * authViewController = [[CustomAuthViewController alloc] initWithScope:kGTLAuthScopeDrive clientID:kClientID clientSecret:kClientSecret keychainItemName:kKeychainItemName delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)]; // Create navigation VC UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:authViewController]; // Set as Modal form -> not full screen in IPad [navController setModalPresentationStyle:UIModalPresentationFormSheet]; // Add cancel button to the navigation navController.navigationBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(buttonCancelTapped:)];; // Set Title [navController.navigationBar.topItem setTitle:@"Google Drive"]; return navController; }
let finishedSelector = #selector(YourViewControllerOrAnotherObject.googleAuthViewController(_:finishedWithAuth:error:)) let authController = GTMOAuth2ViewControllerTouch(scope: kGTLAuthScopeDriveReadonly, clientID: AppGoogleClientID, clientSecret: nil, keychainItemName: keychainItemName, delegate: self, finishedSelector: finishedSelector) let cancelAction = #selector(YourViewControllerOrAnotherObject.dismissGoogleAuthController) authController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: cancelAction) authController.title = "Google Authentication" let navigationController = UINavigationController(rootViewController: authController) self.presentViewController(navigationController, animated: true, completion: { () -> Void in // remove default rightBarButtonItem authController.navigationItem.rightBarButtonItem = nil })