Sunday, July 7, 2024
HomeiOS Developmentswift - iOS 18 Management Widget that opens a URL

swift – iOS 18 Management Widget that opens a URL


I have already got an iOS 17 App Intent that works with a URL:

@accessible(iOS 16, *)
struct MyAppIntent: AppIntent {
    static let title : LocalizedStringResource = "My App Inent"
    static let openAppWhenRun   : Bool = true
    
    @MainActor
    func carry out() async throws -> some IntentResult{
        await UIApplication.shared.open(URL(string: "myapp://myappintent")!)
        return .end result()
    }
}

Now, with iOS 18 and Management Widgets, I wish to create a Management Widget button that merely opens the app with the identical URL. Nevertheless, UIApplication code is just not allowed inside extensions. For this, Apple says to make use of OpenIntent which is proven right here:

Documentation Hyperlink

Apple Pattern Code from the hyperlink:

import AppIntents

struct LaunchAppIntent: OpenIntent {
    static var title: LocalizedStringResource = "Launch App"
    @Parameter(title: "Goal")
    var goal: LaunchAppEnum
}


enum LaunchAppEnum: String, AppEnum {
    case timer
    case historical past


    static var typeDisplayRepresentation = TypeDisplayRepresentation("Productiveness Timer's app screens")
    static var caseDisplayRepresentations = [
        LaunchAppEnum.timer : DisplayRepresentation("Timer"),
        LaunchAppEnum.history : DisplayRepresentation("History")
    ]
}

WWDC session video about this doesn’t cowl this specific methodology intimately and likewise this pattern code is a bit complicated.

So how can I alter this code to simply open the app with a URL?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments