Диск Google Диска iOS: Дисплей Отмена кнопки входа
Я работаю над iOS-приложением, и я использую диск Google для доступа к моим файлам, файл регистрации и листинга работает нормально, но я просто спрашиваю, как добавить кнопку отмены в интерфейсе входа, предоставленном приводом Google sdk см. изображение ниже
![enter image description here]()
Как вы видите, нет способа сделать кнопку cancel
или go back
.
Вот мой код
// 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];
}
так что не так?
Ответы
Ответ 1
Пожалуйста, выполните следующие действия:
![enter image description here]()
Перейдите к → 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 и запускайте его. вы получите кнопку отмены, например:
![enter image description here]()
Счастливое кодирование......!!
Ответ 2
Измените источники - плохой способ. Вот мое решение. Работает на 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";
});
Ответ 3
Swift Version - для новых пользователей. Это не скроет логотип 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
}
Работает как шарм.
Ответ 4
Ответ
Вы должны сделать ширину правильной, поэтому замените:
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 из-за ориентации интерфейса, я рекомендую использовать NSNotification Center, см. этот ответ.
Что касается этой ситуации, вы должны сделать следующее:
- Внедрите свой NavBar в @interface в GTMOAuth2ViewControllerTouch.m
- Поместите прослушиватель уведомлений в ViewDidLoad.
- Изменить navBar.
Итак, отпустите:
-
прямо под @interface
put
@property (nonatomic, strong) UINavigationBar *naviBarObj;
-
в ViewDidLoad:
[[NSNotificationCenter defaultCenter]
addObserver: самостоятельно
селектор: @selector (deviceOrientationDidChangeNotification:)
Имя: UIDeviceOrientationDidChangeNotification
Объект: ноль];
-
и, наконец:
-(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);
} }
P.S. не забывайте, что теперь вы должны использовать self.naviBarObj
везде, где вы использовали naviBarObj
. И удалите UINavigationBar
до naviBarObj
в ViewDidLoad
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
методов:)
Ответ 5
Я согласен с @nab0y4enko - сменить SDK плохо.
1. Текущий GTMOAuth2ViewControllerTouch добавляет 2 кнопки в навигационную панель.
Поэтому мне пришлось создать новый класс "CustomAuthViewController", который унаследовал от GTMOAuth2ViewControllerTouch и переопределить setUpNavigation без реализации. Эта реализация не добавит эти 2 кнопки
@implementation CustomAuthViewController
- (void)setUpNavigation
{
// Don't call super becasue it is adding ugly Back / Forwad image buttons
}
@end
2. Мы можем установить кнопку отмены без объявления и изменить ширину навигации и без асинхронного потока отправки. Мы можем сделать это, отредактировав 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;
}
Ответ 6
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
})