Skip to content

Commit 275f5f0

Browse files
authored
Allow UIBarButtonItem or UIView as share presentation anchor AIS-360 AIS-376
1 parent a0beb83 commit 275f5f0

File tree

8 files changed

+120
-25
lines changed

8 files changed

+120
-25
lines changed

Branch-SDK/Branch-SDK/BranchShareLink.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ Creates a BranchShareLink object.
7575
/**
7676
Presents a UIActivityViewController that shares the Branch link.
7777
78-
@param viewController The parent view controller from which to present the the activity sheet.
79-
@param anchor The anchor point for the activity sheet. Used for iPad form factors.
78+
@param viewController The parent view controller from which to present the the activity sheet.
79+
@param anchorViewOrButtonItem The anchor point for the activity sheet. Used for iPad form factors.
8080
*/
8181
- (void) presentActivityViewControllerFromViewController:(UIViewController*_Nullable)viewController
82-
anchor:(UIBarButtonItem*_Nullable)anchor;
82+
anchor:(id _Nullable)anchorViewOrButtonItem;
8383

8484
///The title for the share sheet.
8585
@property (nonatomic, strong) NSString*_Nullable title;

Branch-SDK/Branch-SDK/BranchShareLink.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ - (void) shareDidComplete:(BOOL)completed activityError:(NSError*)error {
153153
}
154154

