简体   繁体   中英

Facebook Messenger SDK to share text and url to facebook friends

I m trying to send text message which also contains urls to my friends on fb messenger, but not getting any way to send them.

I have tried this

 let result = FBSDKMessengerSharer.messengerPlatformCapabilities().rawValue & FBSDKMessengerPlatformCapability.Image.rawValue
    if result != 0 {
        let content: FBSDKShareLinkContent = FBSDKShareLinkContent()

        content.contentURL = NSURL(string: Urls().WEONE_ITUNES_TINYURL)
        content.contentDescription = "Dscription"
        content.contentTitle = "Title"
        let facebookSendButton: FBSDKSendButton = FBSDKSendButton()
        facebookSendButton.shareContent = content
        facebookSendButton.sendActionsForControlEvents(UIControlEvents.TouchUpInside)

    } else {
        Utils().alertView(self, title: "Cannot Send Message", message: "Your device is not able to send Facebook Messenger messages.")
    }

but this is only for sharing links

I tried sending message using urlscheme also but it just opens up the fb messenger:

    if UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb-messenger-api://")!) {
        var msgString = "Hello World: https://randomurl.com"
        let urlStringEncoded = msgString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
        var urlString = "fb-messenger://messaging?text=\(urlStringEncoded!)"
        UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
    }
    else {
        print("Failed to open fb-messenger App ")
    }

In android it is possible by this

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Your message");
    sendIntent.setType("text/plain");
    sendIntent.setPackage("com.facebook.orca");

   try {
        startActivity(sendIntent);
   } catch (android.content.ActivityNotFoundException ex) {
        ToastHelper.show(this, "Please Install Facebook Messenger");
   }

This is sent from android

这是从android发送的

Any help will be appreciated

Thanks in advance

Same as Виктор Иванов answer in Swift:

            let content = FBSDKShareLinkContent()
            content.quote = "A nice quote here..."
            content.contentURL = URL(string: "Your_Link")

            let messageDialog = FBSDKMessageDialog()
            messageDialog.delegate = nil
            messageDialog.shareContent = content

            if messageDialog.canShow() {
                messageDialog.show()
            }

.imageURL, .contentTitle, .contentDescription are now deprecated

Try this:

FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:@"YOUR_LINK"];
content.imageURL = [NSURL URLWithString:@"SOME_IMAGE"];
content.contentTitle = @"Awesome title here!";
content.contentDescription = @"Some description maybe...";
content.quote = @"A nice quote here...";

FBSDKMessageDialog *messageDialog = [[FBSDKMessageDialog alloc] init];
messageDialog.delegate = nil;
[messageDialog setShareContent:content];

if ([messageDialog canShow]) {
    [messageDialog show];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM