iOS18 Control Widget dynamic switch intent

Objective: When the app is running, directly operate the app without launching it. When the app is not started, wake up the app to perform the corresponding operation.

Premise: AppIntent's openAppWhenRun can dynamically control whether the app is launched, but the variable itself cannot be changed.

Therefore, the idea is to refresh the widget when the app is started or terminated, displaying different content (binding different intents) to achieve the goal.

However, there are persistent issues during code implementation.


struct ControlWidgetContent: ControlWidgetTemplate {

    

    var body: some ControlWidgetTemplate {

        if (applaunched) {

            return ControlWidgetButton(action: Aintent()) {

                Label("xxxx", systemImage: "calendar")

            }

        } else {

            return ControlWidgetButton(action: Bintent()) {

                Label("xxxx", systemImage: "calendar")

            }

        }

    }

}



@available(iOS 18.0, *)

struct ControlWidgetContent: ControlWidget {

    let kind: String = "ControlWidgetInterface"



    var body: some ControlWidgetConfiguration {

        StaticControlConfiguration(

            kind: kind

        ) {

            ControlWidgetContent()

        }.displayName("xxxx")

    }

}

it preports error:

Function declares an opaque return type 'some ControlWidgetTemplate', but the return statements in its body do not have matching underlying types

Return statement has underlying type 'ControlWidgetButton<Label<Text, Image>, ControlWidgetButtonDefaultActionLabel, PlayDayRcmdIntent>'

What's a ControlWidgetTemplate? I can't find that in the Developer app.

The error is because you're returning a ControlWidgetButton in a struct that says it's a ControlWidgetTemplate type.

It looks like you just want to return the right 'thing' in the ControlWidget struct. Something like this: (tweak it to get it to do what you want)

func chooseControlWidgetContent() -> ControlWidgetButton {
	if (applaunched) {
		return ControlWidgetButton(action: Aintent()) {
			Label("xxxx", systemImage: "calendar")
		}

	} else {
		return ControlWidgetButton(action: Bintent()) {
			Label("xxxx", systemImage: "calendar")
		}
	}
}



@available(iOS 18.0, *)
struct ControlWidgetContent: ControlWidget {
	let kind: String = "ControlWidgetInterface"

	var body: some ControlWidgetConfiguration {
		StaticControlConfiguration(
			kind: kind
		) {
			chooseControlWidgetContent()
		}.displayName("xxxx")
	}
}

Did you solve this problem?

iOS18 Control Widget dynamic switch intent
 
 
Q