Skip to content

Implements web view for presenting Auth web content on iOS 7 and 8. #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 44 additions & 42 deletions Example/Auth/Tests/FIRAuthURLPresenterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#import "FIRAuthUIDelegate.h"
#import "FIRAuthURLPresenter.h"
#import "FIRAuthWebViewController.h"

/** @var kExpectationTimeout
@brief The maximum time waiting for expectations to fulfill.
Expand Down Expand Up @@ -61,7 +62,6 @@ - (void)testFIRAuthURLPresenterNilUIDelegate {
*/
- (void)testFIRAuthURLPresenterUsingDefaultUIDelegate:(BOOL)usesDefaultUIDelegate {
id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
id mockUIApplication = OCMPartialMock([UIApplication sharedApplication]);
NSURL *presenterURL = [NSURL URLWithString:@"https://presenter.url"];
FIRAuthURLPresenter *presenter = [[FIRAuthURLPresenter alloc] init];

Expand All @@ -70,68 +70,70 @@ - (void)testFIRAuthURLPresenterUsingDefaultUIDelegate:(BOOL)usesDefaultUIDelegat
OCMStub(ClassMethod([mockDefaultUIDelegateClass defaultUIDelegate])).andReturn(mockUIDelegate);
}

XCTestExpectation *callbackMatcherExpectation =
[self expectationWithDescription:@"callbackMatcher callback"];
__block XCTestExpectation *callbackMatcherExpectation;
FIRAuthURLCallbackMatcher callbackMatcher = ^BOOL(NSURL *_Nonnull callbackURL) {
XCTAssertNotNil(callbackMatcherExpectation);
XCTAssertEqualObjects(callbackURL, presenterURL);
[callbackMatcherExpectation fulfill];
return YES;
};

XCTestExpectation *completionBlockExpectation =
[self expectationWithDescription:@"completion callback"];
__block XCTestExpectation *completionBlockExpectation;
FIRAuthURLPresentationCompletion completionBlock = ^(NSURL *_Nullable callbackURL,
NSError *_Nullable error) {
XCTAssertNotNil(completionBlockExpectation);
XCTAssertEqualObjects(callbackURL, presenterURL);
XCTAssertNil(error);
[completionBlockExpectation fulfill];
};

if ([SFSafariViewController class]) {
id presenterArg = [OCMArg isKindOfClass:[SFSafariViewController class]];
OCMExpect([mockUIDelegate presentViewController:presenterArg
animated:YES
completion:nil]).andDo(^(NSInvocation *invocation) {
__unsafe_unretained id unretainedArgument;
// Indices 0 and 1 indicate the hidden arguments self and _cmd.
// `presentViewController` is at index 2.
[invocation getArgument:&unretainedArgument atIndex:2];

SFSafariViewController *viewController = unretainedArgument;
XCTAssertEqual(viewController.delegate, presenter);
XCTestExpectation *UIPresentationExpectation = [self expectationWithDescription:@"present UI"];
OCMExpect([mockUIDelegate presentViewController:[OCMArg any]
animated:YES
completion:nil]).andDo(^(NSInvocation *invocation) {
XCTAssertTrue([NSThread isMainThread]);
__unsafe_unretained id unretainedArgument;
// Indices 0 and 1 indicate the hidden arguments self and _cmd.
// `presentViewController` is at index 2.
[invocation getArgument:&unretainedArgument atIndex:2];

id presentViewController = unretainedArgument;
if ([SFSafariViewController class]) {
SFSafariViewController *viewController = presentViewController;
XCTAssertTrue([viewController isKindOfClass:[SFSafariViewController class]]);
});
} else {
id mockUIApplicationClass = OCMClassMock([UIApplication class]);
OCMStub(ClassMethod([mockUIApplicationClass sharedApplication])).andReturn(mockUIApplication);
OCMExpect([mockUIApplication openURL:OCMOCK_ANY]).andDo(^(NSInvocation *invocation) {
__unsafe_unretained id unretainedArgument;
// Indices 0 and 1 indicate the hidden arguments self and _cmd.
// `openURL` is at index 2.
[invocation getArgument:&unretainedArgument atIndex:2];
XCTAssertEqualObjects(presenterURL, unretainedArgument);
});
}
XCTAssertEqual(viewController.delegate, presenter);
} else {
UINavigationController *navigationController = presentViewController;
XCTAssertTrue([navigationController isKindOfClass:[UINavigationController class]]);
FIRAuthWebViewController *webViewController =
navigationController.viewControllers.firstObject;
XCTAssertTrue([webViewController isKindOfClass:[FIRAuthWebViewController class]]);
}
[UIPresentationExpectation fulfill];
});

// Present the content.
[presenter presentURL:presenterURL
UIDelegate:usesDefaultUIDelegate ? nil : mockUIDelegate
callbackMatcher:callbackMatcher
completion:completionBlock];
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
OCMVerifyAll(mockUIDelegate);
OCMVerifyAll(mockUIApplication);
if ([SFSafariViewController class]) {
OCMExpect([mockUIDelegate dismissViewControllerAnimated:OCMOCK_ANY
completion:OCMOCK_ANY])
.andDo(^(NSInvocation *invocation) {
__unsafe_unretained id unretainedArgument;
// Indices 0 and 1 indicate the hidden arguments self and _cmd.
// `completion` is at index 3.
[invocation getArgument:&unretainedArgument atIndex:3];
void (^finishBlock)() = unretainedArgument;
finishBlock();
});
}

// Pretend dismissing view controller.
OCMExpect([mockUIDelegate dismissViewControllerAnimated:OCMOCK_ANY
completion:OCMOCK_ANY])
.andDo(^(NSInvocation *invocation) {
XCTAssertTrue([NSThread isMainThread]);
__unsafe_unretained id unretainedArgument;
// Indices 0 and 1 indicate the hidden arguments self and _cmd.
// `completion` is at index 3.
[invocation getArgument:&unretainedArgument atIndex:3];
void (^completion)() = unretainedArgument;
dispatch_async(dispatch_get_main_queue(), completion);
});
completionBlockExpectation = [self expectationWithDescription:@"completion callback"];
callbackMatcherExpectation = [self expectationWithDescription:@"callbackMatcher callback"];

// Close the presented content.
XCTAssertTrue([presenter canHandleURL:presenterURL]);
Expand Down
1 change: 1 addition & 0 deletions Example/Auth/Tests/FIRUserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,7 @@ - (void)testlinkEmailAndRetrieveDataError {
completion:^(FIRAuthDataResult *_Nullable
linkAuthResult,
NSError *_Nullable error) {
XCTAssertTrue([NSThread isMainThread]);
XCTAssertNil(linkAuthResult);
XCTAssertEqual(error.code, FIRAuthErrorCodeTooManyRequests);
[expectation fulfill];
Expand Down
2 changes: 2 additions & 0 deletions Firebase/Auth/FirebaseAuth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Simplify your iOS development, grow your user base, and monetize more effectivel
'Source/**/FIRAuthDefaultUIDelegate.[mh]',
'Source/**/FIRAuthUIDelegate.h',
'Source/**/FIRAuthURLPresenter.[mh]',
'Source/**/FIRAuthWebView.[mh]',
'Source/**/FIRAuthWebViewController.[mh]',
'Source/**/FIRPhoneAuthCredential.[mh]',
'Source/**/FIRPhoneAuthProvider.[mh]'
s.public_header_files = 'Source/Public/*.h'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,16 @@ - (void)verifyPhoneNumber:(NSString *)phoneNumber
callBackOnMainThread(nil, error);
return;
}
FIRAuthURLCallbackMatcher callbackMatcher = ^BOOL(NSURL *_Nullable callbackURL) {
return [self isVerifyAppURL:callbackURL];
};
[_auth.authURLPresenter presentURL:reCAPTCHAURL
UIDelegate:UIDelegate
callbackMatcher:^BOOL(NSURL * _Nullable callbackURL) {
return [self isVerifyAppURL:callbackURL];
}
callbackMatcher:callbackMatcher
completion:^(NSURL *_Nullable callbackURL,
NSError *_Nullable error) {
if (error) {
completion(nil, error);
callBackOnMainThread(nil, error);
return;
}
NSError *reCAPTCHAError;
Expand Down
38 changes: 38 additions & 0 deletions Firebase/Auth/Source/FIRAuthDefaultUIDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "FIRAuthUIDelegate.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRAuthDefaultUIDelegate : NSObject <FIRAuthUIDelegate>

/** @fn defaultUIDelegate
@brief Unavailable. Please use @c +defaultUIDelegate:
*/
- (instancetype)init NS_UNAVAILABLE;

/** @fn defaultUIDelegate
@brief Returns a default FIRAuthUIDelegate object.
@return The default FIRAuthUIDelegate object.
*/
+ (id<FIRAuthUIDelegate>)defaultUIDelegate;

@end

NS_ASSUME_NONNULL_END
14 changes: 5 additions & 9 deletions Firebase/Auth/Source/FIRAuthDefaultUIDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "FIRAuthUIDelegate.h"
#import "FIRAuthDefaultUIDelegate.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRAuthDefaultUIDelegate : NSObject <FIRAuthUIDelegate>
/** @fn defaultUIDelegate
@brief Unavailable. Please use initWithViewController:
*/
- (instancetype)init NS_UNAVAILABLE;
@interface FIRAuthDefaultUIDelegate ()

/** @fn initWithViewController:
@brief Initializes the instance with a view controller.
@param viewController The view controller as the presenting view controller in @c GOIUIDelegate.
@param viewController The view controller as the presenting view controller in @c
FIRAuthUIDelegate.
@return The initialized instance.
*/
- (instancetype)initWithViewController:(UIViewController *)viewController NS_DESIGNATED_INITIALIZER;

@end

@implementation FIRAuthDefaultUIDelegate {
Expand Down
99 changes: 70 additions & 29 deletions Firebase/Auth/Source/FIRAuthURLPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@

#import <SafariServices/SafariServices.h>

#import "FIRAuthDefaultUIDelegate.h"
#import "FIRAuthErrorUtils.h"
#import "FIRAuthGlobalWorkQueue.h"
#import "FIRAuthUIDelegate.h"
#import "FIRAuthWebViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRAuthDefaultUIDelegate : NSObject <FIRAuthUIDelegate>
/** @fn defaultUIDelegate
@brief Returns a default FIRAuthUIDelegate object.
@return The default FIRAuthUIDelegate object.
*/
+ (id<FIRAuthUIDelegate>)defaultUIDelegate;
@end

@interface FIRAuthURLPresenter () <SFSafariViewControllerDelegate>
@interface FIRAuthURLPresenter () <SFSafariViewControllerDelegate, FIRAuthWebViewDelegate>
@end

@implementation FIRAuthURLPresenter {
Expand All @@ -50,6 +45,11 @@ @implementation FIRAuthURLPresenter {
*/
SFSafariViewController *_Nullable _safariViewController;

/** @var _webViewController
@brief The FIRAuthWebViewController used for the current presentation, if any.
*/
FIRAuthWebViewController *_Nullable _webViewController;

/** @var _UIDelegate
@brief The UIDelegate used to present the SFSafariViewController.
*/
Expand All @@ -74,17 +74,20 @@ - (void)presentURL:(NSURL *)URL
_isPresenting = YES;
_callbackMatcher = callbackMatcher;
_completion = completion;
_UIDelegate = UIDelegate ?: [FIRAuthDefaultUIDelegate defaultUIDelegate];
if ([SFSafariViewController class]) {
SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:URL];
_safariViewController = safariViewController;
_safariViewController.delegate = self;
[_UIDelegate presentViewController:safariViewController animated:YES completion:nil];
return;
} else {
// TODO: Use web view instead.
[[UIApplication sharedApplication] openURL:URL];
}
dispatch_async(dispatch_get_main_queue(), ^() {
_UIDelegate = UIDelegate ?: [FIRAuthDefaultUIDelegate defaultUIDelegate];
if ([SFSafariViewController class]) {
_safariViewController = [[SFSafariViewController alloc] initWithURL:URL];
_safariViewController.delegate = self;
[_UIDelegate presentViewController:_safariViewController animated:YES completion:nil];
return;
} else {
_webViewController = [[FIRAuthWebViewController alloc] initWithURL:URL delegate:self];
UINavigationController *navController =
[[UINavigationController alloc] initWithRootViewController:_webViewController];
[_UIDelegate presentViewController:navController animated:YES completion:nil];
}
});
}

- (BOOL)canHandleURL:(NSURL *)URL {
Expand All @@ -98,13 +101,45 @@ - (BOOL)canHandleURL:(NSURL *)URL {
#pragma mark - SFSafariViewControllerDelegate

- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
if (controller == _safariViewController) {
_safariViewController = nil;
//TODO:Ensure that the SFSafariViewController is actually removed from the screen before
//invoking finishPresentationWithURL:error:
[self finishPresentationWithURL:nil
error:[FIRAuthErrorUtils webContextCancelledErrorWithMessage:nil]];
}
dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
if (controller == _safariViewController) {
_safariViewController = nil;
//TODO:Ensure that the SFSafariViewController is actually removed from the screen before
//invoking finishPresentationWithURL:error:
[self finishPresentationWithURL:nil
error:[FIRAuthErrorUtils webContextCancelledErrorWithMessage:nil]];
}
});
}

#pragma mark - FIRAuthwebViewControllerDelegate

- (BOOL)webViewController:(FIRAuthWebViewController *)webViewController canHandleURL:(NSURL *)URL {
__block BOOL result = NO;
dispatch_sync(FIRAuthGlobalWorkQueue(), ^() {
if (webViewController == _webViewController) {
result = [self canHandleURL:URL];
}
});
return result;
}

- (void)webViewControllerDidCancel:(FIRAuthWebViewController *)webViewController {
dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
if (webViewController == _webViewController) {
[self finishPresentationWithURL:nil
error:[FIRAuthErrorUtils webContextCancelledErrorWithMessage:nil]];
}
});
}

- (void)webViewController:(FIRAuthWebViewController *)webViewController
didFailWithError:(NSError *)error {
dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
if (webViewController == _webViewController) {
[self finishPresentationWithURL:nil error:error];
}
});
}

#pragma mark - Private methods
Expand All @@ -127,8 +162,14 @@ - (void)finishPresentationWithURL:(nullable NSURL *)URL
};
SFSafariViewController *safariViewController = _safariViewController;
_safariViewController = nil;
if (safariViewController) {
[UIDelegate dismissViewControllerAnimated:YES completion:finishBlock];
FIRAuthWebViewController *webViewController = _webViewController;
_webViewController = nil;
if (safariViewController || webViewController) {
dispatch_async(dispatch_get_main_queue(), ^() {
[UIDelegate dismissViewControllerAnimated:YES completion:^() {
dispatch_async(FIRAuthGlobalWorkQueue(), finishBlock);
}];
});
} else {
finishBlock();
}
Expand Down
Loading