swift – Shortcut Redirect In loop iOS – Shortcut

0
22
swift – Shortcut Redirect In loop iOS – Shortcut


I’ve implement shortcut. If goal app open then open my app utilizing shortcut.
So I’ve dealing with problem stepping into loop.

That is right behaviour,

  1. Open goal app
  2. Open my app
  3. Once more open goal app (now goal app already in background)
  4. Once more open my app.

We anticipated behaviours,

  1. Open goal app
  2. Open my app
  3. Once more open goal app (now goal app already in background)
  4. No must once more redirect to my app as a result of app is in background.

Right here is my code for shortcut.

import AppIntents
import SwiftUI
import SwiftUICore
import UIKit
//
// These would be the choices within the Shortcut motion to open a e-book or navigate to the library
enum NavigationType: String, AppEnum, CaseDisplayRepresentable {
    case library
    case e-book

    // This will likely be displayed because the title of the menu proven when selecting from the choices
  @out there(iOSApplicationExtension 16, *)
  static var typeDisplayRepresentation = TypeDisplayRepresentation(identify: "Navigation")
    
  @out there(iOSApplicationExtension 16.0, *)
  static var caseDisplayRepresentations: [Self:DisplayRepresentation] = [
        .library: DisplayRepresentation(title: "Library",
                                        subtitle: "Return to the home page",
                                        image: .init(systemName: "books.vertical")),
        .book: DisplayRepresentation(title: "Book",
                                     subtitle: "Navigate to a specific book",
                                     image: .init(systemName: "book"))
    ]
}
// Enum to characterize the static e-book choices
enum AppOption: String, AppEnum, CaseDisplayRepresentable {
    case youtube = "YouTube"
    case fb = "Fb"
    case twitter = "Twitter"

    @out there(iOSApplicationExtension 16.0, *)
    static var typeDisplayRepresentation = TypeDisplayRepresentation(identify: "App")

    @out there(iOSApplicationExtension 16.0, *)
    static var caseDisplayRepresentations: [Self: DisplayRepresentation] = [
        .youtube: DisplayRepresentation(title: "YouTube",
                                       subtitle: "com.google.ios.youtube"),
        .facebook: DisplayRepresentation(title: "Facebook",
                                        subtitle: "com.facebook.ios"
                                        ),
        .twitter: DisplayRepresentation(title: "Twitter",
                                       subtitle: "com.twitter.ios")
    ]
}


// Struct to characterize an app
struct App {
    let appName: String
    let bundleID: String
    let deepLinking:String
}

// Pattern app knowledge (you possibly can load this dynamically sooner or later)
let appsDetail = [
  App(appName: "YouTube", bundleID: "com.google.ios.youtube",deepLinking:"youtube://"),
    App(appName: "Facebook", bundleID: "com.facebook.ios",deepLinking:"fb://"),
    App(appName: "Twitter", bundleID: "com.twitter.ios",deepLinking:"twitter://")
]

@out there(iOSApplicationExtension 16.0, *)
struct OpenBook: AppIntent {
  
  // Title o  f the motion within the Shortcuts app
  @out there(iOSApplicationExtension 16, *)
  static var title: LocalizedStringResource = "Open App"
  // Description of the motion within the Shortcuts app
  @out there(iOSApplicationExtension 16.0, *)
  static var description: IntentDescription = IntentDescription("This motion will open the chosen e-book within the Booky app or navigate to the house library.", categoryName: "Navigation")
  // This opens the host app when the motion is run
  static var openAppWhenRun = true
  
  @Parameter(title: "App", description: "The e-book to open in Booky", requestValueDialog: IntentDialog("Which e-book would you prefer to open?"))
  var apps: AppOption
  
  // An enum parameter
  @Parameter(title: "Navigation", description: "Select whether or not to open a e-book or navigate to Booky's library", default: .e-book, requestValueDialog: IntentDialog("What would you prefer to navigate to?"))
  var navigation: NavigationType
  
  // How the abstract will seem within the shortcut motion.
  static var parameterSummary: some ParameterSummary {
    Abstract("Open (.$apps)")
  }
  
  @MainActor // <-- embody if the code must be run on the principle thread
func carry out() async throws -> some IntentResult {
     guard let selectedBook = appsDetail.first(the place: { $0.appName == apps.rawValue }) else {
                 throw NSError(area: "OpenBookError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Book not found."])
             }
             let userInfo: [String: Any] = ["selectedBook": [
                 "appName": selectedBook.appName,
                 "bundleId": selectedBook.bundleID,
                 "deepLinking": selectedBook.deepLinking,
             ]]
     
     NotificationCenter.default.publish(identify: NSNotification.Title("OpenBookCallback"), object: nil, userInfo: userInfo)
     
     return .outcome()
   }
 }

We anticipated behaviours,

  1. Open goal app
  2. Open my app
  3. Once more open goal app (now goal app already in background)
  4. No must once more redirect to my app as a result of app is in background.

LEAVE A REPLY

Please enter your comment!
Please enter your name here