Blank UIActivityViewController swift 5 - swift

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.

Related

UIActivityViewController becomes blank on iOS 15

I have an iOS App similar to Photos App from Apple, which also has a ‘Share’ button for sharing photo, which worked well before. The code is as follows (for simplicity, I changed the sharing content to a string):
#objc func shareButtonTapped()
{
let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
if let pop = vc.popoverPresentationController
{
pop.sourceView = someView;
pop.sourceRect = shareButton.frame;
}
self.present(vc, animated: true, completion: nil);
}
But when my iPhone was upgraded to iOS 15, the UIActivityViewController that is showed up was invisible. I attach an operation video:
enter link description here
Observe carefully, in fact, UIActivityViewController has a pop-up, but it has become almost transparent.
Then, I added a statement to deliberately set the background color of its view:
#objc func shareButtonTapped()
{
let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
vc.view.backgroundColor = UIColor.systemBackground;
if let pop = vc.popoverPresentationController
{
pop.sourceView = someView;
pop.sourceRect = shareButton.frame;
}
self.present(vc, animated: true, completion: nil);
}
The operation video is as follows:
enter link description here
This code is very simple and very standard. I don't know why this happens?
Hope someone can help. Thanks in advance!
In fact, I define the shareButton within a class derived from UICollectionViewCell:
class AssetPreviewCell: UICollectionViewCell, UIScrollViewDelegate, PHLiveViewDelegate, UINavigationControllerDelegate
{
//....
}
And the complete code is:
#objc func shareButtonTapped()
{
guard let svc = self.findViewController() else { return }
let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
if let pop = vc.popoverPresentationController
{
pop.sourceView = someView;
pop.sourceRect = shareButton.frame;
}
svc.present(vc, animated: true, completion: nil);
}
The func findViewController() is the method from enter link description here
Edit:
And I have an 'Albums' button next to the 'Share' button. When the 'Albums' button is tapped, I present another view controller for add current photo to albums or remove current photo from albums according user select or deselect. This view controller is presented quiet normally. The operation video is on enter link description here . So I think the problem is just from UIActivityViewController or something else.
I find the answer. The problem is caused because I overrided the viewDidLoad function of UIActivityViewController for some reason:
override open func viewDidLoad()
{
super.viewDidLoad();
a_var += 1;
}
This cause problem on iOS15 while works well on iOS14 or iOS13.
Now I override the viewDidAppear(_) instead and the problem dissappears:
override open func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated);
a_var += 1;
}
ps: #mczmma is another account of mine. This account is restricted to ask question. I don't know the reason.
What worked for me in my use case was just to change the sourceView from
let shareAll = [textView.text]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
to
let shareAll = [textView.text]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = textView
self.present(activityViewController, animated: true, completion: nil)
So that on iPad the share shows above the textView, and it just works normally on iPhone.

SwiftUI | How to use share sheet to copy localized strings?

I added share sheet button and it works great for my English texts (copying ,sharing) BUT for the localized texts the app hung and crash when I add LocalizedStringKeys() , and when I don't add it the text is showing English.
func shareSheet(){
isShareSheetShowing.toggle()
let av = UIActivityViewController(activityItems: [LocalizedStringKey(quote)], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
}
You can use directly NSLocalizedString
func shareSheet(){
isShareSheetShowing.toggle()
let av = UIActivityViewController(activityItems: [NSLocalizedString(quote, comment: "")], applicationActivities: nil) //<--Here
UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
}

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 Text from an RTF File Swift 3

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)]

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.