You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.1 KiB

6 years ago
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRAuthWebViewController.h"
  17. #import "FIRAuthWebView.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. @interface FIRAuthWebViewController () <UIWebViewDelegate>
  20. @end
  21. @implementation FIRAuthWebViewController {
  22. /** @var _URL
  23. @brief The initial URL to display.
  24. */
  25. NSURL *_URL;
  26. /** @var _delegate
  27. @brief The delegate to call.
  28. */
  29. __weak id<FIRAuthWebViewControllerDelegate> _delegate;
  30. /** @var _webView;
  31. @brief The web view instance for easier access.
  32. */
  33. __weak FIRAuthWebView *_webView;
  34. }
  35. - (instancetype)initWithURL:(NSURL *)URL
  36. delegate:(__weak id<FIRAuthWebViewControllerDelegate>)delegate {
  37. self = [super initWithNibName:nil bundle:nil];
  38. if (self) {
  39. _URL = URL;
  40. _delegate = delegate;
  41. }
  42. return self;
  43. }
  44. #pragma mark - Lifecycle
  45. - (void)loadView {
  46. FIRAuthWebView *webView = [[FIRAuthWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  47. webView.webView.delegate = self;
  48. self.view = webView;
  49. _webView = webView;
  50. self.navigationItem.leftBarButtonItem =
  51. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
  52. target:self
  53. action:@selector(cancel)];
  54. }
  55. - (void)viewDidAppear:(BOOL)animated {
  56. [super viewDidAppear:animated];
  57. // Loads the requested URL in the web view.
  58. [_webView.webView loadRequest:[NSURLRequest requestWithURL:_URL]];
  59. }
  60. #pragma mark - UI Targets
  61. - (void)cancel {
  62. [_delegate webViewControllerDidCancel:self];
  63. }
  64. #pragma mark - UIWebViewDelegate
  65. - (BOOL)webView:(UIWebView *)webView
  66. shouldStartLoadWithRequest:(NSURLRequest *)request
  67. navigationType:(UIWebViewNavigationType)navigationType {
  68. return ![_delegate webViewController:self canHandleURL:request.URL];
  69. }
  70. - (void)webViewDidStartLoad:(UIWebView *)webView {
  71. // Show & animate the activity indicator.
  72. _webView.spinner.hidden = NO;
  73. [_webView.spinner startAnimating];
  74. }
  75. - (void)webViewDidFinishLoad:(UIWebView *)webView {
  76. // Hide & stop the activity indicator.
  77. _webView.spinner.hidden = YES;
  78. [_webView.spinner stopAnimating];
  79. }
  80. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
  81. if ([error.domain isEqualToString:NSURLErrorDomain] && error.code == NSURLErrorCancelled) {
  82. // It's okay for the page to be redirected before it is completely loaded. See b/32028062 .
  83. return;
  84. }
  85. // Forward notification to our delegate.
  86. [self webViewDidFinishLoad:webView];
  87. [_delegate webViewController:self didFailWithError:error];
  88. }
  89. @end
  90. NS_ASSUME_NONNULL_END