Skip to content

Commit c0696e0

Browse files
Merge pull request #174 from NativeScript/dtodorov/gogoout-add-call-back
Fest(share): add callback functionality
2 parents b7f2330 + 31dc783 commit c0696e0

File tree

10 files changed

+168
-22
lines changed

10 files changed

+168
-22
lines changed

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,17 @@ If the Messenger app is not installed, the Send button will be hidden. Be sure t
346346

347347
### Show Share Dialog Programmatically
348348

349-
**Note** The share dialog will try fallback to browse page sharing if user doesn't have Facebook installed (only for linkContent)
349+
**Note** The share dialog will try fallback to browse page sharing if user doesn't have Facebook installed (only for linkContent)
350350

351351
```TypeScript
352352
showShareDialog(this.linkContent);
353353
showMessageDialog(this.linkContent);
354+
showShareDialog(this.linkContent, (error:Error, result:ShareCallbackResult) => {
355+
if(!error){
356+
console.log(result.android); // com.facebook.share.Sharer.Result
357+
console.log(result.ios); // (NSDictionary * ) The results from the sharer. This may be nil or empty.
358+
}
359+
});
354360
```
355361

356362
### Hide Custom Button If Can't share
@@ -556,6 +562,12 @@ If the Messenger app is not installed, the Send button will be hidden. Be sure t
556562
```TypeScript
557563
showShareDialog(this.linkContent);
558564
showMessageDialog(this.linkContent);
565+
showShareDialog(this.linkContent, (error:Error, result:ShareCallbackResult) => {
566+
if(!error){
567+
console.log(result.android); // com.facebook.share.Sharer.Result
568+
console.log(result.ios); // (NSDictionary * ) The results from the sharer. This may be nil or empty.
569+
}
570+
});
559571
```
560572

561573
### Hide Custom Button If Can't share

demo-angular/app/pages/login/login.component.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ export class LoginComponent {
9999
}
100100

101101
onShareDialog() {
102-
Facebook.showShareDialog(this.linkContent);
102+
Facebook.showShareDialog(this.linkContent, (error, result) => {
103+
if (error) {
104+
console.error(error);
105+
return;
106+
}
107+
alert('Successfully shared');
108+
});
103109
}
104110

