How to create a button in live activity?How can not allow open app when click this button?
Now you can in iOS 17! Full documentation here.
Starting with iOS 17, iPadOS 17, or macOS 14, widgets and Live Activities can include buttons and toggles to offer specific app functionality without launching the app
For example, a Reminders widget allows people to mark a task as completed with a toggle. On a locked device, buttons and toggles are inactive and the system doesn’t perform actions unless a person authenticates and unlocks their device.
AppIntent
protocol and add it to your app target. For a Live Activity interactive, adopt the LiveActivityIntent
protocol@Parameter
property wrapper (must conform to AppEntity
)perform()
function, add code for the action you want to make available to the widget.Example:
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
struct SuperCharge: LiveActivityIntent {
static var title: LocalizedStringResource = "Emoji Ranger SuperCharger"
static var description = IntentDescription("All heroes get instant 100% health.")
func perform() async throws -> some IntentResult {
EmojiRanger.superchargeHeros()
return .result()
}
}
Notes
perform()
function is asynchronous.struct EmojiRangerWidgetEntryView: View {
var entry: SimpleEntry
@Environment(\.widgetFamily) var family
@ViewBuilder
var body: some View {
switch family {
// Code for other widget sizes.
case .systemLarge:
if #available(iOS 17.0, *) {
HStack(alignment: .top) {
Button(intent: SuperCharge()) {
Image(systemName: "bolt.fill")
}
}
.tint(.white)
.padding()
}
// ...rest of view
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With