Neglected Sanity
Neglected Sanity

Reputation: 1934

Launching an app with a url in swift then calling an API before launching the app

This is a bit of a tough one and I apologize if the title is incorrect. I wasn't sure how to word the title to have it make sense.

Essentially I am using the Jitsi SDK in iOS and we have it setup to use JWT for authentication and identifying host/guest. My problem comes in when the app is launched with a URL. The method...

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {...

When that is launched it has the room number as part of the URL. That all makes sense. My problem is I need to call an API to "Checkin" the user and retrieve the jwt that is generated on the server. The above function has a return on whether or not the app should launch and all that jazz and in the Jitsi documentation it shows the return should be...

return JitsiMeet.sharedInstance().application(app, open: finalURL, options: options)

However I don't want it to do that. I need to make an API call, the callback of my api call will have the jwt that I need, then I want it to open the app normally so I can handle joining the conference on my own.

I guess my main question is.. If I make an API call that has a callback that will launch the application with the needed arguments, but then I just return false from that function, will it work correctly?

I know that may sound confusing, so here is a quick snippet of what I was thinking...

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
  guard NSURLComponents(url: url resolvingAgainstBaseURL: true) != nil else {
    return false
  }
  let room = url.lastPathComponent
  if let finalURL = URL(string: 'myserverhere/' + room) {
    callCheckinAPI(room: room) { (res) in
      if(res.success) {
        DispatchQueue.main.async {
          self.launchMainApp(room)
        }
      } else {
        //not sure how to return false from here
      }
    }
    return false
  }
  return false
}

I feel like that would be a problem because the function would get its return before the Api call is completed, since the return can't wait for the api call to finish, but I'm not sure how I could go about handling this scenario. That is the method that gets called in AppDelegate when the app is launched with a specific URL, which is what happens when a user clicks on the link to join the meeting. So, is there a way to make an API call if the app is launched with a specific URL, and then handle it accordingly? Am I on the right path and the above stuff should, theoretically, work? I am just kind of lost and need the advice of someone who is much better with swift than I am.

Thank you for reading through, and I apologize again if the title is not correct. It was hard to explain and I wasn't sure what the title should be.

Upvotes: 1

Views: 1455

Answers (1)

Yogurt
Yogurt

Reputation: 3045

When application(open,options) is called you have to return a bool right away. If you make an asynchronous call to the server for the JWT token and then return false it'll just stop right there.

So just return true and proceed with what you're doing. After looking at the documentation https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application it seems like you're on your way to launching the app anyways if it's not already launched.


func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
  guard NSURLComponents(url: url resolvingAgainstBaseURL: true) != nil else {
    return false
  }

  let room = url.lastPathComponent

  // new made up function
  guard roomExists(url) else {
    return false
  }

  if let finalURL = URL(string: 'myserverhere/' + room) {
    callCheckinAPI(room: room) { (res) in
      if(res.success) {
        DispatchQueue.main.async {
          // the view controller should already be attached to the window by this point
          // so inside this function it should have a function to connect to the room
          mainviewcontroller.connect(to:room)
          // self.launchMainApp(room)
        }
      } else {
         // show alert that something went wrong
      } 
    }
  }

  return true
}

Upvotes: 1

Related Questions