Advertisement
Guest User

Untitled

a guest
May 6th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. //
  2. // ActionRequestHandler.swift
  3. // CocoaConf Action no UI
  4. //
  5. // Created by Chris Adamson on 3/9/15.
  6. // Copyright (c) 2015 Subsequently & Furthermore, Inc. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import MobileCoreServices
  11.  
  12. class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
  13.  
  14. var extensionContext: NSExtensionContext?
  15.  
  16. func beginRequestWithExtensionContext(context: NSExtensionContext) {
  17. // Do not call super in an Action extension with no user interface
  18. self.extensionContext = context
  19.  
  20. var found = false
  21.  
  22. // Find the item containing the results from the JavaScript preprocessing.
  23. outer:
  24. for item: AnyObject in context.inputItems {
  25. let extItem = item as NSExtensionItem
  26. if let attachments = extItem.attachments {
  27. for itemProvider: AnyObject in attachments {
  28. NSLog ("itemProvider: \(itemProvider)")
  29. if itemProvider.hasItemConformingToTypeIdentifier(String(kUTTypePropertyList)) {
  30. NSLog ("property list")
  31. itemProvider.loadItemForTypeIdentifier(String(kUTTypePropertyList), options: nil, completionHandler: { (item, error) in
  32. let dictionary = item as [String: AnyObject]
  33. NSOperationQueue.mainQueue().addOperationWithBlock {
  34. self.itemLoadCompletedWithPreprocessingResults(dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as [NSObject: AnyObject])
  35. }
  36. found = true
  37. })
  38. if found {
  39. break outer
  40. }
  41. }
  42. }
  43. }
  44. }
  45.  
  46. if !found {
  47. self.doneWithResults(nil)
  48. }
  49. }
  50.  
  51. func itemLoadCompletedWithPreprocessingResults(javaScriptPreprocessingResults: [NSObject: AnyObject]) {
  52. NSLog ("itemLoadCompleteWithPreprocessingResults: \(javaScriptPreprocessingResults)")
  53.  
  54.  
  55. // Here, do something, potentially asynchronously, with the preprocessing
  56. // results.
  57.  
  58. // In this very simple example, the JavaScript will have passed us the
  59. // current background color style, if there is one. We will construct a
  60. // dictionary to send back with a desired new background color style.
  61. let bgColor: AnyObject? = javaScriptPreprocessingResults["currentBackgroundColor"]
  62. if bgColor == nil || bgColor! as String == "" {
  63. // No specific background color? Request setting the background to red.
  64. self.doneWithResults(["newBackgroundColor": "red"])
  65. } else {
  66. // Specific background color is set? Request replacing it with green.
  67. self.doneWithResults(["newBackgroundColor": "green"])
  68. }
  69. }
  70.  
  71. func doneWithResults(resultsForJavaScriptFinalizeArg: [NSObject: AnyObject]?) {
  72. if let resultsForJavaScriptFinalize = resultsForJavaScriptFinalizeArg {
  73. // Construct an NSExtensionItem of the appropriate type to return our
  74. // results dictionary in.
  75.  
  76. // These will be used as the arguments to the JavaScript finalize()
  77. // method.
  78.  
  79. var resultsDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: resultsForJavaScriptFinalize]
  80.  
  81. var resultsProvider = NSItemProvider(item: resultsDictionary, typeIdentifier: String(kUTTypePropertyList))
  82.  
  83. var resultsItem = NSExtensionItem()
  84. resultsItem.attachments = [resultsProvider]
  85.  
  86. // Signal that we're complete, returning our results.
  87. self.extensionContext!.completeRequestReturningItems([resultsItem], completionHandler: nil)
  88. } else {
  89. // We still need to signal that we're done even if we have nothing to
  90. // pass back.
  91. self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
  92. }
  93.  
  94. // Don't hold on to this after we finished with it.
  95. self.extensionContext = nil
  96. }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement