Skip to content

Commit 12dba8b

Browse files
committed
Merge pull request #270 from garnett/share-sheet
Implement methods exposing proper sharing completion handling
2 parents b49f4ed + 1900455 commit 12dba8b

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Branch-SDK/Branch-SDK/BranchUniversalObject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "Branch.h"
1212

1313
typedef void (^callback) ();
14+
typedef void (^shareCompletion) (NSString *activityType, BOOL completed);
1415

1516
@interface BranchUniversalObject : NSObject
1617

@@ -40,8 +41,11 @@ typedef NS_ENUM(NSInteger, ContentIndexMode) {
4041
- (NSString *)getShortUrlWithLinkProperties:(BranchLinkProperties *)linkProperties;
4142
- (void)getShortUrlWithLinkProperties:(BranchLinkProperties *)linkProperties andCallback:(callbackWithUrl)callback;
4243
- (UIActivityItemProvider *)getBranchActivityItemWithLinkProperties:(BranchLinkProperties *)linkProperties;
43-
- (void)showShareSheetWithShareText:(NSString *)shareText andCallback:(callback)callback;
44-
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties andShareText:(NSString *)shareText fromViewController:(UIViewController *)viewController andCallback:(callback)callback;
44+
- (void)showShareSheetWithShareText:(NSString *)shareText andCallback:(callback)callback __attribute__((deprecated(("This method has been deprecated. Use -[showShareSheetWithShareText:completion:] instead."))));
45+
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties andShareText:(NSString *)shareText fromViewController:(UIViewController *)viewController andCallback:(callback)callback __attribute__((deprecated(("This method has been deprecated. Use -[showShareSheetWithLinkProperties:andShareText:fromViewController:viewController:completion:] instead."))));
46+
- (void)showShareSheetWithShareText:(NSString *)shareText completion:(shareCompletion)completion;
47+
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties andShareText:(NSString *)shareText fromViewController:(UIViewController *)viewController completion:(shareCompletion)completion;
48+
4549
- (void)listOnSpotlight;
4650
- (void)listOnSpotlightWithCallback:(callbackWithUrl)callback;
4751
- (void)listOnSpotlightWithIdentifierCallback:(callbackWithUrlAndSpotlightIdentifier)spotlightCallback;

Branch-SDK/Branch-SDK/BranchUniversalObject.m

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,28 @@ - (UIActivityItemProvider *)getBranchActivityItemWithLinkProperties:(BranchLinkP
120120
alias:linkProperties.alias];
121121
}
122122

123-
- (void)showShareSheetWithShareText:(NSString *)shareText andCallback:(callback)callback {
124-
[self showShareSheetWithLinkProperties:nil andShareText:shareText fromViewController:nil andCallback:callback];
123+
- (void)showShareSheetWithShareText:(NSString *)shareText completion:(shareCompletion)completion {
124+
[self showShareSheetWithLinkProperties:nil andShareText:shareText fromViewController:nil completion:completion];
125125
}
126126

127-
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties andShareText:(NSString *)shareText fromViewController:(UIViewController *)viewController andCallback:(callback)callback {
127+
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties andShareText:(NSString *)shareText fromViewController:(UIViewController *)viewController completion:(shareCompletion)completion {
128128
UIActivityItemProvider *itemProvider = [self getBranchActivityItemWithLinkProperties:linkProperties];
129129
NSMutableArray *items = [NSMutableArray arrayWithObject:itemProvider];
130130
if (shareText) {
131131
[items addObject:shareText];
132132
}
133133
UIActivityViewController *shareViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
134134

135+
if ([shareViewController respondsToSelector:@selector(completionWithItemsHandler)]) {
136+
shareViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
137+
if (completion) {
138+
completion(activityType, completed);
139+
}
140+
};
141+
} else {
142+
shareViewController.completionHandler = completion;
143+
}
144+
135145
UIViewController *presentingViewController;
136146
if (viewController && [viewController respondsToSelector:@selector(presentViewController:animated:completion:)]) {
137147
presentingViewController = viewController;
@@ -154,13 +164,26 @@ - (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties
154164
if ([presentingViewController respondsToSelector:@selector(popoverPresentationController)]) {
155165
shareViewController.popoverPresentationController.sourceView = presentingViewController.view;
156166
}
157-
[presentingViewController presentViewController:shareViewController animated:YES completion:callback];
167+
[presentingViewController presentViewController:shareViewController animated:YES completion:nil];
158168
}
159169
else {
160170
NSLog(@"[Branch warning, fatal] No view controller is present to show the share sheet. Aborting.");
161171
}
162172
}
163173

174+
175+
- (void)showShareSheetWithShareText:(NSString *)shareText andCallback:(callback)callback {
176+
[self showShareSheetWithLinkProperties:nil andShareText:shareText fromViewController:nil andCallback:callback];
177+
}
178+
179+
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties andShareText:(NSString *)shareText fromViewController:(UIViewController *)viewController andCallback:(callback)callback {
180+
[self showShareSheetWithLinkProperties:linkProperties andShareText:shareText fromViewController:viewController completion:^(NSString *activityType, BOOL completed) {
181+
if (callback) {
182+
callback();
183+
}
184+
}];
185+
}
186+
164187
- (void)listOnSpotlight {
165188
[self listOnSpotlightWithCallback:nil];
166189
}

Branch-TestBed/Branch-TestBed/ViewController.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ - (IBAction)cmdShareLink:(id)sender {
5656
[linkProperties addControlParam:@"$ios_url" withValue:@"http://example.com/ios"];
5757

5858

59-
[self.branchUniversalObject showShareSheetWithLinkProperties:linkProperties
60-
andShareText:@"Super amazing thing I want to share"
61-
fromViewController:self
62-
andCallback:^{
63-
NSLog(@"Finished showing the share sheet!!");
59+
[self.branchUniversalObject
60+
showShareSheetWithShareText:@"Super amazing thing I want to share"
61+
completion:^(NSString *activityType, BOOL completed) {
62+
if (completed) {
63+
NSLog(@"%@", [NSString stringWithFormat:@"Completed sharing to %@", activityType]);
64+
}
6465
}];
6566
}
6667
- (IBAction)cmdRegisterView:(id)sender {

0 commit comments

Comments
 (0)