Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS 8 App Extensions

iOS 8 App Extensions

A presentation originally delivered at Pragma Mark in Milan, Italy on October 4th, 2014,

Ben Scheirman

October 03, 2014
Tweet

More Decks by Ben Scheirman

Other Decks in Programming

Transcript

  1. SUBQUERY ( extensionItems, $extensionItem, SUBQUERY ( $extensionItem.attachments, $attachment, ANY $attachment.registeredTypeIdentifiers

    UTI-CONFORMS-TO "com.adobe.pdf" ).@count == $extensionItem.attachments.@count ).@count == 1
  2. Setup Shared NSUserDefaults var userDefaults: NSUserDefaults { get { return

    NSUserDefaults(suiteName: "group.com.nsscreencast.coffeetracker") } }
  3. Add to CoffeeTracker var numberOfCups: Int { get { return

    userDefaults.integerForKey("cups") } set { userDefaults.setInteger(newValue, forKey: "cups") updateDisplay() } }
  4. Handle Periodic Background Updates func widgetPerformUpdateWithCompletionHandler(completionHandler: (NCUpdateResult) -> ()) {

    coffeeTracker.updateDisplay() // If an error is encountered, use NCUpdateResult.Failed // If there's no update required, use NCUpdateResult.NoData // If there's an update, use NCUpdateResult.NewData completionHandler(NCUpdateResult.NewData) }
  5. · Share content to another app / service · Use

    System Sharing UI or Custom · Upload using shared background session
  6. Get the Item Provider override func viewDidLoad() { var itemProvider:

    NSItemProvider? if let items = extensionContext?.inputItems { } }
  7. Get the Item Provider override func viewDidLoad() { var itemProvider:

    NSItemProvider? if let items = extensionContext?.inputItems { if let item = items.first as? NSExtensionItem { } } }
  8. Get the Item Provider override func viewDidLoad() { var itemProvider:

    NSItemProvider? if let items = extensionContext?.inputItems { if let item = items.first as? NSExtensionItem { if let attachment = item.attachments?.first as? NSItemProvider { itemProvider = attachment } } } }
  9. Pull an image out of the item provider override func

    viewDidLoad() { // ... let imageType = kUTTypeImage as NSString if itemProvider?.hasItemConformingToTypeIdentifier(imageType) == true { } }
  10. Pull an image out of the item provider override func

    viewDidLoad() { // ... let imageType = kUTTypeImage as NSString if itemProvider?.hasItemConformingToTypeIdentifier(imageType) == true { itemProvider?.loadItemForTypeIdentifier(imageType, options: nil) { (item, error) in }) } }
  11. Pull an image out of the item provider override func

    viewDidLoad() { // ... let imageType = kUTTypeImage as NSString if itemProvider?.hasItemConformingToTypeIdentifier(imageType) == true { itemProvider?.loadItemForTypeIdentifier(imageType, options: nil) { (item, error) in if error == nil { let url = item as NSURL let imageData = NSData(contentsOfURL: url) self.image = UIImage(data: imageData) } else { println("ERROR: \(error)") } }) } }
  12. Action.js var Action = function() {}; Action.prototype = { run:

    function(arguments) { }, finalize: function(arguments) { } }; var ExtensionPreprocessingJS = new Action
  13. func beginRequestWithExtensionContext(context: NSExtensionContext) { self.extensionContext = context var itemProvider: NSItemProvider?

    if let item = context.inputItems.first as? NSExtensionItem { if let attachment = item.attachments?.first as? NSItemProvider { itemProvider = attachment } } let itemType = String(kUTTypePropertyList) if itemProvider?.hasItemConformingToTypeIdentifier(itemType) == true { itemProvider?.loadItemForTypeIdentifier(itemType, options: nil) { item, error in let dictionary = item as NSDictionary NSOperationQueue.mainQueue().addOperationWithBlock { let preprocessingResults = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as [NSObject:AnyObject] self.itemLoadCompletedWithPreprocessingResults(preprocessingResults) } } } else { self.doneWithResults(nil) } }
  14. let dictionary = item as NSDictionary NSOperationQueue.mainQueue().addOperationWithBlock { let results

    = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as NSDictionary self.itemLoadCompletedWithPreprocessingResults(results)
  15. func itemLoadCompletedWithPreprocessingResults(results: NSDictionary) { let host = results["host"] as String

    let path = results["path"] as String let newURL = "http://cat.\(host).meowbify.com\(path)" doneWithResults(["newURL": newURL]) }
  16. func doneWithResults(results: NSDictionary?) { if let finalizeResults = results {

    var resultsDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: finalizeResults] let itemType = kUTTypePropertyList as NSString var resultsProvider = NSItemProvider(item: resultsDictionary, typeIdentifier: itemType) var resultsItem = NSExtensionItem() resultsItem.attachments = [resultsProvider] extensionContext?.completeRequestReturningItems([resultsItem], completionHandler: nil) } else { extensionContext?.completeRequestReturningItems(nil, completionHandler: nil) } extensionContext = nil }