105111
onShareDialogPhotos() {

demo-vue/app/components/Login.vue

+7-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@
123123
});
124124
},
125125
onShareDialog: function() {
126-
showShareDialog(this.linkContent);
126+
showShareDialog(this.linkContent, (error, result) => {
127+
if (error) {
128+
console.error(error);
129+
return;
130+
}
131+
alert('Successfully shared');
132+
});
127133
},
128134
onShareDialogPhotos: function() {
129135
showShareDialog(this.photosContent);

demo/app/login-view-model.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ export class LoginViewModel extends Observable {
9898
}
9999

100100
public onShareDialog() {
101-
showShareDialog(this.linkContent);
101+
showShareDialog(this.linkContent, (error, result) => {
102+
if (error) {
103+
console.error(error);
104+
return;
105+
}
106+
alert('Successfully shared');
107+
});
102108
}
103109

104110
public onShareDialogPhotos() {

src/platforms/ios/Podfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pod 'FBSDKCoreKit', '~> 4.7'
22
pod 'FBSDKLoginKit', '~> 4.7'
3-
pod 'FBSDKShareKit', '~> 4.39.1'
3+
pod 'FBSDKShareKit', '~> 4.40'

src/share-manager.android.ts

+58-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
export * from './share-manager.common';
22

33
import {ImageSource} from 'tns-core-modules/image-source';
4-
import {android as androidApp} from 'tns-core-modules/application';
4+
import {android as androidApp, AndroidApplication} from 'tns-core-modules/application';
55
import {
66
MessageActionButton,
77
MessageGenericTemplateContent,
8-
MessageGenericTemplateImageAspectRatio, MessageMediaTemplateContent, MessageMediaTemplateMediaType,
9-
ShareAdditionContent
8+
MessageGenericTemplateImageAspectRatio,
9+
MessageMediaTemplateContent,
10+
MessageMediaTemplateMediaType,
11+
ShareAdditionContent,
12+
ShareCallbackFunction
1013
} from './share-manager.common';
1114

1215
function attachAdditionalContent(content: any, addition?: ShareAdditionContent) {
@@ -154,10 +157,58 @@ export function createShareMessageMediaTemplateContent(contentConfig: MessageMed
154157
return contentBuilder.build();
155158
}
156159

157-
export function showShareDialog(content: any) {
158-
com.facebook.share.widget.ShareDialog.show(androidApp.foregroundActivity, content);
160+
export function showShareDialog(content: any, callback?: ShareCallbackFunction) {
161+
let dialog = new com.facebook.share.widget.ShareDialog(androidApp.startActivity || androidApp.foregroundActivity);
162+
if (callback) {
163+
registerShareCallback(dialog, callback);
164+
}
165+
dialog.show(content);
159166
}
160167

161-
export function showMessageDialog(content: any) {
162-
com.facebook.share.widget.MessageDialog.show(androidApp.foregroundActivity, content);
168+
export function showMessageDialog(content: any, callback?: ShareCallbackFunction) {
169+
let dialog = new com.facebook.share.widget.MessageDialog(androidApp.startActivity || androidApp.foregroundActivity);
170+
if (callback) {
171+
registerShareCallback(dialog, callback);
172+
}
173+
dialog.show(content);
174+
}
175+
176+
177+
function registerShareCallback(dialog: com.facebook.share.widget.ShareDialog | com.facebook.share.widget.MessageDialog, callback?: ShareCallbackFunction) {
178+
let callbackManager = com.facebook.CallbackManager.Factory.create();
179+
dialog.registerCallback(callbackManager, new com.facebook.FacebookCallback<com.facebook.share.Sharer.Result>({
180+
onSuccess: function (result) {
181+
callback(null, {
182+
android: result
183+
});
184+
},
185+
onCancel: function () {
186+
callback(new Error('canceled'), null);
187+
},
188+
onError: function (e) {
189+
let errorMessage = 'Error with Facebook';
190+
if (e['getErrorMessage']) {
191+
errorMessage += ': ' + e['getErrorMessage']();
192+
}
193+
else if (e['getErrorCode']) {
194+
errorMessage += ': Code ' + e['getErrorCode']();
195+
}
196+
else {
197+
errorMessage += ': ' + e;
198+
}
199+
callback(new Error(errorMessage), null);
200+
}
201+
}));
202+
203+
let onActivityResult = (args) => {
204+
if (callbackManager.onActivityResult(args.requestCode, args.resultCode, args.intent)) {
205+
unsubscribe();
206+
}
207+
};
208+
209+
let unsubscribe = () => {
210+
androidApp.off(AndroidApplication.activityResultEvent, onActivityResult);
211+
};
212+
213+
androidApp.on(AndroidApplication.activityResultEvent, onActivityResult);
163214
}

src/share-manager.common.d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ export interface MessageMediaTemplateContent {
3838
mediaType: MessageMediaTemplateMediaType;
3939
pageID: string;
4040
button?: MessageActionButton;
41-
}
41+
}
42+
43+
export interface ShareCallbackResult {
44+
android?: any; // com.facebook.share.Sharer.Result
45+
ios?: any; // (NSDictionary * ) The results from the sharer. This may be nil or empty.
46+
}
47+
48+
export type ShareCallbackFunction = (error: Error | null, result?: ShareCallbackResult | null) => void;

src/share-manager.common.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ export interface MessageMediaTemplateContent {
3838
mediaType: MessageMediaTemplateMediaType;
3939
pageID: string;
4040
button?: MessageActionButton;
41-
}
41+
}
42+
43+
export interface ShareCallbackResult {
44+
android?: any; // com.facebook.share.Sharer.Result
45+
ios?: any; // (NSDictionary * ) The results from the sharer. This may be nil or empty.
46+
}
47+
48+
export type ShareCallbackFunction = (error: Error | null, result?: ShareCallbackResult | null) => void;

src/share-manager.d.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { ImageSource } from 'tns-core-modules/image-source';
22
import {
33
MessageGenericTemplateContent,
44
MessageMediaTemplateContent,
5-
ShareAdditionContent
5+
ShareAdditionContent,
6+
ShareCallbackFunction
67
} from './share-manager.common';
78

89
export * from './share-manager.common';
@@ -43,15 +44,17 @@ export declare function createShareMessageMediaTemplateContent(contentConfig: Me
4344
* the SDK automatically checks for the native Facebook app.
4445
* If it isn't installed, the SDK switches people to their default browser and opens the Feed Dialog. If someone wants to share an Open Graph story, the SDK opens the Web Share Dialog.
4546
* @param {any} content: Links content or photos content
47+
* @param {ShareCallbackFunction} callback: Callback for the sharing dialog
4648
*/
47-
export declare function showShareDialog(content: any): void;
49+
export declare function showShareDialog(content: any, callback?: ShareCallbackFunction): void;
4850

4951

5052
/**
5153
* The Message Dialog switches to the native Messenger for iOS app, then returns control to your app after a post is published.
5254
* @param {any} content: Links content or photos content, SUPPORTED SHARE TYPES - ShareLinkContent - ShareCameraEffectContent - ShareMessengerOpenGraphMusicTemplateContent - ShareMessengerMediaTemplateContent - ShareMessengerGenericTemplateContent UNSUPPORTED SHARE TYPES (DEPRECATED AUGUST 2018) - ShareOpenGraphContent - SharePhotoContent - ShareVideoContent - Any other types that are not one of the four supported types listed above
55+
* @param {ShareCallbackFunction} callback: Callback for the sharing dialog
5356
*/
54-
export declare function showMessageDialog(content: any): void;
57+
export declare function showMessageDialog(content: any, callback?: ShareCallbackFunction): void;
5558

5659

5760
/**

src/share-manager.ios.ts

+53-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
MessageGenericTemplateImageAspectRatio,
88
MessageMediaTemplateContent,
99
MessageMediaTemplateMediaType,
10-
ShareAdditionContent
10+
ShareAdditionContent,
11+
ShareCallbackFunction
1112
} from './share-manager.common';
1213
import {topmost} from 'tns-core-modules/ui/frame';
1314

@@ -128,12 +129,27 @@ export function createShareMessageMediaTemplateContent(contentConfig: MessageMed
128129
return content;
129130
}
130131

131-
export function showShareDialog(content: any) {
132-
FBSDKShareDialog.showFromViewControllerWithContentDelegate(currentViewController(), content, null);
132+
function getCallbackDelegate(callback?: ShareCallbackFunction) {
133+
let delegate;
134+
if (callback) {
135+
delegate = SharingDelegate.new().initWithCallback((error, result) => {
136+
if (callback) {
137+
callback(error, result);
138+
}
139+
CFRelease(delegate);
140+
delegate = undefined;
141+
});
142+
CFRetain(delegate);
143+
}
144+
return delegate;
133145
}
134146

135-
export function showMessageDialog(content: any) {
136-
FBSDKMessageDialog.showWithContentDelegate(content, null);
147+
export function showShareDialog(content: any, callback?: ShareCallbackFunction) {
148+
FBSDKShareDialog.showFromViewControllerWithContentDelegate(currentViewController(), content, getCallbackDelegate(callback));
149+
}
150+
151+
export function showMessageDialog(content: any, callback?: ShareCallbackFunction) {
152+
FBSDKMessageDialog.showWithContentDelegate(content, getCallbackDelegate(callback));
137153
}
138154

139155
// to save the memory usage, cause ios don't have static method to check if a dialog can show
@@ -172,4 +188,36 @@ export function canMessageDialogShow(content: any): boolean {
172188
return dialog.canShow;
173189
}
174190
return false;
191+
}
192+
193+
class SharingDelegate extends NSObject implements FBSDKSharingDelegate {
194+
public static ObjCProtocols = [];
195+
196+
static new(): SharingDelegate {
197+
if (SharingDelegate.ObjCProtocols.length === 0 && typeof (FBSDKSharingDelegate) !== 'undefined') {
198+
SharingDelegate.ObjCProtocols.push(FBSDKSharingDelegate);
199+
}
200+
return <SharingDelegate>super.new();
201+
}
202+
203+
private callback: ShareCallbackFunction;
204+
205+
public initWithCallback(callback: ShareCallbackFunction): SharingDelegate {
206+
this.callback = callback;
207+
return this;
208+
}
209+
210+
sharerDidCancel(sharer: FBSDKSharing): void {
211+
this.callback(new Error('canceled'), null);
212+
}
213+
214+
sharerDidCompleteWithResults(sharer: FBSDKSharing, results: NSDictionary<any, any>): void {
215+
this.callback(null, {
216+
ios: results
217+
});
218+
}
219+
220+
sharerDidFailWithError(sharer: FBSDKSharing, error: NSError): void {
221+
this.callback(new Error(error.localizedDescription), null);
222+
}
175223
}

0 commit comments

Comments
 (0)