ActivityUtil.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ActivityUtil.h"
  21. #import "MessageActivityItem.h"
  22. #import "TTOpenInAppActivity.h"
  23. #import "ForwardTextActivity.h"
  24. #import "ForwardURLActivity.h"
  25. #import "QRCodeActivity.h"
  26. @implementation ActivityUtil
  27. + (NSArray *)defaultApplicationActivities {
  28. NSArray *applicationActivities = @[
  29. [[ForwardTextActivity alloc] init],
  30. [[ForwardURLActivity alloc] init],
  31. [[QRCodeActivity alloc] init]
  32. ];
  33. return applicationActivities;
  34. }
  35. + (UIActivityViewController *)activityViewControllerForMessage:(BaseMessage *)message withView:(UIView *)view andRect:(CGRect)rect {
  36. TTOpenInAppActivity *openInAppActivity = [[TTOpenInAppActivity alloc] initWithView:view andRect:rect];
  37. return [self activityViewControllerForMessage:message withTTOpenInAppActivity:openInAppActivity];
  38. }
  39. + (UIActivityViewController *)activityViewControllerForMessage:(BaseMessage *)message withView:(UIView *)view andBarButtonItem:(UIBarButtonItem *)barButtonItem {
  40. TTOpenInAppActivity *openInAppActivity = [[TTOpenInAppActivity alloc] initWithView:view andBarButtonItem:barButtonItem];
  41. return [self activityViewControllerForMessage:message withTTOpenInAppActivity:openInAppActivity];
  42. }
  43. + (UIActivityViewController *)activityViewControllerForMessage:(BaseMessage *)message withTTOpenInAppActivity:(TTOpenInAppActivity *)openInAppActivity {
  44. MessageActivityItem *item = [MessageActivityItem activityItemFor: message];
  45. NSMutableArray *applicationActivities = [NSMutableArray arrayWithArray:[ActivityUtil defaultApplicationActivities]];
  46. [applicationActivities addObject:openInAppActivity];
  47. UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:applicationActivities];
  48. openInAppActivity.superViewController = activityViewController;
  49. activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
  50. NSURL *tmpFileUrl = [item getURL];
  51. if (tmpFileUrl) {
  52. [[NSFileManager defaultManager] removeItemAtURL:tmpFileUrl error:nil];
  53. }
  54. };
  55. return activityViewController;
  56. }
  57. + (UIActivityViewController *)activityViewControllerWithActivityItems:(NSArray *)activityItems applicationActivities:(NSArray *)applicationActivities {
  58. UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
  59. return activityViewController;
  60. }
  61. @end