Skip to content

Commit

Permalink
Change how UIApplication.open() launches a URL to fix error about mis…
Browse files Browse the repository at this point in the history
…sing FLAG_ACTIVITY_NEW_TASK (fixes https://github.com/orgs/skiptools/discussions/173)
  • Loading branch information
marcprux committed Jul 15, 2024
1 parent 05000e9 commit e2765f9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Sources/SkipUI/SkipUI/System/UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import Foundation
import OSLog

#if SKIP
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.AndroidUriHandler
import androidx.compose.ui.text.input.KeyboardType
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
Expand Down Expand Up @@ -57,11 +57,14 @@ let logger: Logger = Logger(subsystem: "skip.ui", category: "SkipUI") // adb log
public func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any] = [:]) async -> Bool {
#if SKIP
let context = ProcessInfo.processInfo.androidContext
let uriHandler = AndroidUriHandler(context)
do {
uriHandler.openUri(url.absoluteString)
let intent = Intent(Intent.ACTION_VIEW, android.net.Uri.parse(url.absoluteString))
// needed or else: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
return true
} catch {
logger.warning("UIApplication.launch error: \(error)")
return false
}
#else
Expand Down

0 comments on commit e2765f9

Please sign in to comment.