2017-01-27 12 views
2

SiriKitを使用して簡単な写真検索を開発しています。メインのビューコントロールで画像を検索して表示できますが、IntentsUIフレームワークを使用してインテントUIに画像を表示できません。私はこれに続いてtutorialしかし、インテントのUI実装が欠けています。ここで私はこれまで何をしてきたのですか?SiriKitを使用してIntents UIに画像を表示

IntentHandler.swift

class IntentHandler: INExtension, INSearchForPhotosIntentHandling { 

    override func handler(for intent: INIntent) -> Any { 

     return self 
    } 

    // MARK: - INSearchForPhotosIntentHandling 

    func resolveDateCreated(forSearchForPhotos intent: INSearchForPhotosIntent, with completion: @escaping (INDateComponentsRangeResolutionResult) -> Void) { 

     if intent.dateCreated != nil { 
      completion(INDateComponentsRangeResolutionResult.success(with: intent.dateCreated!)) 
     } 
     else{ 
      completion(INDateComponentsRangeResolutionResult.needsValue()) 
     } 
    } 


    func confirm(searchForPhotos intent: INSearchForPhotosIntent, completion: @escaping (INSearchForPhotosIntentResponse) -> Void) { 

     let response = INSearchForPhotosIntentResponse(code: .ready, userActivity: nil) 
     completion(response) 
    } 

    func handle(searchForPhotos intent: INSearchForPhotosIntent, completion: @escaping (INSearchForPhotosIntentResponse) -> Void) { 
     let response = INSearchForPhotosIntentResponse(code:.continueInApp,userActivity: nil) 

     if intent.dateCreated != nil { 
      let calendar = Calendar(identifier: .gregorian) 
      let startDate = calendar.date(from:(intent.dateCreated?.startDateComponents)!) 
      response.searchResultsCount = photoSearchFrom(startDate!) 

     } 
     completion(response) 
    } 


    // MARK: - Search Photos 

    func photoSearchFrom(_ startDate: Date) -> Int { 

     let fetchOptions = PHFetchOptions() 
     fetchOptions.predicate = NSPredicate(format: "creationDate >= %@", startDate as CVarArg) 

     let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, 
               options: fetchOptions) 
     return fetchResult.count 
    } 

} 

AppDelegate.Swift

class AppDelegate: UIResponder, UIApplicationDelegate { 
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { 

     // implement to handle user activity created by Siri or by our SiriExtension 
     let viewController = self.window?.rootViewController as! PhotoViewController 
     viewController.handleActivity(userActivity) 

     return true 
    } 
} 

PhotoViewController.swift

class ViewController: UIViewController { 

    @IBOutlet weak var imageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     INPreferences.requestSiriAuthorization { (status) in 
      print(status) 
     } 
    } 

    func handleActivity(_ userActivity: NSUserActivity) { 

     let intent = userActivity.interaction?.intent as! INSearchForPhotosIntent 

     if (intent.dateCreated?.startDateComponents) != nil { 
      let calendar = Calendar(identifier: .gregorian) 
      let startDate = calendar.date(from:(intent.dateCreated?.startDateComponents)!) 

      self.displayPhoto(startDate!) 
     } 
    } 


    func displayPhoto(_ startDate: Date) { 

     let fetchOptions = PHFetchOptions() 
     fetchOptions.predicate = NSPredicate(format: "creationDate >= %@", startDate as CVarArg) 
     let fetchResult = PHAsset.fetchAssets(with: 
      PHAssetMediaType.image, options: fetchOptions) 

     let imgManager = PHImageManager.default() 

     imgManager.requestImage(for: fetchResult.firstObject! as PHAsset, 
           targetSize: view.frame.size, 
           contentMode: PHImageContentMode.aspectFill, 
           options: nil, 
           resultHandler: { (image, _) in 
            self.imageView.image = image 
     }) 
    } 
} 

今ここでは、IntentViewController.swift

class IntentViewController: UIViewController, INUIHostedViewControlling { 

    @IBOutlet weak var imageView:UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 

     INPreferences.requestSiriAuthorization { (status) in 
      print("From IntentViewController: \(status)") 
     } 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(true) 
     print("IntentViewController-> viewDidLoad") 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - INUIHostedViewControlling 

    // Prepare your view controller for the interaction to handle. 
    func configure(with interaction: INInteraction!, context: INUIHostedViewContext, completion: ((CGSize) -> Void)!) { 

     print("From configure") 

     let intent = interaction?.intent as! INSearchForPhotosIntent 

     if (intent.dateCreated?.startDateComponents) != nil { 
      let calendar = Calendar(identifier: .gregorian) 
      let startDate = calendar.date(from:(intent.dateCreated?.startDateComponents)!) 
      self.displayPhoto(startDate!) 
     } 

     if let completion = completion { 
      completion(self.desiredSize) 
     } 
    } 

    var desiredSize: CGSize { 
     return self.extensionContext!.hostedViewMaximumAllowedSize 
    } 

    func displayPhoto(_ startDate: Date) { 

     let fetchOptions = PHFetchOptions() 
     fetchOptions.predicate = NSPredicate(format: "creationDate >= %@", startDate as CVarArg) 
     let fetchResult = PHAsset.fetchAssets(with: 
      PHAssetMediaType.image, options: fetchOptions) 

     let imgManager = PHImageManager.default() 

     imgManager.requestImage(for: fetchResult.firstObject! as PHAsset, 
           targetSize: view.frame.size, 
           contentMode: PHImageContentMode.aspectFill, 
           options: nil, 
           resultHandler: { (image, _) in 
            self.imageView.image = image 
     }) 
    } 
} 

に来て、私はインテントUIを使用して画像を表示するIntentHandler.swiftファイルのhandleメソッド内の余分なコードを記述する必要がありますか?私は実際にはインテントのUIで結果が欲しいというアプリで続行したくない。前もって感謝します。

+0

https://developer.apple.com/documentation/sirikit/photos – Mrunal

答えて

1

私はリンゴでそれを見つけたdocumentation。あなたは以下のドメインで をインテントをサポートしている場合

あなたはインテントUIの拡張機能を提供することができます:私は写真検索用のインテントUIを使用しないことを示し

Messaging 

Payments 

Ride booking 

Workouts 

を。

+0

https://developer.apple.com/documentation/sirikit/photos – Mrunal

関連する問題