0

So i have Intents working to a point. As in I mean siri repsonse with Done or Ok. When I ask Siri the following command

List item

: "Hey siri Play DRN1"

Siri Response is DONE. (when it should be Opening now)

But the app does nothing.

Now I have noticed I have not set up the following screen or I have missed a step??

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()
          }

      }


}
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
  • Did you implement your `PlayDRN1IntentHandling` protocol (where `PlayDRN1IntentResponse` should be returned in its handle() method)? – Wei WANG Dec 05 '19 at 03:50
  • @WeiWANG How do I do that? As This is my first intents I have ever done - the only things I have done are above... – RussellHarrower Dec 06 '19 at 06:01
  • When you add an Intent definition file, there will be some class and protocol files auto-generated by Xcode for you, one of which being e.g. PlayDRN1IntentHandling, where you validate and handle the incoming intent and its params. You may want to take a look and try out Apple's **Soup Chef** example: https://developer.apple.com/documentation/sirikit/soup_chef_accelerating_app_interactions_with_shortcuts, which is a good reference. – Wei WANG Dec 06 '19 at 09:52

0 Answers0