Skip to content

Commit c6353ca

Browse files
author
Paul-Brittain
committed
Swift installation
1 parent d3407aa commit c6353ca

File tree

1 file changed

+78
-27
lines changed

1 file changed

+78
-27
lines changed

README.md

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -189,44 +189,44 @@ To setup the iOS project, you need to perform three steps:
189189
2. [Register redirect URL scheme](#register-redirect-url-scheme)
190190
3. [Define openURL callback in AppDelegate](#define-openurl-callback-in-appdelegate)
191191

192-
#### Install native dependencies
192+
##### Install native dependencies
193193

194194
This library depends on the native [AppAuth-ios](https://github.com/openid/AppAuth-iOS) project. To
195195
keep the React Native library agnostic of your dependency management method, the native libraries
196196
are not distributed as part of the bridge.
197197

198198
AppAuth supports three options for dependency management.
199199

200-
##### CocoaPods
200+
1. **CocoaPods**
201201

202-
With [CocoaPods](https://guides.cocoapods.org/using/getting-started.html), add the following line to
203-
your `Podfile`:
202+
With [CocoaPods](https://guides.cocoapods.org/using/getting-started.html), add the following line to
203+
your `Podfile`:
204204

205-
pod 'AppAuth', '>= 0.91'
205+
pod 'AppAuth', '>= 0.91'
206206

207-
Then run `pod install`. Note that version 0.91 is the first of the library to support iOS 11.
207+
Then run `pod install`. Note that version 0.91 is the first of the library to support iOS 11.
208208

209-
##### Carthage
209+
2. **Carthage**
210210

211-
With [Carthage](https://github.com/Carthage/Carthage), add the following line to your `Cartfile`:
211+
With [Carthage](https://github.com/Carthage/Carthage), add the following line to your `Cartfile`:
212212

213-
github "openid/AppAuth-iOS" "master"
213+
github "openid/AppAuth-iOS" "master"
214214

215-
Then run `carthage bootstrap`.
215+
Then run `carthage bootstrap`.
216216

217-
##### Static Library
217+
3. **Static Library**
218218

219-
You can also use [AppAuth-iOS](https://github.com/openid/AppAuth-iOS) as a static library. This
220-
requires linking the library and your project and including the headers. Suggested configuration:
219+
You can also use [AppAuth-iOS](https://github.com/openid/AppAuth-iOS) as a static library. This
220+
requires linking the library and your project and including the headers. Suggested configuration:
221221

222-
1. Create an XCode Workspace.
223-
2. Add `AppAuth.xcodeproj` to your Workspace.
224-
3. Include libAppAuth as a linked library for your target (in the "General -> Linked Framework and
225-
Libraries" section of your target).
226-
4. Add `AppAuth-iOS/Source` to your search paths of your target ("Build Settings -> "Header Search
227-
Paths").
222+
1. Create an XCode Workspace.
223+
2. Add `AppAuth.xcodeproj` to your Workspace.
224+
3. Include libAppAuth as a linked library for your target (in the "General -> Linked Framework and
225+
Libraries" section of your target).
226+
4. Add `AppAuth-iOS/Source` to your search paths of your target ("Build Settings -> "Header Search
227+
Paths").
228228

229-
#### Register redirect URL scheme
229+
##### Register redirect URL scheme
230230

231231
If you intend to support iOS 10 and older, you need to define the supported redirect URL schemes in
232232
your `Info.plist` as follows:
@@ -249,7 +249,7 @@ your `Info.plist` as follows:
249249
* `CFBundleURLSchemes` is an array of URL schemes your app needs to handle. The scheme is the
250250
beginning of your OAuth Redirect URL, up to the scheme separator (`:`) character.
251251

252-
#### Define openURL callback in AppDelegate
252+
##### Define openURL callback in AppDelegate
253253

254254
You need to have a property in your AppDelegate to hold the auth session, in order to continue the
255255
authorization flow from the redirect. To add this, open `AppDelegate.h` in your project and add the
@@ -266,15 +266,19 @@ following lines:
266266

267267
The authorization response URL is returned to the app via the iOS openURL app delegate method, so
268268
you need to pipe this through to the current authorization session (created in the previous
269-
instruction). To do this, open `AppDelegate.m` and add an import statement:
269+
instruction).
270270

271-
```objective-c.
271+
##### Add a current Authorization session
272+
273+
To do this, open `AppDelegate.m` and add an import statement:
274+
275+
```Objective-C
272276
#import <AppAuth/AppAuth.h>
273277
```
274278

275279
And in the bottom of the class, add the following handler:
276280

277-
```objective-c.
281+
```Objective-C
278282
- (BOOL)application:(UIApplication *)app
279283
openURL:(NSURL *)url
280284
options:(NSDictionary<NSString *, id> *)options {
@@ -286,14 +290,61 @@ And in the bottom of the class, add the following handler:
286290
}
287291
```
288292

293+
#### Integration of the library with a Swift iOS project
294+
295+
Until a better solution is available, we must use `react-native-app-auth` as a Static Library. This is due to the fact that the library is calling `AppDelegate.swift`.
296+
297+
1. Unlink `react-native-app-auth` from your projects `Libraries/`.
298+
299+
2. Manually copy the `RNAppAuth.h` and `RNAppAuth.m` files from the library folder in your `node_modules/` into your project folder.
300+
301+
3. In `RNAppAuth.m` add a new import:
302+
```Objective-C
303+
#import "<YouProjectName>-Swift.h"
304+
```
305+
306+
4. In your project's `AppDelegate.swift`, expose your function to `Objective-C` by annotating the AppDelegate with:
307+
```Swift
308+
@objc(AppDelegate)
309+
```
310+
311+
5. Add the following code just after the class declaration:
312+
```Swift
313+
var currentAuthorizationFlow: OIDAuthorizationFlowSession?
314+
```
315+
316+
6. At the bottom of your class add the following code:
317+
318+
```Swift
319+
func application(
320+
_ app: UIApplication,
321+
open url: URL,
322+
options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
323+
324+
if currentAuthorizationFlow!.resumeAuthorizationFlow(with: url){
325+
currentAuthorizationFlow = nil
326+
return true
327+
}
328+
329+
return false;
330+
}
331+
```
332+
This is a translation of the following `Objective-C` code provided [above](#add-a-current-authorization-session)
333+
334+
**Warning:**
335+
336+
You may need to perform Step 4 and compile your project so that the hidden bridging header file is created. If this file is not created and you follow the rest of the steps then you may fall into a chicken before the egg problem where the project is failing to build because of the missing header file and the header file won't be created because the build is failing.
337+
338+
339+
289340
### Android Setup
290341

291342
To setup the Android project, you need to perform two steps:
292343

293344
1. [Install Android support libraries](#install-android-support-libraries)
294345
2. [Add redirect scheme manifest placeholder](#add-redirect-scheme-manifest-placeholder)
295346

296-
#### Install Android support libraries
347+
##### Install Android support libraries
297348

298349
This library depends on the [AppAuth-Android](https://github.com/openid/AppAuth-android) project.
299350
The native dependencies for Android are automatically installed by Gradle, but you need to add the
@@ -320,7 +371,7 @@ correct Android Support library version to your project:
320371
}
321372
```
322373
323-
#### Add redirect scheme manifest placeholder
374+
##### Add redirect scheme manifest placeholder
324375
325376
To
326377
[capture the authorization redirect](https://github.com/openid/AppAuth-android#capturing-the-authorization-redirect),
@@ -362,7 +413,7 @@ try {
362413

363414
See example configurations for different providers below.
364415

365-
### Note about client secrets
416+
#### Note about client secrets
366417

367418
Some authentication providers, including examples cited below, require you to provide a client secret. The authors of the AppAuth library
368419

0 commit comments

Comments
 (0)