155155
- (void) presentActivityViewControllerFromViewController:(UIViewController*_Nullable)viewController
156-
anchor:(UIBarButtonItem*_Nullable)anchor {
156+
anchor:(id _Nullable)anchorViewOrButtonItem {
157157

158158
UIActivityViewController *shareViewController =
159159
[[UIActivityViewController alloc]
@@ -209,9 +209,17 @@ - (void) presentActivityViewControllerFromViewController:(UIViewController*_Null
209209

210210
// Required for iPad/Universal apps on iOS 8+
211211
if ([presentingViewController respondsToSelector:@selector(popoverPresentationController)]) {
212-
shareViewController.popoverPresentationController.sourceView = presentingViewController.view;
213-
if (anchor) {
212+
if ([anchorViewOrButtonItem isKindOfClass:UIBarButtonItem.class]) {
213+
UIBarButtonItem *anchor = (UIBarButtonItem*) anchorViewOrButtonItem;
214214
shareViewController.popoverPresentationController.barButtonItem = anchor;
215+
} else
216+
if ([anchorViewOrButtonItem isKindOfClass:UIView.class]) {
217+
UIView *anchor = (UIView*) anchorViewOrButtonItem;
218+
shareViewController.popoverPresentationController.sourceView = anchor;
219+
shareViewController.popoverPresentationController.sourceRect = anchor.bounds;
220+
} else {
221+
shareViewController.popoverPresentationController.sourceView = presentingViewController.view;
222+
shareViewController.popoverPresentationController.sourceRect = CGRectMake(0.0, 0.0, 40.0, 40.0);
215223
}
216224
}
217225
[presentingViewController presentViewController:shareViewController animated:YES completion:nil];

Branch-SDK/Branch-SDK/BranchUniversalObject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ typedef NS_ENUM(NSInteger, ContentIndexMode) {
6363
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController completionWithError:(nullable shareCompletionWithError)completion;
6464

6565
//iPad
66-
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController anchor:(nullable UIBarButtonItem *)anchor completion:(nullable shareCompletion)completion;
66+
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController anchor:(nullable id)anchorViewOrBarbutton completion:(nullable shareCompletion)completion;
6767

6868
// Returns with activityError as well
69-
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController anchor:(nullable UIBarButtonItem *)anchor completionWithError:(nullable shareCompletionWithError)completion;
69+
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController anchor:(nullable id)anchorViewOrBarButton completionWithError:(nullable shareCompletionWithError)completion;
7070

7171
- (void)listOnSpotlight;
7272
- (void)listOnSpotlightWithCallback:(nullable callbackWithUrl)callback;

Branch-SDK/Branch-SDK/BranchUniversalObject.m

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,20 @@ - (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties
231231
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties andShareText:(NSString *)shareText fromViewController:(UIViewController *)viewController completionWithError:(shareCompletionWithError)completion {
232232
[self showShareSheetWithLinkProperties:linkProperties andShareText:shareText fromViewController:viewController anchor:nil completion:nil orCompletionWithError:completion];
233233
}
234-
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController anchor:(nullable UIBarButtonItem *)anchor completion:(nullable shareCompletion)completion {
234+
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController anchor:(nullable id)anchor completion:(nullable shareCompletion)completion {
235235
[self showShareSheetWithLinkProperties:linkProperties andShareText:shareText fromViewController:viewController anchor:anchor completion:completion orCompletionWithError:nil];
236236
}
237237

238-
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController anchor:(nullable UIBarButtonItem *)anchor completionWithError:(nullable shareCompletionWithError)completion {
238+
- (void)showShareSheetWithLinkProperties:(nullable BranchLinkProperties *)linkProperties andShareText:(nullable NSString *)shareText fromViewController:(nullable UIViewController *)viewController anchor:(nullable id)anchor completionWithError:(nullable shareCompletionWithError)completion {
239239
[self showShareSheetWithLinkProperties:linkProperties andShareText:shareText fromViewController:viewController anchor:anchor completion:nil orCompletionWithError:completion];
240240
}
241241

242-
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties andShareText:(NSString *)shareText fromViewController:(UIViewController *)viewController anchor:(UIBarButtonItem *)anchor completion:(shareCompletion)completion orCompletionWithError:(shareCompletionWithError)completionError {
242+
- (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties
243+
andShareText:(NSString *)shareText
244+
fromViewController:(UIViewController *)viewController
245+
anchor:(id)anchorViewOrButtonItem
246+
completion:(shareCompletion)completion
247+
orCompletionWithError:(shareCompletionWithError)completionError {
243248
// Log share initiated event
244249
[self userCompletedAction:BNCShareInitiatedEvent];
245250
UIActivityItemProvider *itemProvider = [self getBranchActivityItemWithLinkProperties:linkProperties];
@@ -290,9 +295,17 @@ - (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties
290295
if (presentingViewController) {
291296
// Required for iPad/Universal apps on iOS 8+
292297
if ([presentingViewController respondsToSelector:@selector(popoverPresentationController)]) {
293-
shareViewController.popoverPresentationController.sourceView = presentingViewController.view;
294-
if (anchor) {
298+
if ([anchorViewOrButtonItem isKindOfClass:UIBarButtonItem.class]) {
299+
UIBarButtonItem *anchor = (UIBarButtonItem*) anchorViewOrButtonItem;
295300
shareViewController.popoverPresentationController.barButtonItem = anchor;
301+
} else
302+
if ([anchorViewOrButtonItem isKindOfClass:UIView.class]) {
303+
UIView *anchor = (UIView*) anchorViewOrButtonItem;
304+
shareViewController.popoverPresentationController.sourceView = anchor;
305+
shareViewController.popoverPresentationController.sourceRect = anchor.bounds;
306+
} else {
307+
shareViewController.popoverPresentationController.sourceView = presentingViewController.view;
308+
shareViewController.popoverPresentationController.sourceRect = CGRectMake(0.0, 0.0, 40.0, 40.0);
296309
}
297310
}
298311
[presentingViewController presentViewController:shareViewController animated:YES completion:nil];

Branch-TestBed/Branch-TestBed-UITests/UITestBed/TBBranchViewController.m

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929
@interface TBBranchViewController () <UITableViewDelegate, UITableViewDataSource>
3030
@property (nonatomic, strong) TBTableData *tableData;
31-
@property (nonatomic, strong) BranchUniversalObject *branchUniversalObject;
31+
@property (nonatomic, strong) BranchUniversalObject *universalObject;
32+
@property (nonatomic, strong) BranchLinkProperties *linkProperties;
3233
@property (nonatomic, weak) IBOutlet UITableView *tableView;
3334
@property (nonatomic, strong) IBOutlet UINavigationItem *navigationItem;
3435
@end
@@ -58,6 +59,11 @@ - (void)initializeTableData {
5859
section(@"Events");
5960
row(@"Send Commerce Event", sendCommerceEvent:);
6061

62+
section(@"Sharing");
63+
row(@"ShareLink from table row", sharelinkTableRow:);
64+
row(@"ShareLink no anchor", sharelinkTableRowNilAnchor:);
65+
row(@"BUO Share from table row", buoShareTableRow:);
66+
6167
section(@"Miscellaneous");
6268
row(@"Show Local IP Addess", showLocalIPAddress:);
6369

@@ -69,16 +75,16 @@ - (void)viewDidLoad {
6975
[super viewDidLoad];
7076
[self initializeTableData];
7177

72-
_branchUniversalObject =
78+
_universalObject =
7379
[[BranchUniversalObject alloc] initWithCanonicalIdentifier: cononicalIdentifier];
74-
_branchUniversalObject.canonicalUrl = canonicalUrl;
75-
_branchUniversalObject.title = contentTitle;
76-
_branchUniversalObject.contentDescription = contentDescription;
77-
_branchUniversalObject.imageUrl = imageUrl;
78-
_branchUniversalObject.price = 1000;
79-
_branchUniversalObject.currency = @"$";
80-
_branchUniversalObject.type = type;
81-
[_branchUniversalObject
80+
_universalObject.canonicalUrl = canonicalUrl;
81+
_universalObject.title = contentTitle;
82+
_universalObject.contentDescription = contentDescription;
83+
_universalObject.imageUrl = imageUrl;
84+
_universalObject.price = 1000;
85+
_universalObject.currency = @"$";
86+
_universalObject.type = type;
87+
[_universalObject
8288
addMetadataKey:@"deeplink_text"
8389
value:[NSString stringWithFormat:
8490
@"This text was embedded as data in a Branch link with the following characteristics:\n\n"
@@ -101,6 +107,14 @@ - (void)viewDidLoad {
101107
r.size.height *= 1.75f;
102108
versionLabel.frame = r;
103109
self.tableView.tableHeaderView = versionLabel;
110+
111+
// Add a share button item:
112+
UIBarButtonItem *barButtonItem =
113+
[[UIBarButtonItem alloc]
114+
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
115+
target:self
116+
action:@selector(buoShareBarButton:)];
117+
self.navigationItem.rightBarButtonItem = barButtonItem;
104118
}
105119

106120
#pragma mark - Table View Delegate & Data Source
@@ -178,7 +192,7 @@ - (IBAction)createBranchLink:(TBTableRow*)sender {
178192
[linkProperties addControlParam:@"$desktop_url" withValue: desktop_url];
179193
[linkProperties addControlParam:@"$ios_url" withValue: ios_url];
180194

181-
[self.branchUniversalObject
195+
[self.universalObject
182196
getShortUrlWithLinkProperties:linkProperties
183197
andCallback:^(NSString *url, NSError *error) {
184198
sender.value = url;
@@ -286,4 +300,46 @@ - (IBAction)showLocalIPAddress:(id)sender {
286300
];
287301
}
288302

303+
#pragma mark - Sharing
304+
305+
- (IBAction) sharelinkTableRow:(id)sender {
306+
NSIndexPath *indexPath = [self.tableData indexPathForRow:sender];
307+
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
308+
BranchShareLink *shareLink =
309+
[[BranchShareLink alloc]
310+
initWithUniversalObject:self.universalObject
311+
linkProperties:self.linkProperties];
312+
[shareLink presentActivityViewControllerFromViewController:self anchor:cell];
313+
}
314+
315+
- (IBAction) sharelinkTableRowNilAnchor:(id)sender {
316+
BranchShareLink *shareLink =
317+
[[BranchShareLink alloc]
318+
initWithUniversalObject:self.universalObject
319+
linkProperties:self.linkProperties];
320+
[shareLink presentActivityViewControllerFromViewController:self anchor:nil];
321+
}
322+
323+
- (IBAction) buoShareTableRow:(id)sender {
324+
NSIndexPath *indexPath = [self.tableData indexPathForRow:sender];
325+
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
326+
[self.universalObject showShareSheetWithLinkProperties:self.linkProperties
327+
andShareText:@"Ha ha"
328+
fromViewController:self
329+
anchor:cell
330+
completionWithError: ^ (NSString * _Nullable activityType, BOOL completed, NSError * _Nullable activityError) {
331+
BNCLogDebug(@"Done.");
332+
}];
333+
}
334+
335+
- (IBAction) buoShareBarButton:(id)sender {
336+
[self.universalObject showShareSheetWithLinkProperties:self.linkProperties
337+
andShareText:@"Ha ha"
338+
fromViewController:self
339+
anchor:sender
340+
completionWithError: ^ (NSString * _Nullable activityType, BOOL completed, NSError * _Nullable activityError) {
341+
BNCLogDebug(@"Done.");
342+
}];
343+
}
344+
289345
@end

Branch-TestBed/Branch-TestBed-UITests/UITestBed/TBTableData.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
- (TBTableSection*) addSectionWithTitle:(NSString*)title;
3333
- (TBTableRow*) addRowWithTitle:(NSString*)title selector:(SEL)selector;
34+
35+
- (NSIndexPath*) indexPathForRow:(TBTableRow*)row;
3436
@end
3537

3638

Branch-TestBed/Branch-TestBed-UITests/UITestBed/TBTableData.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@ - (TBTableRow*) addRowWithTitle:(NSString*)title selector:(SEL)selector {
7777
return r;
7878
}
7979

80+
- (NSIndexPath*) indexPathForRow:(TBTableRow*)rowToFind {
81+
NSInteger rowIndex = 0;
82+
NSInteger sectionIndex = 0;
83+
for (NSArray *sections in self.rows) {
84+
for (TBTableRow *row in sections) {
85+
if (row == rowToFind) {
86+
return [NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex];
87+
}
88+
rowIndex++;
89+
}
90+
rowIndex = 0;
91+
sectionIndex++;
92+
}
93+
return nil;
94+
}
95+
8096
@end

Branch-TestBed/Branch-TestBed/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ - (IBAction)shareLinkButtonTouchUpInside:(id)sender {
264264
@"Shared from Branch's Branch-TestBed at %@.",
265265
[self.dateFormatter stringFromDate:[NSDate date]]];
266266

267-
[shareLink presentActivityViewControllerFromViewController:self anchor:nil];
267+
[shareLink presentActivityViewControllerFromViewController:self anchor:sender];
268268
}
269269

270270
- (IBAction)shareLinkAsActivityItem:(id)sender {

0 commit comments

Comments
 (0)