sharing Image from App to other app in ios - iphone

How to share an image from my app to other app?
I have a button in my UIViewController and when it is clicked it should show other app available for sharing
how can I show a pop up showing all the apps available?
how can I share it with flicker app?

Try this code
let activityItem: [AnyObject] = [self.imageView.image as! AnyObject]
let avc = UIActivityViewController(activityItems: activityItem as [AnyObject], applicationActivities: nil)
self.presentViewController(avc, animated: true, completion: nil)

Related

SwiftUI Share Sheet Crashes iPad

I was following this tutorial https://jeevatamil.medium.com/how-to-create-share-sheet-uiactivityviewcontroller-in-swiftui-cef64b26f073
to add a simple share sheet to my swiftui app. It works properly on iPhones but crashes on iPad with this error
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<UIPopoverPresentationController: 0x107d95ee0>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
Any way to get around this error? Not exactly sure what's happening here. Thanks!
You just need to set sourceView and sourceRect on the UIActivityViewController's popoverPresentationController. Here's a more complete and correct example than I've seen so far:
// Get a scene that's showing (iPad can have many instances of the same app, some in the background)
let activeScene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
let rootViewController = (activeScene?.windows ?? []).first(where: { $0.isKeyWindow })?.rootViewController
// iPad stuff (fine to leave this in for all iOS devices, it will be effectively ignored when not needed)
activityVC.popoverPresentationController?.sourceView = rootViewController?.view
activityVC.popoverPresentationController?.sourceRect = .zero
rootViewController?.present(activityVC, animated: true, completion: nil)
Try this code to open Actionsheet in iPad.
if let vc = UIApplication.shared.windows.first?.rootViewController{
let activityVC = UIActivityViewController(activityItems: [urlShare], applicationActivities: nil)
if isIpad{
activityVC.popoverPresentationController?.sourceView = vc.view
activityVC.popoverPresentationController?.sourceRect = .zero
}
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
}

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 can I present Facebook messenger app in activity controller view to send a message?

func pickerView(sender : UIButton) {
let message="text"
let objectsToShare = [message]
let wsActivity = WhatsAppActivity()
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: [wsActivity])
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList,UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll,UIActivityTypePrint]
self.presentViewController(activityVC, animated: true, completion: nil)
}
How can i do the same thing with Facebook messenger ?
As far as my research went, you cannot share "string" in FB messenger. This has something to do with FB messenger policy (lost link ref for this). In order to share on Facebook messenger, your "activityItems: objectsToShare" should contain an NSURL. I have tried sharing multiple NSRULs and it did not work out.
You can pass an array of [String, NSURL] for example:
func pickerView(sender : UIButton) {
let message="text"
let url = NSURL(String: "https://www.google.com")!
let objectsToShare = [message, url] //this is now an [AnyObject]
//assuming you are don't have applications to exclude
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: [wsActivity])
self.presentViewController(activityVC, animated: true, completion: nil)
}
You will find both whatsapp and FB messenger showing up in your share sheet. However, FB messenger will only use the NSURL its, and ignore the String item in the AnyObject Array. WhatsApp will show the text followed by the link.
You might be able to do what you want to do using the FB SDK. I have not tried it personally though.
If you want to present facebook app in UIActivityViewController
than not specify any excludedActivityTypes property .
Because excludedActivityTypes: [String]? //default is nil.`
so just use as
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: [wsActivity])
self.presentViewController(activityVC, animated: true, completion: nil)
it will show you all option including facebook , twitter ext.

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/