How to Share Text from an RTF File Swift 3 - swift

I'm not even sure if this is possible, but here it goes anyway:
I have RTF files within my app, which are used to populate text views. I want my users to be able to share the text within those files, not the files themselves.
This is the code I'm currently using to share the files:
#IBAction func shareRecipeTxtBtn(_ sender: Any) {
let grabIngredientTxt = Bundle.main.url(forResource: "bananaPancakes_ing", withExtension: "rtf")
let grabDirectionTxt = Bundle.main.url(forResource: "bananaPancakes_dir", withExtension: "rtf")
let txtToShare = [grabIngredientTxt, grabDirectionTxt]
let activityViewController = UIActivityViewController(activityItems: txtToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [UIActivityType.postToFacebook, UIActivityType.postToTwitter]
self.present(activityViewController, animated: true, completion: nil)
}
The attached screenshot is the result of the code above, but it's not ideal.
What users currently see when they attempt to share a file.

why don't you grab the text being displayed from the text views
let txtToShare = [textView1.text, textView2.text]
or
let txtToShare = ["\(textView1.text)\n\(textView2.text)]

Related

Blank UIActivityViewController swift 5

I want to share a PDF file that i created with PDFKIT. This pdf file is stored in myVariables struct. Here is the code that i use to display the ActivityView when a button is pressed.
#IBAction func shareButton(_ sender: Any) {
let shareContent = [myVariables.pdfInView]
let activityController = UIActivityViewController(activityItems: shareContent,
applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
}
However, when i run my code, i just get completely blank UIActivityViewController with nothing.
You get completely blank UIActivityViewController with nothing just because your shareContent is nil.Please check that for example use below code:
let shareContent = ["Example"]
Hope it will help you.Thank you.

Share URL to Messages with Rich Preview in Swift

When I share a webpage (example: apple.com) from Safari to the Messages app, it shares a rich preview in a custom UI element with an image and the page's title and URL.
However, when I try to share a URL to Messages from my own code (even if it's one that works from Safari like apple.com), it just shares the URL as plain text. How can I get the same behavior as Safari when link sharing to Messages?
Here is my code that creates and presents the UIActivityViewController that shares the URL:
extension UIViewController {
func shareUrl(url: URL) {
let objectsToShare: Array<NSObject> = [url as NSObject]
let activityVC = UIActivityViewController(activityItems: objectsToShare,
applicationActivities: nil)
present(activityVC, animated: true)
}
}
let items = [URL(string: "https://www.apple.com")!]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
present(ac, animated: true)
More detail please see at here

UIActivityViewController Facebook sharing URL + IMAGE and URL + TEXT not working

Please don't mark duplicate already seen iOS11: UIActivityViewController not successfully sharing UIImage to 3rd party apps
So I am trying to share Image + URL with UIActivityViewController .
However when tap on facebook it only show URL, Image is not showingup
Here is my code
#IBAction func btnShareTapped(_ sender: UIButton) {
if let image = self.imgBackground.image, let jpgImage = UIImageJPEGRepresentation(image, 0.8) {
let shareItems:Array = [jpgImage,URL(string: "https://www.facebook.com")] as [Any]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo]
self.present(activityViewController, animated: true, completion: nil)
DispatchQueue.main.async {
self.present(activityViewController, animated: true, completion: nil);
}
}
}
If I remove URL then I can able to share image,
Only URL Visible for sharing
Is there any workaround to share Image with URL or any text ?
it's impossible to share images and URL at the same time currently

How to share both Image and Text together in swift?

I am trying to share both image and text in swift. but when i choose to share via facebook, messenger or whatsapp it only gives text (image is not shared). I am using UIActivityViewController for sharing.
here is my code:
func displayShareSheet(latitude:NSString?, longitude:NSString?, image:UIImage?, address:NSString? ) {
let activityViewController = UIActivityViewController(activityItems: [(latitude as NSString?)!, (longitude as NSString?)!, (image as UIImage?)!, (address as NSString?)!], applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: {}
)
}
Below is UIActivityViewController code is working for me. also attached screen shot for both the methods.
func shareImage() {
let img = UIImage(named: "SoSampleImage")
let messageStr = "Ketan SO"
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [img!, messageStr], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
Screen shot for UIActivityViewController example :
Alternative Using SLComposeViewController :
func share(){
let img = UIImage(named: "SoSampleImage")
let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
composeSheet.setInitialText("Hello, Ketan!")
composeSheet.addImage(img)
self.presentViewController(composeSheet, animated: true, completion: nil)
}
Screen shot for SLComposeViewController example :
Hope it will help you..
Do let me know if you have any query.
Try this This is working for me!!!
#IBAction func btnExport(sender: AnyObject)
{
print("Export")
let someText:String = "Hello want to share text also"
let objectsToShare:UIImage = self.imgView.image!
let sharedObjects:[AnyObject] = [objectsToShare,someText]
let activityViewController = UIActivityViewController(activityItems : sharedObjects, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook,UIActivityTypePostToTwitter]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
I achieve this with the help of VisualActivityViewController which is present in this GitHub repository
It gives me a nice, custom view as well--one that shows the user both the text and image that the user is going to share.

Sharing A Screenshot in Activity View Controller - Swift

I'm developing part of an app so that when you tap the share button, it allows you to instantly share a screenshot of your highscore along with a message. I haven't been able to produce/share a screenshot, and when I tap the share button, the app only allows me to copy my default text or "Mail" my default text, not allowing me to post to Facebook, Twitter, Messages, and more.
func shareButtonPress() {
var postPhrase = "Just hit \(highscore)! Beat it! #SwypIt"
//Generate the screenshot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var postImage = UIImage(named: "\(image)")
var activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [postPhrase, postImage!], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
}
What is the best way of going about this? Thanks!
This is how i handle sharing in my app.
func socialShare(#sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
var sharingItems = [AnyObject]()
if let text = sharingText {
sharingItems.append(text)
}
if let image = sharingImage {
sharingItems.append(image)
}
if let url = sharingURL {
sharingItems.append(url)
}
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
I have excluded a number of sharing options using .excludedActvityTypes.
Then whenever you hit the share button have it call this
socialShare(sharingText: "Just hit \(highscore)! Beat it! #SwypI", sharingImage: UIImage(named: "The screenshot you are saving"), sharingURL: NSURL(string: "http://itunes.apple.com/app/"))
The reason you are not seeing Twitter and Facebook as sharing options is because you need to be signed into them within the settings on the IPhone. Not the individual apps.
Hope this helps.
Use a SLComposeViewController.
import Social
func shareButtonPress() {
var postPhrase = "New high score \(highscore)!"
//Generate the screenshot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var postImage = UIImage(named: "\(image)")
let shareToFacebook = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
shareToFacebook.setInitialText(postPhrase)
shareToFacebook.addImage(postImage)
presentViewController(shareToFacebook, animated: true, completion: nil)
}
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/SLComposeViewController_Class/