Created
July 25, 2016 09:40
-
-
Save sketchytech/24fcd4f79c71c21e2f38234c64e8a27c to your computer and use it in GitHub Desktop.
UIActivityViewController that adds an Open In option
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// ShareTest | |
// | |
// Created by A J LEVINGS on 24/07/2016. | |
// Copyright © 2016 Gylphi. All rights reserved. | |
// | |
import UIKit | |
let UIActivityTypeOpenIn = "OpenIn" | |
class OpenIn: UIActivity { | |
var docController: UIDocumentInteractionController! | |
weak var barButton: UIBarButtonItem! | |
// it's necessary to know which button the UIActivityViewController originated from | |
init(barButton barB: UIBarButtonItem) { | |
self.barButton = barB | |
} | |
// this provides a way of excluding the activity if we wish | |
override func activityType() -> String? { | |
return UIActivityTypeOpenIn | |
} | |
// provides title within popover | |
override func activityTitle() -> String? { | |
// your activity title here | |
return "Open In..." | |
} | |
// in real use you would provide image here at required size (see class reference) | |
override func activityImage() -> UIImage? { | |
// return your icon image here, for simplicity I've used nil | |
return nil | |
} | |
// specify whether Action or Share (default is Action) | |
override class func activityCategory() -> UIActivityCategory { | |
// ,Share places the option on the top row | |
return .Share | |
} | |
// confirm the action can be performed | |
override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool { | |
for a in activityItems { | |
if a is NSURL { | |
return true | |
} | |
} | |
return false | |
} | |
// prepare for the activity | |
override func prepareWithActivityItems(activityItems: [AnyObject]) { | |
for a in activityItems { | |
if let url = a as? NSURL { | |
docController = UIDocumentInteractionController(URL: url) | |
} | |
} | |
} | |
// perform the activity | |
override func performActivity() { | |
docController.presentOpenInMenuFromBarButtonItem(barButton, animated: true) | |
} | |
} | |
class ViewController: UIViewController { | |
// UIDocumentInteractionController instance is a class property | |
var activityController: UIActivityViewController! | |
@IBOutlet weak var shareButton: UIBarButtonItem! | |
// called when bar button item is pressed | |
@IBAction func shareStuff(sender: AnyObject) { | |
if let barButton = sender as? UIBarButtonItem { | |
self.presentViewController(activityController, animated: true, completion: nil) | |
let presCon = activityController.popoverPresentationController | |
presCon?.barButtonItem = barButton | |
} | |
else { | |
print("not a bar button!") | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let open = OpenIn(barButton: shareButton) | |
if let fileURL = NSBundle.mainBundle().URLForResource("MyImage", withExtension: "jpg") { | |
activityController = UIActivityViewController(activityItems: [fileURL], applicationActivities: [open]) | |
} | |
else { | |
print("file missing!") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment