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)
}
Related
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.
For SwiftUI, when you want to present the ActivityViewController you can do with 3 steps like the code below
Deprecated Code
guard let urlShare = URL(string: "https://apple.com") else { return }
let activityVC = UIActivityViewController(activityItems: [urlShare], applicationActivities: nil)
// 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
There are similar questions
How to use UIWindowScene.windows on iOS 15?
But in the case of presenting the UIActivityViewController, the view doesn't not present at all, and it presents the error log
UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow})
.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
[Presentation] Attempt to present
<UIActivityViewController: 0x10f015e00> on
<_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10290e570>
(from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10290e570>)
which is already presenting
<_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x10a923870>.
What is the equivalent of this code in iOS 15?
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
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.
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.
I am trying to figure out how to use a share sheet for my SpriteKit game and when I try to present the view controller from my scene, it gives me an error.
if self.nodeAtPoint(location) == self.shareButton {
var tempMessage = "I just got \(score) on [insert app]"
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [tempMessage], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
}
This is in my GameOverScene. How exactly do you go about managing viewcontrollers in Swift because the answers I have seen were in OBJ-C.
Thanks!
Ok so I did some digging and found a solution that works.
Basically, I used NSNoticationCenter and set up the view controller in my GameViewController file as such.
override func viewDidLoad() {
.
.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showSocialNetworks", name: "showSocialNetworksID", object: nil)
}
and made the function to be called
func showSocialNetworks(){
var tempMessage = "Insert your message to be shared on social network"
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [tempMessage], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
}
Now jump back to my GameOverScene, I set it up so when the user taps the Share Button, it performs the following code
if self.nodeAtPoint(location) == self.shareButton {
NSNotificationCenter.defaultCenter().postNotificationName("showSocialNetworksID", object: nil)
}
This may not be the cleanest way to do it, but it works for me. Hope this helps!