0

I wonder if I can expose my func navigateToLoginWidget to React Native. So that it can be triggered from RN.

I have managed to change the Objective-C template that comes with React Native to Swift like so:

import Foundation
import IBMCloudAppID
import BMSCore

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  
  var window: UIWindow?
  var bridge: RCTBridge!

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    // initializing App ID SDK
    let region = AppID.REGION_SOMEWHERE
    let backendGUID = "MY_TENANT_ID_FROM_IBM"
    AppID.sharedInstance.initialize(tenantId: backendGUID, region: region)
    
    // Initializing React Native front-end with Swift instead of Obj-c template
    let jsCodeLocation: URL

    jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index.js", fallbackResource:nil)
    let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "MY_PROJECT_NAME", initialProperties: nil, launchOptions: launchOptions)
    let rootViewController = UIViewController()
    rootViewController.view = rootView

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = rootViewController
    self.window?.makeKeyAndVisible()

    return true
  }
  
  func application(_ application: UIApplication, open url: URL, options :[UIApplication.OpenURLOptionsKey : Any]) -> Bool {
      return AppID.sharedInstance.application(application, open: url, options: options)
  }
  
  func navigateToLoginWidget(_ sender : Any) {
    print("clicked")
    AppID.sharedInstance.loginWidget?.launch(delegate: SigninDelegate)
  }
  
}

I would normally have this function in another module called SigninDelegate.swift, but I have included it in the same class for explanatory purposes.

Yagooza
  • 53
  • 1
  • 4

1 Answers1

0

According to the official documentation:

Swift doesn't have support for macros so exposing it to React Native requires a bit more setup but works relatively the same.

So yes, it is possible, but some setup is needed. Check the docs in the link above for the steps you need to follow to set it up.

romin21
  • 1,532
  • 1
  • 14
  • 35
  • The link you provided uses Objective-C to expose it to React Native. I am aware of this setup, but what I want to now is whether I can expose Swift "without" Objc and how. – Yagooza Mar 19 '20 at 18:56
  • I don't think that is possible. – romin21 Mar 20 '20 at 08:49
  • 1
    It seems as if you were right. I can't find any solution on working with pure Swift, or anybody that can help me with that. Though my problem was that I wanted it to use Swift in the Appdelegate so that I could configure my app to process redirects through App ID. By having my AppDelegate written in Swift and using the documentation I managed to make it work. – Yagooza Mar 25 '20 at 12:08