Skip to content

Commit

Permalink
feat(ios): use ASWebAuthenticationSession instead of SFAuthentication…
Browse files Browse the repository at this point in the history
…Session (#21)
  • Loading branch information
vijaysingh-axway authored May 20, 2020
1 parent 3f8c37f commit f10f984
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 55 deletions.
15 changes: 8 additions & 7 deletions ios/Classes/TiWebdialogAuthenticationSessionProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
* Please see the LICENSE included with this distribution for details.
*/

#if IS_IOS_11

#import "TiProxy.h"
#import <SafariServices/SafariServices.h>
#import <AuthenticationServices/AuthenticationServices.h>

@interface TiWebdialogAuthenticationSessionProxy : TiProxy {
SFAuthenticationSession *_authSession;
#if IS_IOS_13
@interface TiWebdialogAuthenticationSessionProxy : TiProxy <ASWebAuthenticationPresentationContextProviding>
#else
@interface TiWebdialogAuthenticationSessionProxy : TiProxy
#endif
{
id _authSession;
}

#pragma mark Public API's
Expand All @@ -24,5 +27,3 @@
- (NSNumber *)isSupported:(id)unused;

@end

#endif
71 changes: 50 additions & 21 deletions ios/Classes/TiWebdialogAuthenticationSessionProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,77 @@
* Please see the LICENSE included with this distribution for details.
*/

#if IS_IOS_11

#import "TiWebdialogAuthenticationSessionProxy.h"
#import "TiUtils.h"
#import <SafariServices/SafariServices.h>

@implementation TiWebdialogAuthenticationSessionProxy

- (SFAuthenticationSession *)authSession
- (id)authSession
{
if (_authSession == nil) {
NSString *url = [TiUtils stringValue:[self valueForKey:@"url"]];
NSString *scheme = [TiUtils stringValue:[self valueForKey:@"scheme"]];

_authSession = [[SFAuthenticationSession alloc] initWithURL:[TiUtils toURL:url proxy:self]
callbackURLScheme:[TiUtils stringValue:scheme]
completionHandler:^(NSURL *callbackURL, NSError *error) {
NSMutableDictionary *event = [NSMutableDictionary dictionaryWithDictionary:@{
@"success" : NUMBOOL(error == nil)
if ([TiUtils isIOSVersionOrGreater:@"12.0"]) {
_authSession = [[ASWebAuthenticationSession alloc] initWithURL:[TiUtils toURL:url proxy:self]
callbackURLScheme:scheme
completionHandler:^(NSURL *_Nullable callbackURL, NSError *_Nullable error) {
[self fireEventWithCallbackUrl:callbackURL andError:error];
}];
#if IS_IOS_13
if ([TiUtils isIOSVersionOrGreater:@"13.0"]) {
((ASWebAuthenticationSession *)_authSession).presentationContextProvider = self;
}
#endif
} else {
_authSession = [[SFAuthenticationSession alloc] initWithURL:[TiUtils toURL:url proxy:self]
callbackURLScheme:scheme
completionHandler:^(NSURL *callbackURL, NSError *error) {
[self fireEventWithCallbackUrl:callbackURL andError:error];
}];
}
}

if (error != nil) {
[event setObject:[error localizedDescription] forKey:@"error"];
} else {
[event setObject:[callbackURL absoluteString] forKey:@"callbackURL"];
}
return _authSession;
}

if ([self _hasListeners:@"callback"]) {
[self fireEvent:@"callback" withObject:event];
}
}];
- (void)fireEventWithCallbackUrl:(NSURL *)callbackURL andError:(NSError *)error
{
NSMutableDictionary *event = [NSMutableDictionary dictionaryWithDictionary:@{
@"success" : NUMBOOL(error == nil)
}];

if (error != nil) {
[event setObject:[error localizedDescription] forKey:@"error"];
} else {
[event setObject:[callbackURL absoluteString] forKey:@"callbackURL"];
}

return _authSession;
if ([self _hasListeners:@"callback"]) {
[self fireEvent:@"callback" withObject:event];
}
}

#pragma mark Delegate method

#if IS_IOS_13
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session
{
return [[UIApplication sharedApplication] keyWindow];
}
#endif

#pragma mark Public API's

- (void)start:(id)unused
{
[[self authSession] start];
id session = [self authSession];
if ([session isKindOfClass:[SFAuthenticationSession class]]) {
[(SFAuthenticationSession *)session start];
} else if ([session isKindOfClass:[ASWebAuthenticationSession class]]) {
[(ASWebAuthenticationSession *)session start];
}
}

- (void)cancel:(id)unused
Expand All @@ -59,5 +90,3 @@ - (NSNumber *)isSupported:(id)unused
}

@end

#endif
16 changes: 3 additions & 13 deletions ios/Classes/TiWebdialogModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ - (SFSafariViewController *)safariController:(NSString *)url withEntersReaderIfA
{
if (_safariController == nil) {
NSURL *safariURL = [NSURL URLWithString:[url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
#if IS_IOS_11
if (@available(iOS 11.0, *)) {
SFSafariViewControllerConfiguration *config = [[SFSafariViewControllerConfiguration alloc] init];
config.entersReaderIfAvailable = entersReaderIfAvailable;
Expand All @@ -72,12 +71,9 @@ - (SFSafariViewController *)safariController:(NSString *)url withEntersReaderIfA
_safariController = [[SFSafariViewController alloc] initWithURL:safariURL
configuration:config];
} else {
#endif
_safariController = [[SFSafariViewController alloc] initWithURL:safariURL
entersReaderIfAvailable:entersReaderIfAvailable];
#if IS_IOS_11
}
#endif

[_safariController setDelegate:self];
}
Expand Down Expand Up @@ -115,7 +111,7 @@ - (NSNumber *)isOpen:(id)unused

- (NSNumber *)isSupported:(id)unused
{
return NUMBOOL([TiUtils isIOS9OrGreater]);
return NUMBOOL([TiUtils isIOSVersionOrGreater:@"9.0"]);
}

- (void)close:(id)unused
Expand Down Expand Up @@ -144,9 +140,7 @@ - (void)open:(id)args
BOOL entersReaderIfAvailable = [TiUtils boolValue:@"entersReaderIfAvailable" properties:args def:YES];
BOOL barCollapsingEnabled = NO;

#if IS_IOS_11
barCollapsingEnabled = [TiUtils boolValue:@"barCollapsingEnabled" properties:args def:YES];
#endif

SFSafariViewController *safari = [self safariController:_url withEntersReaderIfAvailable:entersReaderIfAvailable andBarCollapsingEnabled:barCollapsingEnabled];

Expand All @@ -157,30 +151,28 @@ - (void)open:(id)args
if ([args objectForKey:@"tintColor"]) {
TiColor *newColor = [TiUtils colorValue:@"tintColor" properties:args];

if ([TiUtils isIOS10OrGreater]) {
if ([TiUtils isIOSVersionOrGreater:@"10.0"]) {
[safari setPreferredControlTintColor:[newColor _color]];
} else {
[[safari view] setTintColor:[newColor _color]];
}
}

if ([args objectForKey:@"barColor"]) {
if ([TiUtils isIOS10OrGreater]) {
if ([TiUtils isIOSVersionOrGreater:@"10.0"]) {
[safari setPreferredBarTintColor:[[TiUtils colorValue:@"barColor" properties:args] _color]];
} else {
NSLog(@"[ERROR] Ti.WebDialog: The barColor property is only available in iOS 10 and later");
}
}

#if IS_IOS_11
if ([args objectForKey:@"dismissButtonStyle"]) {
if (@available(iOS 11.0, *)) {
[safari setDismissButtonStyle:[TiUtils intValue:@"dismissButtonStyle" properties:args def:SFSafariViewControllerDismissButtonStyleDone]];
} else {
NSLog(@"[ERROR] Ti.WebDialog: The dismissButtonStyle property is only available in iOS 11 and later");
}
}
#endif

[[TiApp app] showModalController:safari
animated:animated];
Expand All @@ -198,10 +190,8 @@ - (void)open:(id)args

#pragma mark Constants

#if IS_IOS_11
MAKE_SYSTEM_PROP(DISMISS_BUTTON_STYLE_DONE, SFSafariViewControllerDismissButtonStyleDone);
MAKE_SYSTEM_PROP(DISMISS_BUTTON_STYLE_CLOSE, SFSafariViewControllerDismissButtonStyleClose);
MAKE_SYSTEM_PROP(DISMISS_BUTTON_STYLE_CANCEL, SFSafariViewControllerDismissButtonStyleCancel);
#endif

@end
7 changes: 3 additions & 4 deletions ios/TiWebdialog_Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#import <Foundation/Foundation.h>
#endif

// iOS 11+ API's
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
#define IS_IOS_11 true
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
#define IS_IOS_13 true
#else
#define IS_IOS_11 false
#define IS_IOS_13 false
#endif
2 changes: 1 addition & 1 deletion ios/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.1.0
version: 1.2.0
apiversion: 2
architectures: armv7 arm64 i386 x86_64
description: titanium-web-dialog
Expand Down
2 changes: 1 addition & 1 deletion ios/module.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
OTHER_LDFLAGS=$(inherited) -weak_framework SafariServices
OTHER_LDFLAGS=$(inherited) -weak_framework SafariServices -weak_framework AuthenticationServices
9 changes: 3 additions & 6 deletions ios/titanium.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
// OF YOUR TITANIUM SDK YOU'RE BUILDING FOR
//
//
TITANIUM_SDK_VERSION = 7.5.2.GA
TITANIUM_SDK_VERSION = 9.0.0.GA

//
// THESE SHOULD BE OK GENERALLY AS-IS
//
TITANIUM_SDK = ~/Library/Application Support/Titanium/mobilesdk/osx/$(TITANIUM_SDK_VERSION)
TITANIUM_BASE_SDK = "$(TITANIUM_SDK)/iphone/include"
TITANIUM_BASE_SDK2 = "$(TITANIUM_SDK)/iphone/include/TiCore"
TITANIUM_BASE_SDK3 = "$(TITANIUM_SDK)/iphone/include/JavaScriptCore"
HEADER_SEARCH_PATHS= $(TITANIUM_BASE_SDK) $(TITANIUM_BASE_SDK2) $(TITANIUM_BASE_SDK3)
TITANIUM_SDK = /Users/$(USER)/Library/Application Support/Titanium/mobilesdk/osx/$(TITANIUM_SDK_VERSION)
HEADER_SEARCH_PATHS = $(inherited) "$(TITANIUM_SDK)/iphone/include"
FRAMEWORK_SEARCH_PATHS = $(inherited) "$(TITANIUM_SDK)/iphone/Frameworks"


2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@titanium-sdk/ti.webdialog",
"version": "2.0.0",
"version": "2.1.0",
"description": "Use the native `SFSafariViewController` (iOS) and `Chrome Pages` (Android) within Axway Titanium.",
"scripts": {
"commit": "git-cz",
Expand Down

0 comments on commit f10f984

Please sign in to comment.