Created
December 13, 2012 13:56
-
-
Save danielctull/4276526 to your computer and use it in GitHub Desktop.
A controller object that presents an activity view controller either modally or in a popover depending on the platform. It associates the created controller object with the activity view controller or popover controller, so that when either are dismissed, all the associations are removed. This way if you hold only a *weak* reference to the retur…
This file contains 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
#import <UIKit/UIKit.h> | |
@interface ActivityController : NSObject | |
+ (instancetype)presentActivityItems:(NSArray *)activityItems | |
fromViewController:(UIViewController *)viewController | |
barButtonItem:(UIBarButtonItem *)item; | |
@end |
This file contains 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
#import "ActivityController.h" | |
#import <objc/runtime.h> | |
void* ActivityControllerContext = &ActivityControllerContext; | |
@interface ActivityController () <UIPopoverControllerDelegate> | |
@end | |
@implementation ActivityController { | |
UIPopoverController *_popoverController; | |
} | |
+ (instancetype)presentActivityItems:(NSArray *)activityItems | |
fromViewController:(UIViewController *)viewController | |
barButtonItem:(UIBarButtonItem *)item { | |
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems | |
applicationActivities:nil]; | |
ActivityController *controller = [self new]; | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { | |
[viewController presentViewController:activityViewController animated:YES completion:NULL]; | |
objc_setAssociatedObject(activityViewController, ActivityControllerContext, controller, OBJC_ASSOCIATION_RETAIN); | |
return nil; | |
} | |
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:activityViewController]; | |
popoverController.delegate = controller; | |
controller->_popoverController = popoverController; | |
objc_setAssociatedObject(popoverController, ActivityControllerContext, controller, OBJC_ASSOCIATION_RETAIN); | |
[popoverController presentPopoverFromBarButtonItem:item | |
permittedArrowDirections:UIPopoverArrowDirectionAny | |
animated:YES]; | |
popoverController.passthroughViews = @[]; | |
return controller; | |
} | |
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { | |
_popoverController = nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment