Tuesday, April 16, 2024
HomeiOS Developmentios - SwiftUI Widget - Is it potential to open an arbitrary...

ios – SwiftUI Widget – Is it potential to open an arbitrary URL when the consumer faucets on a Widget (both through widget URL or AppIntent)


I am seeking to create a widget for a particular use case. I might like to have the ability to have a consumer faucet on a button (or the widget itself), and place a telephone name. Importantly, the Widget should work whereas the telephone is locked, and never launch the app.

From what I can inform, the one solution to place a telephone name is to open a URL with the next format:

tel://<phone-number>

The issue is I can not open the URL in a Widget. Here is what I’ve tried:

  1. I’ve handed the URL to the .widgetURL methodology like so
struct BeaconWidgetEntryView : View {
    let entry: Supplier.Entry

    var physique: some View {
        VStack {
            Textual content(entry.title)
                .daring()
            Textual content(entry.phoneNumber)
                .foregroundStyle(.secondary)
        }
        .widgetURL(URL(string: "tel://(entry.phoneNumber)")!)
    }
}

This simply launches the app, passing on this URL. No good.

  1. I’ve tried to open the URL in an AppIntent (and carry out the motion on a button faucet), however UIApplication.shared shouldn’t be out there in app extensions.
struct CallContactIntent: AppIntent {
    static var title: LocalizedStringResource = "Name Contact"
    static var authenticationPolicy: IntentAuthenticationPolicy = .alwaysAllowed
    static var openAppWhenRun: Bool = false
    static var isDiscoverable: Bool = false

    @Parameter(title: "Cellphone Quantity")
    var phoneNumber: String

    init() { }

    init(phoneNumber: String) {
        self.phoneNumber = phoneNumber
    }

    func carry out() async throws -> some IntentResult {
        guard let callNumber = URL(string: "tel://" + phoneNumber) else { return .consequence() }

        await UIApplication.shared.open(callNumber) // Not out there in extensions

        return .consequence()
    }
}
  1. I’ve seemed on the openURL atmosphere variable in SwiftUI, however I can not cross that into the AppIntent, since it isn’t sendable.
struct BeaconWidgetEntryView : View {
    var entry: Supplier.Entry

    @Setting(.openURL) personal var openURL // Undecided what I can do with this...

    var physique: some View {
        VStack {
            Textual content(entry.title)
                .daring()
            Textual content(entry.phoneNumber)
                .foregroundStyle(.secondary)
        }
    }
}

That is intently associated to this query, however not one of the options there work for me.

The explanation I have never but given up on that is Apple’s Widgets are in a position to do that. I created a Shortcut that calls a telephone quantity, and I can name the quantity on faucet whereas the telephone is locked. Is that this one other case of Apple giving their very own apps entry to personal APIs?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments