Skip to content

Commit 0686f30

Browse files
authored
Merge pull request #1122 from BranchMetrics/CORE-1589-share-sheet-configuration
Core 1589 share sheet configuration
2 parents 2cd00ea + 2d889ac commit 0686f30

File tree

5 files changed

+54
-96
lines changed

5 files changed

+54
-96
lines changed

Branch-SDK/BranchShareLink.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "BranchUniversalObject.h"
10+
#import <LinkPresentation/LinkPresentation.h>
1011
@class BranchShareLink;
1112

1213
@protocol BranchShareLinkDelegate <NSObject>
@@ -55,7 +56,7 @@ A delegate on the BranchShareLink can further configure the share experience. Fo
5556
parameters can be changed depending on the activity that the user selects.
5657
*/
5758

58-
@interface BranchShareLink : NSObject
59+
@interface BranchShareLink : NSObject <UIActivityItemSource>
5960

6061
/**
6162
Creates a BranchShareLink object.
@@ -91,6 +92,9 @@ Presents a UIActivityViewController that shares the Branch link.
9192
// By default, we use the Branch bnc.lt link, but if you wish more control override it here.
9293
@property (nonatomic, strong, nullable) NSURL *placeholderURL;
9394

95+
// iOS 13+ : LinkPresentation metadata for the preview header.
96+
@property (nonatomic, strong, nullable) LPLinkMetadata *lpMetaData API_AVAILABLE(ios(13.0));
97+
9498
///Share text for the item. This is not the text in the iOS 13+ preview header.
9599
///This text can be changed later when the `branchShareSheetWillShare:` delegate method is called.
96100
@property (nonatomic, strong) NSString*_Nullable shareText;
@@ -120,4 +124,8 @@ Presents a UIActivityViewController that shares the Branch link.
120124

121125
///The delegate. See 'BranchShareLinkDelegate' above for a description.
122126
@property (nonatomic, weak) id<BranchShareLinkDelegate>_Nullable delegate;
127+
128+
@property void (^ _Nullable completion)(NSString * _Nullable activityType, BOOL completed);
129+
@property void (^ _Nullable completionError)(NSString * _Nullable activityType, BOOL completed, NSError*_Nullable error);
130+
123131
@end

Branch-SDK/BranchShareLink.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ - (void) shareDidComplete:(BOOL)completed activityError:(NSError*)error {
101101
if (completed && !error) {
102102
[[BranchEvent customEventWithName:BNCShareCompletedEvent contentItem:self.universalObject] logEvent];
103103
}
104+
if (self.completion)
105+
self.completion(self.activityType, completed);
106+
else
107+
if (self.completionError)
108+
self.completionError(self.activityType, completed, error);
104109
}
105110

106111
- (NSArray<UIActivityItemProvider*>*_Nonnull) activityItems {
@@ -172,6 +177,12 @@ - (void) shareDidComplete:(BOOL)completed activityError:(NSError*)error {
172177
[_activityItems addObject:item];
173178
}
174179

180+
if (@available(iOS 13.0, *)) {
181+
if (self.lpMetaData) {
182+
[_activityItems addObject:self];
183+
}
184+
}
185+
175186
return _activityItems;
176187
}
177188

@@ -188,6 +199,7 @@ - (void) presentActivityViewControllerFromViewController:(UIViewController*_Null
188199

189200
shareViewController.completionWithItemsHandler =
190201
^ (NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
202+
self->_activityType = activityType;
191203
[self shareDidComplete:completed activityError:activityError];
192204
};
193205

@@ -197,6 +209,7 @@ - (void) presentActivityViewControllerFromViewController:(UIViewController*_Null
197209
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
198210
shareViewController.completionHandler =
199211
^ (UIActivityType activityType, BOOL completed) {
212+
self->_activityType = activityType;
200213
[self shareDidComplete:completed activityError:nil];
201214
};
202215
#pragma clang diagnostic pop
@@ -311,4 +324,20 @@ - (BOOL) returnURL {
311324
return returnURL;
312325
}
313326

327+
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController // called to determine data type. only the class of the return type is consulted. it should match what -itemForActivityType: returns later
328+
{
329+
return @"";
330+
}
331+
332+
- (nullable LPLinkMetadata *)activityViewControllerLinkMetadata:(UIActivityViewController *)activityViewController API_AVAILABLE(ios(13.0))
333+
{
334+
return self.lpMetaData;
335+
}
336+
337+
- (nullable id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(nullable UIActivityType)activityType // called to fetch data after an activity is selected. you can return nil.
338+
{
339+
return nil;
340+
}
341+
342+
314343
@end

Branch-SDK/BranchUniversalObject.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ FOUNDATION_EXPORT BranchCondition _Nonnull BranchConditionRefurbished;
194194
/// @name Share Sheet Handling
195195
#if !TARGET_OS_TV
196196

197-
- (nullable UIActivityItemProvider *)getBranchActivityItemWithLinkProperties:(nonnull BranchLinkProperties *)linkProperties;
198-
199197
- (void)showShareSheetWithShareText:(nullable NSString *)shareText
200198
completion:(void (^ _Nullable)(NSString * _Nullable activityType, BOOL completed))completion;
201199

Branch-SDK/BranchUniversalObject.m

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -413,26 +413,6 @@ - (NSString *)getLongUrlWithChannel:(NSString *)channel
413413
#pragma mark - Share Sheets
414414
#if !TARGET_OS_TV
415415

416-
- (UIActivityItemProvider *)getBranchActivityItemWithLinkProperties:(BranchLinkProperties *)linkProperties {
417-
if (!self.canonicalIdentifier && !self.canonicalUrl && !self.title) {
418-
BNCLogWarning(@"A canonicalIdentifier, canonicalURL, or title are required to uniquely identify content. "
419-
"In order to not break the end user experience with sharing, Branch SDK will proceed to create a URL, "
420-
"but content analytics may not properly include this URL.");
421-
}
422-
423-
NSMutableDictionary *params = [[self getParamsForServerRequestWithAddedLinkProperties:linkProperties] mutableCopy];
424-
if (linkProperties.matchDuration) {
425-
[params setObject:@(linkProperties.matchDuration) forKey:BRANCH_REQUEST_KEY_URL_DURATION];
426-
}
427-
428-
return [Branch getBranchActivityItemWithParams:params
429-
feature:linkProperties.feature
430-
stage:linkProperties.stage
431-
campaign:linkProperties.campaign
432-
tags:linkProperties.tags
433-
alias:linkProperties.alias];
434-
}
435-
436416
- (void)showShareSheetWithShareText:(NSString *)shareText
437417
completion:(void (^ _Nullable)(NSString * _Nullable activityType, BOOL completed))completion {
438418
[self showShareSheetWithLinkProperties:nil andShareText:shareText fromViewController:nil completion:completion];
@@ -478,79 +458,12 @@ - (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties
478458
anchor:(nullable id)anchorViewOrButtonItem
479459
completion:(void (^ _Nullable)(NSString * _Nullable activityType, BOOL completed))completion
480460
orCompletionWithError:(void (^ _Nullable)(NSString * _Nullable activityType, BOOL completed, NSError*_Nullable error))completionError {
481-
482-
// Log share initiated event
483-
[[BranchEvent customEventWithName:BNCShareInitiatedEvent contentItem:self] logEvent];
484-
UIActivityItemProvider *itemProvider = [self getBranchActivityItemWithLinkProperties:linkProperties];
485-
NSMutableArray *items = [NSMutableArray arrayWithObject:itemProvider];
486-
if (shareText) {
487-
[items insertObject:shareText atIndex:0];
488-
}
489-
UIActivityViewController *shareViewController =
490-
[[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
491-
492-
if ([shareViewController respondsToSelector:@selector(completionWithItemsHandler)]) {
493-
shareViewController.completionWithItemsHandler =
494-
^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
495-
// Log share completed event
496-
if (completed && !activityError) {
497-
[[BranchEvent customEventWithName:BNCShareCompletedEvent contentItem:self] logEvent];
498-
}
499-
if (completion)
500-
completion(activityType, completed);
501-
else
502-
if (completionError)
503-
completionError(activityType, completed, activityError);
504-
};
505-
} else {
506-
#pragma clang diagnostic push
507-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
508-
// Deprecated in iOS 8. Safe to hide deprecation warnings as the new completion handler is checked for above
509-
shareViewController.completionHandler = completion;
510-
#pragma clang diagnostic pop
511-
}
512-
513-
UIViewController *presentingViewController = nil;
514-
if (viewController && [viewController respondsToSelector:@selector(presentViewController:animated:completion:)]) {
515-
presentingViewController = viewController;
516-
}
517-
else {
518-
UIViewController *rootViewController = [UIViewController bnc_currentViewController];
519-
if ([rootViewController respondsToSelector:@selector(presentViewController:animated:completion:)]) {
520-
presentingViewController = rootViewController;
521-
}
522-
}
523-
524-
if (linkProperties.controlParams[BRANCH_LINK_DATA_KEY_EMAIL_SUBJECT]) {
525-
@try {
526-
[shareViewController setValue:linkProperties.controlParams[BRANCH_LINK_DATA_KEY_EMAIL_SUBJECT] forKey:@"subject"];
527-
}
528-
@catch (NSException*) {
529-
BNCLogWarning(@"Unable to setValue 'emailSubject' forKey 'subject' on UIActivityViewController.");
530-
}
531-
}
532461

533-
if (presentingViewController) {
534-
// Required for iPad/Universal apps on iOS 8+
535-
if ([presentingViewController respondsToSelector:@selector(popoverPresentationController)]) {
536-
if ([anchorViewOrButtonItem isKindOfClass:UIBarButtonItem.class]) {
537-
UIBarButtonItem *anchor = (UIBarButtonItem*) anchorViewOrButtonItem;
538-
shareViewController.popoverPresentationController.barButtonItem = anchor;
539-
} else
540-
if ([anchorViewOrButtonItem isKindOfClass:UIView.class]) {
541-
UIView *anchor = (UIView*) anchorViewOrButtonItem;
542-
shareViewController.popoverPresentationController.sourceView = anchor;
543-
shareViewController.popoverPresentationController.sourceRect = anchor.bounds;
544-
} else {
545-
shareViewController.popoverPresentationController.sourceView = presentingViewController.view;
546-
shareViewController.popoverPresentationController.sourceRect = CGRectMake(0.0, 0.0, 40.0, 40.0);
547-
}
548-
}
549-
[presentingViewController presentViewController:shareViewController animated:YES completion:nil];
550-
}
551-
else {
552-
BNCLogWarning(@"Unable to show the share sheet since no view controller is present.");
553-
}
462+
BranchShareLink *shareLink = [[BranchShareLink alloc] initWithUniversalObject:self linkProperties:linkProperties];
463+
shareLink.shareText = shareText;
464+
shareLink.completion = completion;
465+
shareLink.completionError = completionError;
466+
[shareLink presentActivityViewControllerFromViewController:viewController anchor:anchorViewOrButtonItem];
554467
}
555468

556469
#pragma mark - Spotlight

Branch-TestBed/Branch-TestBed/ViewController.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "BranchLinkProperties.h"
1616
#import "LogOutputViewController.h"
1717
#import "AppDelegate.h"
18+
#import <LinkPresentation/LinkPresentation.h>
1819

1920
extern AppDelegate* appDelegate;
2021

@@ -338,7 +339,16 @@ - (IBAction)shareLinkButtonTouchUpInside:(id)sender {
338339
shareLink.shareText = [NSString stringWithFormat:
339340
@"Shared from Branch-TestBed at %@.",
340341
[self.dateFormatter stringFromDate:[NSDate date]]];
341-
342+
343+
if (@available(iOS 13.0, *)) {
344+
LPLinkMetadata *tempLinkMetatData = [[LPLinkMetadata alloc] init];
345+
tempLinkMetatData.title = @"Branch URL";
346+
UIImage *img = [UIImage imageNamed:@"Brand Assets"];
347+
tempLinkMetatData.iconProvider = [[NSItemProvider alloc] initWithObject:img];
348+
tempLinkMetatData.imageProvider = [[NSItemProvider alloc] initWithObject:img];
349+
shareLink.lpMetaData = tempLinkMetatData;
350+
}
351+
342352
[shareLink presentActivityViewControllerFromViewController:self anchor:sender];
343353
}
344354

0 commit comments

Comments
 (0)