I'm trying to program a shortcut that automatically opens my app once another app like "Instagram or Tiktok" is opened. After my app is opened you can open Instagram through it. The problem is that once instagram is opened the shortcut gets triggered again because "openAppWhenRun: Bool = true". How can I make openAppWhenRun dynamic to set it to false, once the user opens Instagram through my app? Here is my code:
struct NotifyFriendsScreen: AppIntent {
static var title: LocalizedStringResource = "Connect With Friends"
static var description = IntentDescription("This will activate tell-them to notify your friends. Then you can proceed to app.")
static var openAppWhenRun: Bool = false
@Parameter(title: "❗️Choose App❗️", optionsProvider: SupportedApps())
var selectedApp: String
@MainActor
func perform() async throws -> some IntentResult & OpensIntent {
if UserDefaults.standard.bool(forKey: "openTellThem") == true {
showTargetViewController()
return .result(opensIntent: openApp())
} else {
return .result(opensIntent: dontOpenApp())
}
}
As you can see I tried to fix this problem in the "perform()" method, but this gives me following error: "Function declares an opaque return type 'some IntentResult & OpensIntent', but the return statements in its body do not have matching underlying types". I also tried to make "openAppWhenRun" dynamic, but that doesn't work:
static var openAppWhenRun: Bool {
UserDefaults.standard.bool(forKey: "openTellThem")
...
}
I'm trying to program a shortcut that automatically opens my app once another app like "Instagram or Tiktok" is opened. After my app is opened you can open Instagram through it. The problem is that once instagram is opened the shortcut gets triggered again because "openAppWhenRun: Bool = true". How can I make openAppWhenRun dynamic to set it to false, once the user opens Instagram through my app? Here is my code:
struct NotifyFriendsScreen: AppIntent {
static var title: LocalizedStringResource = "Connect With Friends"
static var description = IntentDescription("This will activate tell-them to notify your friends. Then you can proceed to app.")
static var openAppWhenRun: Bool = false
@Parameter(title: "❗️Choose App❗️", optionsProvider: SupportedApps())
var selectedApp: String
@MainActor
func perform() async throws -> some IntentResult & OpensIntent {
if UserDefaults.standard.bool(forKey: "openTellThem") == true {
showTargetViewController()
return .result(opensIntent: openApp())
} else {
return .result(opensIntent: dontOpenApp())
}
}
As you can see I tried to fix this problem in the "perform()" method, but this gives me following error: "Function declares an opaque return type 'some IntentResult & OpensIntent', but the return statements in its body do not have matching underlying types". I also tried to make "openAppWhenRun" dynamic, but that doesn't work:
static var openAppWhenRun: Bool {
UserDefaults.standard.bool(forKey: "openTellThem")
...
}
Share
Improve this question
asked Feb 10 at 11:04
user29283103user29283103
114 bronze badges
2
- UserDefaults are persistent variables and can be changed and reused in any part of the app. It is one of the most common ways of storing preferences and customisations. You can check the official documentation here for details on how to modify the values: developer.apple/documentation/foundation/userdefaults – Elmar Commented Feb 10 at 11:16
- Yes I can change the Userdefault as following: if UserDefaults.standard.bool(forKey: "openTellThem") == true { return .result(opensIntent: openApp()) } else { return .result(opensIntent: dontOpenApp()) } } This will create following error for the perform() function: Function declares an opaque return type 'some IntentResult', but the return statements in its body do not have matching underlying types – user29283103 Commented Feb 10 at 13:59
2 Answers
Reset to default 0Instead of making openAppWhenRun dynamic, store a flag in UserDefaults I think this way you will prevent the the shortcut from triggering
I've similar issues in one of my POC.
The problem occurs because of the recursive app opening behavior. This way we can fix it, while returning IntentResult type instead of OpaqueType.
struct NotifyFriendsScreen: AppIntent {
static var title: LocalizedStringResource = "Connect With Friends"
static var description = IntentDescription("This will activate tell-them to notify your friends. Then you can proceed to app.")
@Parameter(title: "❗️Choose App❗️", optionsProvider: SupportedApps())
var selectedApp: String
@MainActor
func perform() async throws -> IntentResult & OpensIntent {
if UserDefaults.standard.bool(forKey: "openTellThem") {
showTargetViewController()
UserDefaults.standard.set(false, forKey: "openTellThem")
return .result(opensIntent: openApp())
} else {
return .result(opensIntent: dontOpenApp())
}
}
}
I hope this will fix your issue.