how to share post on facebook or google using URLScheme - ios

I want to share an HTML link on facebook without install SDK like WhatsApp or twitter. I am using below code for share a link on facebook. But I want to share the post to direct app without an open safari browser.
Please tell me this is possible or not.I have searched alot but not find any solution.
//For share twitter
let urlWhats = "twitter://post?message=\(fileURL)"
let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let whatsappURL = URL(string: urlString!)
if UIApplication.shared.canOpenURL(whatsappURL!)
{
UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil)
}
//For Facebook
let shareURL = "https://www.facebook.com/sharer/sharer.php?u=" + url
if let url = URL(string:shareURL)
{
let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
vc.delegate=self
present(vc, animated: true)
}
Thanks in Advance

For sharing in FB make use of 'FacebookShare'
import FBSDKShareKit
class yourViewController: UIViewController {
func shareContentInFB() {
let content = FBSDKShareLinkContent()
content.contentURL = URL(string: "http://yourURL/")
content.quote = "Hey !!!!"
let dialog : FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.shareContent = content
dialog.mode = FBSDKShareDialogMode.automatic
dialog.show()
}
}
You cannot prefill text in fb using this, if you have to do it then make use of Fb graph API

Related

How to share video on WhatsApp without using UIActivityViewController?

I am currently trying to enable the functionality to share a video on WhatsApp but without using UIActivityViewController, as Tik Tok does for example. When you click on "share" on WhatsApp, it directly redirects you to WhatsApp and contacts pop up so that you can share the video.
I kinda succeeded in sharing it but with ActivityViewController, this way:
let path = Bundle.main.url(forResource: "Rap God", withExtension: "mp4")!.relativePath
let fileUrl = NSURL(fileURLWithPath: path)
controller = UIDocumentInteractionController(url: fileUrl as URL)
controller.uti = "net.whatsapp.movie"
controller.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
I would be glad to know someone can help me out.
Try using UIApplication's canOpenURL(_:) and open(_:options:completionHandler:),
let urlString = "https://stackoverflow.com/questions/60282744/how-to-share-video-on-whatsapp-without-using-uiactivityviewcontroller"
let shareString = "whatsapp://send?text=\(urlString)"
if let url = URL(string: shareString), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:]) { (completed) in
print(completed)
}
}

iOS sharing document to WhatsApp using UIDocumentInteractionController

I'm trying to share a simple .txt file to WhatsApp using UIDocumentInteractionController on iOS.
I found this FAQ of WhatsApp explaining the Custom URL Scheme for WhatsApp, but it is not explaining how to share another document to WhatsApp. This should be possible with a WhatsApp update released about 2 months ago.
I also found this answer, but it does not work for other document types. Can anyone help me?
My code, that does not work, because the URL Scheme is wrong:
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL) {
activityVc = UIDocumentInteractionController(url: URL(fileURLWithPath: txtPath))
activityVc.uti = "net.whatsapp.document"
activityVc.presentOptionsMenu(from: CGRect.zero, in: self.view, animated: true)
}
}

How to open Facebook Messenger and direct to someone?

I have FBSDK already and can use FBSDKShareKit send something on Messenger.
I want just open a chat with someone in Facebook Messenger. like this:
Click a button in my app(sample: Facebook app):
http://i.imgur.com/j60P3Ng.png
Go to Facebook Messenger and open a chat with this person:
http://i.imgur.com/yU0EXKF.png
How Can I do it?
Swift 4
UI
In my app I show a user's Facebook ID as a button:
Code
#IBAction func messengerButton(_ sender: UIButton) {
if let id = sender.titleLabel?.text {
if let url = URL(string: "fb-messenger://user-thread/\(id)") {
// Attempt to open in Messenger App first
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in
if success == false {
// Messenger is not installed. Open in browser instead.
let url = URL(string: "https://m.me/\(id)")
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!)
}
}
})
}
}
}
User's Facebook ID
The user can get another person's Facebook ID from looking them up on Facebook and then observing the URL:
I found a solution with URL Scheme:
let userID = 4
let urlStr = String(format: "fb-messenger://user-thread/%d", userID)
let theUrl = NSURL(string: urlStr)
[UIApplication.sharedApplication() .openURL(theUrl!)]
This code will open Message application and talk to Mark.
in swift 5.1, use
guard let messenger = URL(string: "fb-messenger://user-thread/your_id") else { return }
UIApplication.shared.open(messenger)

Swift - Publish directly on facebook

I created a code swift, that use facebook SDK 4.x. The code use FBSDKShareButton and worked very well.
This is the code:
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "http://alexandreccarmo.com")
content.contentTitle = "Teste"
content.contentDescription = "teste teste"
content.imageURL = NSURL(string: "xxxx/2.jpg")
let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25)
self.view.addSubview(button)
But I need to share without use facebook dialog. When the user click on button of my app I need share directly on their facebook.
I tried this:
let contentx : FBSDKShareLinkContent = FBSDKShareLinkContent()
contentx.contentURL = NSURL(string: "http://alexandreccarmo.com")
contentx.contentTitle = "Testexxxxx"
contentx.contentDescription = "teste testexxx"
contentx.imageURL = NSURL(string: "xxxx/2.jpg")
FBSDKShareAPI.shareWithContent(contentx, delegate: nil)
But I receive this error:
FacebookLogin[6721:3464359] FBSDKLog: Warning: [FBSDKAccessToken currentAccessToken] is missing publish_actions permissions
I read on facebook developers that I need to request "publish_actions" when I need to send share. But I don't understand how can i do this. Somebody knows?
So here is some code that I used to publish an image on Facebook without the Facebook SDK:
func actionTapped(sender: AnyObject) {
let objectsToShare = //reference to image
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.completionWithItemsHandler = actionCompleted
presentViewController(activityVC, animated: true, completion: nil)
}
func actionCompleted(activityType: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) {
// Return if cancelled
if (!completed) {
return
}
close()
}
You can check out these links as well:
Can I use UIActivityViewController to share on Facebook ?
http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/
UIActivityViewController share only text
I hope this helps.

Sharing images to Whatsapp using UIDocumentIntractionController in ios

Is it possible to share Image and Text from an iOS app to WhatsApp using UIDocumentIntractionController or API in iPhone application development?
Please look at this link from the FAQ:
http://www.whatsapp.com/faq/en/iphone/23559013
I have found that If your application creates photos, videos or audio notes and you’d like your users to share these media using WhatsApp, you can use the Document Interaction API to send your media to your WhatsApp contacts and groups.
You can refer this link : http://www.whatsapp.com/faq/en/iphone/23559013
In Swift 3 use this code
#IBAction func whatsappShareWithImages(_ sender: AnyObject)
{
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
if let image = UIImage(named: "whatsappIcon") {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.write(to: tempFile, options: .atomic)
self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
self.documentInteractionController.uti = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
} catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
}
Add this code in your app "plist"
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
You can also refer to small app for reference : https://github.com/nithinbemitk/iOS-Whatsapp-Share

Resources