Siri快捷方式运行命令

时间:2019-12-03 06:24:20

标签: ios swift sirikit

所以我的意图在一定程度上起作用。就像我的意思是siri repsonse表示“完成”或“确定”。当我问Siri以下命令时

列表项

:“嘿siri Play DRN1”

Siri响应已完成。 (何时应该现在打开)

但是该应用程序什么也不做。

现在我注意到我尚未设置以下屏幕,或者错过了一步?

custom intents response

AppDelegate.swift

 //
    //  AppDelegate.swift
    //  DRN1
    //
    //  Created by Russell Harrower on 24/11/19.
    //  Copyright © 2019 Russell Harrower. All rights reserved.
    //
    import UIKit
    @UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

           if userActivity.activityType == "PlayDRN1Intent" {
               print("YES I RAN")
           }

           return true
       }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {

        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    }

FirstViewController.swift

//
//  FirstViewController.swift
//  DRN1
//
//  Created by Russell Harrower on 24/11/19.
//  Copyright © 2019 Russell Harrower. All rights reserved.
//

import UIKit
import Intents
import AVKit
import Kingfisher

extension UIImageView {
      func setImage(with urlString: String){
          guard let url = URL.init(string: urlString) else {
              return
          }
          let resource = ImageResource(downloadURL: url, cacheKey: urlString)
          var kf = self.kf
          kf.indicatorType = .activity
          self.kf.setImage(with: resource)
      }
  }
struct Nowplayng: Decodable{
    let data: [Data]
}

struct Data: Decodable{
    let track: Trackinfo
}
struct Trackinfo: Decodable {
    let title: String
    let artist: String
    let imageurl: String
}

class FirstViewController: UIViewController {



      var timer = Timer()
      var rplayed = false;
      var playButton = true;

      @IBOutlet weak var artist: UILabel!
      @IBOutlet weak var song: UILabel!
      @IBOutlet weak var imageurl: UIImageView!

      override func viewDidLoad() {
          let intent = PlayDRN1Intent()
          intent.playdrn1 = "DRN1"

          let interaction = INInteraction(intent: intent, response: nil)
          interaction.donate { (error) in
              print(error ?? "error")
          }


        MusicPlayer.shared.startBackgroundMusic()
        // Do any additional setup after loading the view.
          self.artist.textAlignment = .center
          self.song.textAlignment = .center
          scheduledTimerWithTimeInterval()
          self.nowplaying()


      }




    func scheduledTimerWithTimeInterval(){
        // Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
        timer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(self.nowplaying), userInfo: nil, repeats: true)
    }


     @objc func nowplaying(){
          let jsonURLString = "https://api.drn1.com.au/station/playing"
                  guard let feedurl = URL(string: jsonURLString) else { return }

                  URLSession.shared.dataTask(with: feedurl) { (data,response,err)
                      in

                      guard let data = data else { return }

                      do{
                          let nowplaying = try JSONDecoder().decode(Nowplayng.self, from: data)

                          nowplaying.data.forEach {

                              DispatchQueue.main.async {


                              self.artist.text = nowplaying.data.first?.track.artist
                              self.song.text = nowplaying.data.first?.track.title

                              }


                              print($0.track.title)

                              if var strUrl = nowplaying.data.first?.track.imageurl {

                                  strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
                                  self.imageurl.kf.setImage(with: URL(string: strUrl), placeholder: nil)

                                //MusicPlayer.shared.nowplaying(artist:  $0.track.artist, song: $0.track.title, cover:strUrl)
                                MusicPlayer.shared.getArtBoard(artist: $0.track.artist, song: $0.track.title, cover:strUrl)
                              }





                              //self.imageurl.setImage(with: $0.track.imageurl)

                         }



                      }catch let jsonErr{
                      print("error json ", jsonErr)
                      }


                  }.resume()
      }


      override func viewDidAppear(_ animated: Bool) {
         self.tabBarController?.navigationItem.title = "Now Playing"


      }



      @IBAction func playVideo(_ sender: Any) {
          if(playButton == false){
              playButton = true;
        //  (sender as! UIButton).setTitle("Pause", for: [])
            (sender as! UIButton).setBackgroundImage(UIImage(systemName: "pause.circle"), for: UIControl.State.normal)
              MusicPlayer.shared.startBackgroundMusic()

          }else
          {
              playButton = false;
       //   (sender as! UIButton).setTitle("Play", for: [])

            (sender as! UIButton).setBackgroundImage(UIImage(systemName: "play.circle"), for: UIControl.State.normal)
            MusicPlayer.shared.stopBackgroundMusic()
            //self.player.pause()
          }

      }


}

0 个答案:

没有答案