Skip to content

Commit f10f984

Browse files
feat(ios): use ASWebAuthenticationSession instead of SFAuthenticationSession (#21)
1 parent 3f8c37f commit f10f984

9 files changed

+71
-55
lines changed

ios/Classes/TiWebdialogAuthenticationSessionProxy.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
* Please see the LICENSE included with this distribution for details.
77
*/
88

9-
#if IS_IOS_11
10-
119
#import "TiProxy.h"
12-
#import <SafariServices/SafariServices.h>
10+
#import <AuthenticationServices/AuthenticationServices.h>
1311

14-
@interface TiWebdialogAuthenticationSessionProxy : TiProxy {
15-
SFAuthenticationSession *_authSession;
12+
#if IS_IOS_13
13+
@interface TiWebdialogAuthenticationSessionProxy : TiProxy <ASWebAuthenticationPresentationContextProviding>
14+
#else
15+
@interface TiWebdialogAuthenticationSessionProxy : TiProxy
16+
#endif
17+
{
18+
id _authSession;
1619
}
1720

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

2629
@end
27-
28-
#endif

ios/Classes/TiWebdialogAuthenticationSessionProxy.m

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,77 @@
66
* Please see the LICENSE included with this distribution for details.
77
*/
88

9-
#if IS_IOS_11
10-
119
#import "TiWebdialogAuthenticationSessionProxy.h"
1210
#import "TiUtils.h"
11+
#import <SafariServices/SafariServices.h>
1312

1413
@implementation TiWebdialogAuthenticationSessionProxy
1514

16-
- (SFAuthenticationSession *)authSession
15+
- (id)authSession
1716
{
1817
if (_authSession == nil) {
1918
NSString *url = [TiUtils stringValue:[self valueForKey:@"url"]];
2019
NSString *scheme = [TiUtils stringValue:[self valueForKey:@"scheme"]];
2120

22-
_authSession = [[SFAuthenticationSession alloc] initWithURL:[TiUtils toURL:url proxy:self]
23-
callbackURLScheme:[TiUtils stringValue:scheme]
24-
completionHandler:^(NSURL *callbackURL, NSError *error) {
25-
NSMutableDictionary *event = [NSMutableDictionary dictionaryWithDictionary:@{
26-
@"success" : NUMBOOL(error == nil)
21+
if ([TiUtils isIOSVersionOrGreater:@"12.0"]) {
22+
_authSession = [[ASWebAuthenticationSession alloc] initWithURL:[TiUtils toURL:url proxy:self]
23+
callbackURLScheme:scheme
24+
completionHandler:^(NSURL *_Nullable callbackURL, NSError *_Nullable error) {
25+
[self fireEventWithCallbackUrl:callbackURL andError:error];
26+
}];
27+
#if IS_IOS_13
28+
if ([TiUtils isIOSVersionOrGreater:@"13.0"]) {
29+
((ASWebAuthenticationSession *)_authSession).presentationContextProvider = self;
30+
}
31+
#endif
32+
} else {
33+
_authSession = [[SFAuthenticationSession alloc] initWithURL:[TiUtils toURL:url proxy:self]
34+
callbackURLScheme:scheme
35+
completionHandler:^(NSURL *callbackURL, NSError *error) {
36+
[self fireEventWithCallbackUrl:callbackURL andError:error];
2737
}];
38+
}
39+
}
2840

29-
if (error != nil) {
30-
[event setObject:[error localizedDescription] forKey:@"error"];
31-
} else {
32-
[event setObject:[callbackURL absoluteString] forKey:@"callbackURL"];
33-
}
41+
return _authSession;
42+
}
3443

35-
if ([self _hasListeners:@"callback"]) {
36-
[self fireEvent:@"callback" withObject:event];
37-
}
38-
}];
44+
- (void)fireEventWithCallbackUrl:(NSURL *)callbackURL andError:(NSError *)error
45+
{
46+
NSMutableDictionary *event = [NSMutableDictionary dictionaryWithDictionary:@{
47+
@"success" : NUMBOOL(error == nil)
48+
}];
49+
50+
if (error != nil) {
51+
[event setObject:[error localizedDescription] forKey:@"error"];
52+
} else {
53+
[event setObject:[callbackURL absoluteString] forKey:@"callbackURL"];
3954
}
4055

41-
return _authSession;
56+
if ([self _hasListeners:@"callback"]) {
57+
[self fireEvent:@"callback" withObject:event];
58+
}
4259
}
4360

61+
#pragma mark Delegate method
62+
63+
#if IS_IOS_13
64+
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session
65+
{
66+
return [[UIApplication sharedApplication] keyWindow];
67+
}
68+
#endif
69+
4470
#pragma mark Public API's
4571

4672
- (void)start:(id)unused
4773
{
48-
[[self authSession] start];
74+
id session = [self authSession];
75+
if ([session isKindOfClass:[SFAuthenticationSession class]]) {
76+
[(SFAuthenticationSession *)session start];
77+
} else if ([session isKindOfClass:[ASWebAuthenticationSession class]]) {
78+
[(ASWebAuthenticationSession *)session start];
79+
}
4980
}
5081

5182
- (void)cancel:(id)unused
@@ -59,5 +90,3 @@ - (NSNumber *)isSupported:(id)unused
5990
}
6091

6192
@end
62-
63-
#endif

ios/Classes/TiWebdialogModule.m

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ - (SFSafariViewController *)safariController:(NSString *)url withEntersReaderIfA
6363
{
6464
if (_safariController == nil) {
6565
NSURL *safariURL = [NSURL URLWithString:[url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
66-
#if IS_IOS_11
6766
if (@available(iOS 11.0, *)) {
6867
SFSafariViewControllerConfiguration *config = [[SFSafariViewControllerConfiguration alloc] init];
6968
config.entersReaderIfAvailable = entersReaderIfAvailable;
@@ -72,12 +71,9 @@ - (SFSafariViewController *)safariController:(NSString *)url withEntersReaderIfA
7271
_safariController = [[SFSafariViewController alloc] initWithURL:safariURL
7372
configuration:config];
7473
} else {
75-
#endif
7674
_safariController = [[SFSafariViewController alloc] initWithURL:safariURL
7775
entersReaderIfAvailable:entersReaderIfAvailable];
78-
#if IS_IOS_11
7976
}
80-
#endif
8177

8278
[_safariController setDelegate:self];
8379
}
@@ -115,7 +111,7 @@ - (NSNumber *)isOpen:(id)unused
115111

116112
- (NSNumber *)isSupported:(id)unused
117113
{
118-
return NUMBOOL([TiUtils isIOS9OrGreater]);
114+
return NUMBOOL([TiUtils isIOSVersionOrGreater:@"9.0"]);
119115
}
120116

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

147-
#if IS_IOS_11
148143
barCollapsingEnabled = [TiUtils boolValue:@"barCollapsingEnabled" properties:args def:YES];
149-
#endif
150144

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

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

160-
if ([TiUtils isIOS10OrGreater]) {
154+
if ([TiUtils isIOSVersionOrGreater:@"10.0"]) {
161155
[safari setPreferredControlTintColor:[newColor _color]];
162156
} else {
163157
[[safari view] setTintColor:[newColor _color]];
164158
}
165159
}
166160

167161
if ([args objectForKey:@"barColor"]) {
168-
if ([TiUtils isIOS10OrGreater]) {
162+
if ([TiUtils isIOSVersionOrGreater:@"10.0"]) {
169163
[safari setPreferredBarTintColor:[[TiUtils colorValue:@"barColor" properties:args] _color]];
170164
} else {
171165
NSLog(@"[ERROR] Ti.WebDialog: The barColor property is only available in iOS 10 and later");
172166
}
173167
}
174168

175-
#if IS_IOS_11
176169
if ([args objectForKey:@"dismissButtonStyle"]) {
177170
if (@available(iOS 11.0, *)) {
178171
[safari setDismissButtonStyle:[TiUtils intValue:@"dismissButtonStyle" properties:args def:SFSafariViewControllerDismissButtonStyleDone]];
179172
} else {
180173
NSLog(@"[ERROR] Ti.WebDialog: The dismissButtonStyle property is only available in iOS 11 and later");
181174
}
182175
}
183-
#endif
184176

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

199191
#pragma mark Constants
200192

201-
#if IS_IOS_11
202193
MAKE_SYSTEM_PROP(DISMISS_BUTTON_STYLE_DONE, SFSafariViewControllerDismissButtonStyleDone);
203194
MAKE_SYSTEM_PROP(DISMISS_BUTTON_STYLE_CLOSE, SFSafariViewControllerDismissButtonStyleClose);
204195
MAKE_SYSTEM_PROP(DISMISS_BUTTON_STYLE_CANCEL, SFSafariViewControllerDismissButtonStyleCancel);
205-
#endif
206196

207197
@end

ios/TiWebdialog_Prefix.pch

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
#import <Foundation/Foundation.h>
44
#endif
55

6-
// iOS 11+ API's
7-
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
8-
#define IS_IOS_11 true
6+
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
7+
#define IS_IOS_13 true
98
#else
10-
#define IS_IOS_11 false
9+
#define IS_IOS_13 false
1110
#endif

ios/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 1.1.0
5+
version: 1.2.0
66
apiversion: 2
77
architectures: armv7 arm64 i386 x86_64
88
description: titanium-web-dialog

ios/module.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
OTHER_LDFLAGS=$(inherited) -weak_framework SafariServices
1+
OTHER_LDFLAGS=$(inherited) -weak_framework SafariServices -weak_framework AuthenticationServices

ios/titanium.xcconfig

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
// OF YOUR TITANIUM SDK YOU'RE BUILDING FOR
55
//
66
//
7-
TITANIUM_SDK_VERSION = 7.5.2.GA
7+
TITANIUM_SDK_VERSION = 9.0.0.GA
88

99
//
1010
// THESE SHOULD BE OK GENERALLY AS-IS
1111
//
12-
TITANIUM_SDK = ~/Library/Application Support/Titanium/mobilesdk/osx/$(TITANIUM_SDK_VERSION)
13-
TITANIUM_BASE_SDK = "$(TITANIUM_SDK)/iphone/include"
14-
TITANIUM_BASE_SDK2 = "$(TITANIUM_SDK)/iphone/include/TiCore"
15-
TITANIUM_BASE_SDK3 = "$(TITANIUM_SDK)/iphone/include/JavaScriptCore"
16-
HEADER_SEARCH_PATHS= $(TITANIUM_BASE_SDK) $(TITANIUM_BASE_SDK2) $(TITANIUM_BASE_SDK3)
12+
TITANIUM_SDK = /Users/$(USER)/Library/Application Support/Titanium/mobilesdk/osx/$(TITANIUM_SDK_VERSION)
13+
HEADER_SEARCH_PATHS = $(inherited) "$(TITANIUM_SDK)/iphone/include"
1714
FRAMEWORK_SEARCH_PATHS = $(inherited) "$(TITANIUM_SDK)/iphone/Frameworks"
1815

1916

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@titanium-sdk/ti.webdialog",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Use the native `SFSafariViewController` (iOS) and `Chrome Pages` (Android) within Axway Titanium.",
55
"scripts": {
66
"commit": "git-cz",

0 commit comments

Comments
 (0)