0

I need to declare a variable of type OIDAuthorizationService, from the AppAuth (https://github.com/openid/AppAuth-iOS) library, in Swift while translating the following Objective-C to Swift to use an Objective-C library in a project.

Pre-translated objective-c

.h

+ @protocol OIDAuthorizationFlowSession;

  @interface AppDelegate : UIResponder <UIApplicationDelegate>
+ @property(nonatomic, strong, nullable) 
id<OIDAuthorizationFlowSession> currentAuthorizationFlow;
  @property (nonatomic, strong) UIWindow *window;
  @end

.m

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<NSString *, id> *)options {
  if ([_currentAuthorizationFlow resumeAuthorizationFlowWithURL:url]) {
    _currentAuthorizationFlow = nil;
    return YES;
  }
  return NO;
}

So far I have the following translation:

  var currentAuthorizationFlow: OIDAuthorizationFlowSession?

  ...

  func application(_ application: UIApplication, openURL: NSURL,
                       didFinishLaunchingWithOptions launchOptions: 
 [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if currentAuthorizationFlow!.resumeAuthorizationFlow(with: openURL as URL) {
      return true
    }
    return false
  }

Is this code correct?

Error is: Use of undeclared type: 'OIDAuthorizationFlowSession', as espected, how do I do this?

Much appreciated

Paul Brittain
  • 319
  • 2
  • 17

1 Answers1

2

I understand that OIDAuthorizationFlowSession protocol is defined in Objective-C and you're trying to use it in Swift, if that's the case then you need a Bridging-Header.h, there you can import the corresponding header for your OIDAuthorizationFlowSession.

How do you create a Bridging Header file? Well it should be created automatically when you create a new swift file, if that's not the case, take a look here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

If the easy way doesn't work, see this one: Xcode not automatically creating bridging header?

LE: if you integrated the AppAuth-iOS as a pod, here's what's working 100%:

import UIKit
import AppAuth

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    var currentAuthorizationFlow: OIDAuthorizationFlowSession?

    func application(_ application: UIApplication, openURL: NSURL,
                     didFinishLaunchingWithOptions launchOptions:
        [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if currentAuthorizationFlow!.resumeAuthorizationFlow(with: openURL as URL) {
            return true
        }
        return false
    }

So don't forget the "import AppAuth" part.

Mihai Erős
  • 1,129
  • 1
  • 11
  • 18
  • 1
    I had to import AppAuth in the bridge file, not in the AppDelegate file. I also had to open the xcworkspace not the xcodeproj, thanks for taking the time to answer Mihai -- a great help! – Paul Brittain Mar 28 '18 at 12:53