|
// |
|
// 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 |