How to share Remote URL(https://...) using UIDocumentInteractionController in iOS - ios

I want to share remote URL using UIDocumentInteractionController from my app. How will I do this?
Here is my code:
let documentInteractionController = UIDocumentInteractionController.init()
documentInteractionController.name = dataUrl
documentInteractionController.delegate = self
documentInteractionController.uti = "public.content"
documentInteractionController.presentPreview(animated: true)

Related

Why does Facebook share dialog not show up?

I am currently trying to enable the functionality to share a video through Facebook, however the share dialog does not show up.
Here's the code:
let video = ShareVideo(videoURL: Bundle.main.url(forResource: "Rap God", withExtension: "mp4")!)
let content = ShareVideoContent()
content.video = video
content.contentURL = Bundle.main.url(forResource: "Rap God", withExtension: "mp4")!
let dialog = ShareDialog(fromViewController: self, content: content, delegate: self)
dialog.shareContent = content
dialog.shouldFailOnDataError = true
dialog.mode = .shareSheet
dialog.fromViewController = self
dialog.show()
print(dialog.canShow)
print(dialog.canShow) is here equal to "false". Could please tell me how I could fix this?

Deep linking for Facebook shareLinkContent

I'm working with deep link in iOS. I am going to share a link in Facebook using `FBSDKShareLinkContent. I have created deep linking URL in Facebook like https://fb.me/****************.
I have already done AppInviteContent and it works good like this:
let content : FBSDKAppInviteContent = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "https://fb.me/****************")!
content.appInvitePreviewImageURL = NSURL(string: "http://***.***.***.***/shareImage.png" as String)!
FBSDKAppInviteDialog.showWithContent(content, delegate: self)
Now, I am sharing link in Facebook like this:
let shareLinkContent : FBSDKShareLinkContent = FBSDKShareLinkContent()
shareLinkContent.contentURL = NSURL(string: "https://example.com/a2d69835ae")!
shareLinkContent.contentTitle = "App_Name"
shareLinkContent.contentDescription = "Description"
let dialog : FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = shareLinkContent
dialog.mode = FBSDKShareDialogMode.Web
dialog.show()
How to set deep link URL (e.g. https://fb.me/****************) in this shareLinkContent.
A very apt idea is to use Branch framework for the deep linking feature. You can get to know how to use this framework from here https://branch.io/
It can be used to share your app contents to any of the social networking sites. it has the feature on universal linking and deep linking.
Remove this below code, this have been modified in latest pod version.
let dialog : FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = shareLinkContent
dialog.mode = FBSDKShareDialogMode.Web
dialog.show()
Add this code will show Facebook Share dialog:
FBSDKShareDialog.show(from: self, with: shareLinkContent, delegate: self)
This will fix your FBSDK share issue.
For APP invite try some thing like this:
{
let content : FBSDKAppInviteContent = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "https://fb.me/****************")!
content.appInvitePreviewImageURL = NSURL(string: "http://***.***.***.***/shareImage.png" as String)!
FBSDKAppInviteDialog.show(from: self, with: content, delegate: self)
}

How to add Instagram activity to UIActivityViewController [duplicate]

This question already has answers here:
Add Instagram to UIActivityViewController
(2 answers)
Closed 6 years ago.
I am not able to find solution to add Instagram option to UIActivityViewController. Could anyone help. Thanks.
Use something like this:
#IBAction func shareOnIntagram(sender: UIButton) {
let finalImage: UIImage = UIImage.imageWithView(photoView)
let instagramURL = NSURL(string: "instagram://app")
if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
let imageData = UIImageJPEGRepresentation(finalImage, 1)
let captionString = "caption"
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")
if imageData?.writeToFile(writePath, atomically: true) == false {
return
} else {
let fileURL = NSURL(fileURLWithPath: writePath)
self.documentController = UIDocumentInteractionController(URL: fileURL)
self.documentController.delegate = self
self.documentController.UTI = "com.instagram.photo"
self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)
}
} else {
print(" Instagram is not installed ")
}
}
To make this code work, don't forget to add UIDocumentInteractionControllerDelegate in the UIViewController class.

Why is my share dialog empty? Facebook SDK

I'm trying to post to facebook, which works fine but none of my strings from my content variables are being added to the share dialog.
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "<INSERT STRING HERE>")
content.contentTitle = "My Title Here"
content.contentDescription = "My description here!"
content.imageURL = NSURL(string: "<INSERT STRING HERE>")
let shareDialog = FBSDKShareDialog()
shareDialog.fromViewController = self
shareDialog.shareContent = content
shareDialog.delegate = self
if !shareDialog.canShow() {
print("cannot show native share dialog")
}
shareDialog.show()
Facebook's policies don't allow you to pre-populate status messages and require all content to be user generated. Here is what I have done and I managed to get the URL in the Facebook popup:
let activityItems = ["My app text", "", "http://google.com"]
let vc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
self.presentViewController(vc, animated: true, completion: nil)
If I open Twitter for example I get "My app text" and the URL, for Facebook I only get the URL.

Send Image and Text With Whatsapp

I need to send an image from my app with a text, I know how to send just an image or just a text, but I don't know how to combine both of them.
Just an Image:
let image = UIImage(named: "Image") // replace that with your UIImage
let filename = "myimage.wai"
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as! NSString
let destinationPath = documentsPath.stringByAppendingString("/" + filename).stringByExpandingTildeInPath
UIImagePNGRepresentation(image).writeToFile(destinationPath, atomically: false)
let fileUrl = NSURL(fileURLWithPath: destinationPath)! as NSURL
documentController = UIDocumentInteractionController(URL: fileUrl)
documentController.delegate = self
documentController.UTI = "net.whatsapp.image"
documentController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: false)
Just a text:
var whatsappURL = NSURL(string: "whatsapp://send?text=hello,%20world")
if UIApplication.sharedApplication().canOpenURL(whatsappURL!) {
UIApplication.sharedApplication().openURL(whatsappURL!)
}
How can I send an image with a text?
EDIT #1
I found a code that share an image with text to whatsapp but it's in java, can you translate it to swift?
Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.setType("image/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file)); //add image path
startActivity(Intent.createChooser(share, "Share image using"));
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(activity, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}
You can post Image or Text on WhatsApp. However you can't post both at a time as whatsapp does not provide any API that you can add caption and post image with text.
Now there is an api available for interacting with WhatsApp:
http://www.whatsapp.com/faq/en/iphone/23559013
Also Find below helpful answer:
You can use the UIDocumentInteractionController as mentioned in the 2nd answer to this question as of August 4, 2014: Share image/text through WhatsApp in an iOS app
Hope this will help.
A version of your share image code for swift 3:
let image = myUIImageVariable
let filename = "myimage.wai"
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, false)[0] as NSString
var destinationPath = documentsPath.appending("/" + filename) as NSString
destinationPath = destinationPath.expandingTildeInPath as NSString
let fileUrl = NSURL(fileURLWithPath: destinationPath as String) as NSURL
do{
try UIImagePNGRepresentation(image!)?.write(to: fileUrl as URL, options: Data.WritingOptions.atomic)
}
catch {}
let documentController = UIDocumentInteractionController(url: fileUrl as URL)
documentController.delegate = self
documentController.uti = "net.whatsapp.image"
documentController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: false)
Still does not seem work even for just sharing an image, but may save someone's time

Resources