Skip to content

Instantly share code, notes, and snippets.

@advantis
Created June 10, 2013 07:52
Show Gist options
  • Save advantis/5747152 to your computer and use it in GitHub Desktop.
Save advantis/5747152 to your computer and use it in GitHub Desktop.
Reusable wrapper for UIDocumentInteractionController's "Open In" menu
//
// Copyright © 2013 Yuri Kotov
//
#import <Foundation/Foundation.h>
@interface ADVFileSharer : NSObject
- (instancetype) initWithFileURL:(NSURL *)url;
- (BOOL) presentShareMenuFromBarButtonItem:(UIBarButtonItem *)item completion:(void(^)())completion;
- (void) dismissMenu;
@end
//
// Copyright © 2013 Yuri Kotov
//
#import "ADVFileSharer.h"
@interface ADVFileSharer () <UIDocumentInteractionControllerDelegate>
@property (strong, nonatomic) UIDocumentInteractionController *documentController;
@property (strong, nonatomic) void(^completion)();
@end
@implementation ADVFileSharer
{
NSURL *_fileURL;
BOOL _isSaving;
}
- (instancetype) initWithFileURL:(NSURL *)url
{
if ((self = [super init]))
{
_fileURL = url;
}
return self;
}
- (BOOL) presentShareMenuFromBarButtonItem:(UIBarButtonItem *)item completion:(void(^)())completion
{
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:_fileURL];
self.documentController.delegate = self;
BOOL didShow = [self.documentController presentOpenInMenuFromBarButtonItem:item animated:YES];
if (didShow)
{
__weak typeof(self) weakSelf = self;
self.completion = ^{
weakSelf.documentController.delegate = nil;
weakSelf.documentController = nil;
if (completion) completion();
};
}
else
{
self.documentController = nil;
}
return didShow;
}
- (void) dismissMenu
{
[self.documentController dismissMenuAnimated:YES];
}
#pragma mark - UIDocumentInteractionControllerDelegate
- (void) documentInteractionController:(UIDocumentInteractionController *)controller
willBeginSendingToApplication:(NSString *)application
{
// TODO: Replace with sharing progress indication
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
_isSaving = YES;
}
- (void) documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
{
if (!_isSaving) self.completion();
}
- (void) documentInteractionController:(UIDocumentInteractionController *)controller
didEndSendingToApplication:(NSString *)application
{
_isSaving = NO;
self.completion();
// TODO: Replace with sharing progress indication
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment