简体   繁体   中英

Objective-C AppAuth OIDAuthorizationService class in Swift, and Objective-C -> Swift translation

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM