在swift中启动带有url的应用程序,然后在启动该应用程序之前调用API



这有点难,如果标题不正确,我深表歉意。我不知道如何用词来表达这个标题。

本质上,我在iOS中使用Jitsi SDK,我们将其设置为使用JWT进行身份验证和识别主机/客户。我的问题出现在使用URL启动应用程序时。方法。。。

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

当它启动时,它会将房间编号作为URL的一部分。这一切都是有道理的。我的问题是我需要调用API来";签入";用户,并检索在服务器上生成的jwt。上面的函数返回了应用程序是否应该启动以及所有的爵士乐,在Jitsi文档中,它显示返回应该是…

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

但是我不希望它那样做。我需要做一个API调用,我的API调用的回调会有我需要的jwt,然后我想让它正常打开应用程序,这样我就可以自己处理加入会议了。

我想我的主要问题是…如果我做一个API调用,它有一个回调,将用所需的参数启动应用程序,但我只是从该函数返回false,它能正常工作吗?

我知道这听起来可能很困惑,所以这里有一个我当时的想法。。。

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
}

我觉得这会是一个问题,因为函数会在Api调用完成之前得到返回,因为返回不能等待Api调用完成,但我不确定如何处理这种情况。这是当使用特定URL启动应用程序时在AppDelegate中调用的方法,当用户单击链接加入会议时会发生这种情况。那么,如果应用程序是用特定的URL启动的,那么有没有方法进行API调用,然后进行相应的处理?我是否走在正确的道路上,理论上上述内容应该有效?我只是有点迷路了,需要一个比我更擅长swift的人的建议。

谢谢你通读,如果标题不正确,我再次道歉。这很难解释,我不确定标题应该是什么。

当调用应用程序(open,options(时,必须立即返回bool。如果您对JWT令牌的服务器进行异步调用,然后返回false,那么它将立即停止。

所以,只要回归真实,继续你正在做的事情。查看文档后https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application不管怎样,如果应用程序还没有启动,你似乎正在启动它。


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
}

最新更新