Skip to content

Commit 9811993

Browse files
committed
update docs for Swift default templates
1 parent 22e9877 commit 9811993

File tree

1 file changed

+75
-40
lines changed

1 file changed

+75
-40
lines changed

docs/docs/introduction.md

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Get started by installing the dependencies in your application
1010
```sh
1111
yarn add react-native-app-auth
1212
```
13+
1314
Or
15+
1416
```sh
1517
npm install react-native-app-auth --save
1618
```
@@ -115,9 +117,79 @@ your `Info.plist` as follows:
115117
You need to retain the auth session, in order to continue the
116118
authorization flow from the redirect. Follow these steps:
117119
118-
`RNAppAuth` will call on the given app's delegate via `[UIApplication sharedApplication].delegate`.
119-
Furthermore, `RNAppAuth` expects the delegate instance to conform to the protocol `RNAppAuthAuthorizationFlowManager`.
120-
Make `AppDelegate` conform to `RNAppAuthAuthorizationFlowManager` with the following changes to `AppDelegate.h`:
120+
###### For react-native >= 0.77
121+
122+
As of `[email protected]`, the `AppDelegate` template is now written in Swift.
123+
124+
In order to bridge to the existing Objective-C code that this package utilizes, you need to create a bridging header file. To do so:
125+
126+
1. Create a new file in your project called `AppDelegate+RNAppAuth.h`. (It can be called anything, but it must end with `.h`)
127+
2. Add the following code to the file:
128+
129+
```
130+
#import "RNAppAuthAuthorizationFlowManager.h"
131+
```
132+
133+
3. Ensure that your XCode "Build Settings" has the following `Objective-C Bridging Header` path set to the file you just created. For example, it make look something like: `$(SRCROOT)/AppDelegate+RNAppAuth.h`
134+
135+
4. Add the following code to `AppDelegate.swift` to support React Navigation deep linking and overriding browser behavior in the authorization process
136+
137+
```swift
138+
@main
139+
class AppDelegate: UIResponder, UIApplicationDelegate,
140+
RNAppAuthAuthorizationFlowManager {
141+
//... existing code...
142+
// Required by RNAppAuthAuthorizationFlowManager protocol
143+
public weak var authorizationFlowManagerDelegate:
144+
RNAppAuthAuthorizationFlowManagerDelegate?
145+
//... existing code...
146+
147+
// Handle OAuth redirect URL
148+
func application(
149+
_ app: UIApplication,
150+
open url: URL,
151+
options: [UIApplication.OpenURLOptionsKey: Any] = [:]
152+
) -> Bool {
153+
if let authorizationFlowManagerDelegate = self
154+
.authorizationFlowManagerDelegate
155+
{
156+
if authorizationFlowManagerDelegate.resumeExternalUserAgentFlow(with: url)
157+
{
158+
return true
159+
}
160+
}
161+
return false
162+
}
163+
}
164+
```
165+
166+
5. Add the following code to `AppDelegate.swift` to support universal links:
167+
168+
```swift
169+
170+
171+
func application(
172+
_ application: UIApplication,
173+
continue userActivity: NSUserActivity,
174+
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void
175+
) -> Bool {
176+
177+
// Handle Universal-Link–style OAuth redirects first
178+
if userActivity.activityType == NSUserActivityTypeBrowsingWeb,
179+
let delegate = authorizationFlowManagerDelegate,
180+
delegate.resumeExternalUserAgentFlow(with: userActivity.webpageURL)
181+
{
182+
return true
183+
}
184+
185+
// Fall back to React Native’s own Linking logic
186+
return RCTLinkingManager.application(
187+
application,
188+
continue: userActivity,
189+
restorationHandler: restorationHandler
190+
)
191+
}
192+
```
121193
122194
##### For react-native >= 0.68
123195
@@ -199,45 +271,8 @@ If you want to support universal links, add the following to `AppDelegate.m` und
199271
+ }
200272
```
201273
202-
#### Integration of the library with a Swift iOS project
203-
204-
The approach mentioned should work with Swift. In this case one should make `AppDelegate` conform to `RNAppAuthAuthorizationFlowManager`. Note that this is not tested/guaranteed by the maintainers.
205-
206-
Steps:
207-
208-
1. `swift-Bridging-Header.h` should include a reference to `#import "RNAppAuthAuthorizationFlowManager.h`, like so:
209-
210-
```h
211-
#import <React/RCTBundleURLProvider.h>
212-
#import <React/RCTRootView.h>
213-
#import <React/RCTBridgeDelegate.h>
214-
#import <React/RCTBridge.h>
215-
#import "RNAppAuthAuthorizationFlowManager.h" // <-- Add this header
216-
#if DEBUG
217-
#import <FlipperKit/FlipperClient.h>
218-
// etc...
219-
```
220-
221-
2. `AppDelegate.swift` should implement the `RNAppAuthAuthorizationFlowManager` protocol and have a handler for url deep linking. The result should look something like this:
222-
223-
```swift
224-
@UIApplicationMain
225-
class AppDelegate: UIApplicationDelegate, RNAppAuthAuthorizationFlowManager { //<-- note the additional RNAppAuthAuthorizationFlowManager protocol
226-
public weak var authorizationFlowManagerDelegate: RNAppAuthAuthorizationFlowManagerDelegate? // <-- this property is required by the protocol
227-
//"open url" delegate function for managing deep linking needs to call the resumeExternalUserAgentFlowWithURL method
228-
func application(
229-
_ app: UIApplication,
230-
open url: URL,
231-
options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
232-
return authorizationFlowManagerDelegate?.resumeExternalUserAgentFlow(with: url) ?? false
233-
}
234-
}
235-
```
236-
237274
### Android Setup
238275
239-
**Note:** for RN >= 0.57, you will get a warning about compile being obsolete. To get rid of this warning, use [patch-package](https://github.com/ds300/patch-package) to replace compile with implementation [as in this PR](https://github.com/FormidableLabs/react-native-app-auth/pull/242) - we're not deploying this right now, because it would break the build for RN < 57.
240-
241276
To setup the Android project, you need to add redirect scheme manifest placeholder:
242277
243278
To [capture the authorization redirect](https://github.com/openid/AppAuth-android#capturing-the-authorization-redirect),

0 commit comments

Comments
 (0)