Skip to content

Commit

Permalink
Refactor IOSLinkHandler
Browse files Browse the repository at this point in the history
Trigger `openURL` in async, since it sometimes freezes the app and causes app hanging exception
  • Loading branch information
msasikanth committed Nov 5, 2023
1 parent 05c696f commit 48c09d9
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import platform.SafariServices.SFSafariViewController
import platform.UIKit.UIApplication
import platform.UIKit.UIModalPresentationPageSheet
import platform.UIKit.UIViewController
import platform.darwin.dispatch_async
import platform.darwin.dispatch_get_main_queue

@Inject
@ActivityScope
Expand All @@ -36,16 +38,29 @@ class IOSLinkHandler(

override suspend fun openLink(link: String) {
val browserType = settingsRepository.browserType.first()
val url = NSURL(string = link)

when (browserType) {
BrowserType.Default -> {
UIApplication.sharedApplication().openURL(NSURL(string = link))
val canOpenUrl = UIApplication.sharedApplication().canOpenURL(url)
if (canOpenUrl) {
dispatch_async(dispatch_get_main_queue()) {
UIApplication.sharedApplication().openURL(url)
}
} else {
inAppBrowser(url)
}
}
BrowserType.InApp -> {
val safari = SFSafariViewController(NSURL(string = link))
safari.modalPresentationStyle = UIModalPresentationPageSheet

uiViewControllerProvider().presentViewController(safari, animated = true, completion = null)
inAppBrowser(url)
}
}
}

private fun inAppBrowser(url: NSURL) {
val safari = SFSafariViewController(url)
safari.modalPresentationStyle = UIModalPresentationPageSheet

uiViewControllerProvider().presentViewController(safari, animated = true, completion = null)
}
}

0 comments on commit 48c09d9

Please sign in to comment.