Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,17 @@ If the Messenger app is not installed, the Send button will be hidden. Be sure t

### Show Share Dialog Programmatically

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

```TypeScript
showShareDialog(this.linkContent);
showMessageDialog(this.linkContent);
showShareDialog(this.linkContent, (error:Error, result:ShareCallbackResult) => {
if(!error){
console.log(result.android); // com.facebook.share.Sharer.Result
console.log(result.ios); // (NSDictionary * ) The results from the sharer. This may be nil or empty.
}
});
```

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

### Hide Custom Button If Can't share
Expand Down
8 changes: 7 additions & 1 deletion demo-angular/app/pages/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ export class LoginComponent {
}

onShareDialog() {
Facebook.showShareDialog(this.linkContent);
Facebook.showShareDialog(this.linkContent, (error, result) => {
if (error) {
console.error(error);
return;
}
alert('Successfully shared');
});
}

onShareDialogPhotos() {
Expand Down
8 changes: 7 additions & 1 deletion demo-vue/app/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@
});
},
onShareDialog: function() {
showShareDialog(this.linkContent);
showShareDialog(this.linkContent, (error, result) => {
if (error) {
console.error(error);
return;
}
alert('Successfully shared');
});
},
onShareDialogPhotos: function() {
showShareDialog(this.photosContent);
Expand Down
8 changes: 7 additions & 1 deletion demo/app/login-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ export class LoginViewModel extends Observable {
}

public onShareDialog() {
showShareDialog(this.linkContent);
showShareDialog(this.linkContent, (error, result) => {
if (error) {
console.error(error);
return;
}
alert('Successfully shared');
});
}

public onShareDialogPhotos() {
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pod 'FBSDKCoreKit', '~> 4.7'
pod 'FBSDKLoginKit', '~> 4.7'
pod 'FBSDKShareKit', '~> 4.39.1'
pod 'FBSDKShareKit', '~> 4.40'
65 changes: 58 additions & 7 deletions src/share-manager.android.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
export * from './share-manager.common';

import {ImageSource} from 'tns-core-modules/image-source';
import {android as androidApp} from 'tns-core-modules/application';
import {android as androidApp, AndroidApplication} from 'tns-core-modules/application';
import {
MessageActionButton,
MessageGenericTemplateContent,
MessageGenericTemplateImageAspectRatio, MessageMediaTemplateContent, MessageMediaTemplateMediaType,
ShareAdditionContent
MessageGenericTemplateImageAspectRatio,
MessageMediaTemplateContent,
MessageMediaTemplateMediaType,
ShareAdditionContent,
ShareCallbackFunction
} from './share-manager.common';

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

export function showShareDialog(content: any) {
com.facebook.share.widget.ShareDialog.show(androidApp.foregroundActivity, content);
export function showShareDialog(content: any, callback?: ShareCallbackFunction) {
let dialog = new com.facebook.share.widget.ShareDialog(androidApp.startActivity || androidApp.foregroundActivity);
if (callback) {
registerShareCallback(dialog, callback);
}
dialog.show(content);
}

export function showMessageDialog(content: any) {
com.facebook.share.widget.MessageDialog.show(androidApp.foregroundActivity, content);
export function showMessageDialog(content: any, callback?: ShareCallbackFunction) {
let dialog = new com.facebook.share.widget.MessageDialog(androidApp.startActivity || androidApp.foregroundActivity);
if (callback) {
registerShareCallback(dialog, callback);
}
dialog.show(content);
}


function registerShareCallback(dialog: com.facebook.share.widget.ShareDialog | com.facebook.share.widget.MessageDialog, callback?: ShareCallbackFunction) {
let callbackManager = com.facebook.CallbackManager.Factory.create();
dialog.registerCallback(callbackManager, new com.facebook.FacebookCallback<com.facebook.share.Sharer.Result>({
onSuccess: function (result) {
callback(null, {
android: result
});
},
onCancel: function () {
callback(new Error('canceled'), null);
},
onError: function (e) {
let errorMessage = 'Error with Facebook';
if (e['getErrorMessage']) {
errorMessage += ': ' + e['getErrorMessage']();
}
else if (e['getErrorCode']) {
errorMessage += ': Code ' + e['getErrorCode']();
}
else {
errorMessage += ': ' + e;
}
callback(new Error(errorMessage), null);
}
}));

let onActivityResult = (args) => {
if (callbackManager.onActivityResult(args.requestCode, args.resultCode, args.intent)) {
unsubscribe();
}
};

let unsubscribe = () => {
androidApp.off(AndroidApplication.activityResultEvent, onActivityResult);
};

androidApp.on(AndroidApplication.activityResultEvent, onActivityResult);
}
9 changes: 8 additions & 1 deletion src/share-manager.common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ export interface MessageMediaTemplateContent {
mediaType: MessageMediaTemplateMediaType;
pageID: string;
button?: MessageActionButton;
}
}

export interface ShareCallbackResult {
android?: any; // com.facebook.share.Sharer.Result
ios?: any; // (NSDictionary * ) The results from the sharer. This may be nil or empty.
}

export type ShareCallbackFunction = (error: Error | null, result?: ShareCallbackResult | null) => void;
9 changes: 8 additions & 1 deletion src/share-manager.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ export interface MessageMediaTemplateContent {
mediaType: MessageMediaTemplateMediaType;
pageID: string;
button?: MessageActionButton;
}
}

export interface ShareCallbackResult {
android?: any; // com.facebook.share.Sharer.Result
ios?: any; // (NSDictionary * ) The results from the sharer. This may be nil or empty.
}

export type ShareCallbackFunction = (error: Error | null, result?: ShareCallbackResult | null) => void;
9 changes: 6 additions & 3 deletions src/share-manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ImageSource } from 'tns-core-modules/image-source';
import {
MessageGenericTemplateContent,
MessageMediaTemplateContent,
ShareAdditionContent
ShareAdditionContent,
ShareCallbackFunction
} from './share-manager.common';

export * from './share-manager.common';
Expand Down Expand Up @@ -43,15 +44,17 @@ export declare function createShareMessageMediaTemplateContent(contentConfig: Me
* the SDK automatically checks for the native Facebook app.
* 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.
* @param {any} content: Links content or photos content
* @param {ShareCallbackFunction} callback: Callback for the sharing dialog
*/
export declare function showShareDialog(content: any): void;
export declare function showShareDialog(content: any, callback?: ShareCallbackFunction): void;


/**
* The Message Dialog switches to the native Messenger for iOS app, then returns control to your app after a post is published.
* @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
* @param {ShareCallbackFunction} callback: Callback for the sharing dialog
*/
export declare function showMessageDialog(content: any): void;
export declare function showMessageDialog(content: any, callback?: ShareCallbackFunction): void;


/**
Expand Down
58 changes: 53 additions & 5 deletions src/share-manager.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
MessageGenericTemplateImageAspectRatio,
MessageMediaTemplateContent,
MessageMediaTemplateMediaType,
ShareAdditionContent
ShareAdditionContent,
ShareCallbackFunction
} from './share-manager.common';
import {topmost} from 'tns-core-modules/ui/frame';

Expand Down Expand Up @@ -128,12 +129,27 @@ export function createShareMessageMediaTemplateContent(contentConfig: MessageMed
return content;
}

export function showShareDialog(content: any) {
FBSDKShareDialog.showFromViewControllerWithContentDelegate(currentViewController(), content, null);
function getCallbackDelegate(callback?: ShareCallbackFunction) {
let delegate;
if (callback) {
delegate = SharingDelegate.new().initWithCallback((error, result) => {
if (callback) {
callback(error, result);
}
CFRelease(delegate);
delegate = undefined;
});
CFRetain(delegate);
}
return delegate;
}

export function showMessageDialog(content: any) {
FBSDKMessageDialog.showWithContentDelegate(content, null);
export function showShareDialog(content: any, callback?: ShareCallbackFunction) {
FBSDKShareDialog.showFromViewControllerWithContentDelegate(currentViewController(), content, getCallbackDelegate(callback));
}

export function showMessageDialog(content: any, callback?: ShareCallbackFunction) {
FBSDKMessageDialog.showWithContentDelegate(content, getCallbackDelegate(callback));
}

// to save the memory usage, cause ios don't have static method to check if a dialog can show
Expand Down Expand Up @@ -172,4 +188,36 @@ export function canMessageDialogShow(content: any): boolean {
return dialog.canShow;
}
return false;
}

class SharingDelegate extends NSObject implements FBSDKSharingDelegate {
public static ObjCProtocols = [];

static new(): SharingDelegate {
if (SharingDelegate.ObjCProtocols.length === 0 && typeof (FBSDKSharingDelegate) !== 'undefined') {
SharingDelegate.ObjCProtocols.push(FBSDKSharingDelegate);
}
return <SharingDelegate>super.new();
}

private callback: ShareCallbackFunction;

public initWithCallback(callback: ShareCallbackFunction): SharingDelegate {
this.callback = callback;
return this;
}

sharerDidCancel(sharer: FBSDKSharing): void {
this.callback(new Error('canceled'), null);
}

sharerDidCompleteWithResults(sharer: FBSDKSharing, results: NSDictionary<any, any>): void {
this.callback(null, {
ios: results
});
}

sharerDidFailWithError(sharer: FBSDKSharing, error: NSError): void {
this.callback(new Error(error.localizedDescription), null);
}
}