skip to Main Content

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

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

Now, with iOS 18 and Control Widgets, I want to create a Control Widget button that simply opens the app with the same URL. However, UIApplication code is not allowed within extensions. For this, Apple says to use OpenIntent which is shown here:

Documentation Link

Apple Sample Code from the link:

import AppIntents

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


enum LaunchAppEnum: String, AppEnum {
    case timer
    case history


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

WWDC session video about this does not cover this particular method in detail and also this sample code is a bit confusing.

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

2

Answers


  1. You just need to setup your ControlWidget first and then link your OpenIntent.

    Something like:

    struct OpenAppnButton: ControlWidget {
        var body: some ControlWidgetConfiguration {
            
            StaticControlConfiguration(
                kind: "your_id"
            ) {
                ControlWidgetButton(action: LaunchAppIntent()) { // <-- HERE
                    Label("Something, image: "arrow.up")
                }
            }
            .displayName("Open app")
        }
    }
    
    Login or Signup to reply.
  2. update your existing MyAppIntent to make use of OpenIntent.

    import AppIntents
    import UIKit
    
    @available(iOS 18, *)
    struct MyAppIntent: AppIntent {
        static var title: LocalizedStringResource = "My App Intent"
    
        @MainActor
        func perform() async throws -> some IntentResult {
            guard let url = URL(string: "myapp://myappintent") else {
                throw IntentError(.unknown)
            }
            return .openURL(url)
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search