The code that I am using prepares a screenshot and opens the share window with the screenshot. However, the setInitialText does not seem to work. At least anything I enter in it is not appearing. The root does not state that it is depreciated.
let screen = UIScreen.mainScreen()
if let window = UIApplication.sharedApplication().keyWindow {
UIGraphicsBeginImageContextWithOptions(screen.bounds.size, false, 0);
window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
composeSheet.setInitialText("I just hit \(highscore) on Reflext Test! Can you beat me?")
composeSheet.addImage(image)
presentViewController(composeSheet, animated: true, completion: nil)
}
The problem is now Facebook doesn't allow pre-filled text to be posted/shared. This link will provide you information about Facebook pre-filled text policy.
https://developers.facebook.com/docs/apps/review/prefill
Related
I am trying to share an image + text via WhatsApp.
Here is my code:
let activityViewController = UIActivityViewController(activityItems: [imageNmae, "Share text"], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop]
activityViewController.completionWithItemsHandler = {(activityType: UIActivity.ActivityType?, completed: Bool, returnedItems:[Any]?, error: Error?) in
self.shareMainView.isHidden = true
}
if let viewController = UIApplication.topMostViewController {
viewController.present(activityViewController, animated: true) {
// self.shareMainView.isHidden = true
}
}
How can I achieve this?
I can see some other apps sharing a URL and it's showing both an image and text. Even Android is able to share. But if execute the above code, I can only share text and not an image.
Share URL of the image along with your text.
WhatsApp will create preview from the link, and make it appear like image + text.
There is no real way of sending image + text on WhatsApp (in Nov 2021)
I have tried different ways to show a URL icon with text but didnt succeed. Here is the code and its output snippets
Example 1:
let text = "I am text"
let myWebsite = URL(string:"https://www.youtube.com/")
let shareAll = [text, myWebsite] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Output 1:
UIActivity View Messages View
Example 2:
let myWebsite = URL(string:"https://www.youtube.com/")
let shareAll = [myWebsite] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Output 2:
UIActivity View
Required:
I want URL icon like in Output 2 for Example 1.
There is a difference between sharing data and showing data - I think in your case, showing the data that's about to be shared.
I could be wrong, but if not, you are actually able to share the data - in this case a UIImage and text - but wish to display what is about to be shared. I had this issue a while back. Here's my code. See if it can help you - basically you need to add metadata to UIActivityViewController delegate.
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let shareImage = preview.uiImage.adjustedForShareSheetPreviewIconProvider()
let imageProvider = NSItemProvider(object: shareImage)
let metadata = LPLinkMetadata()
metadata.imageProvider = imageProvider
metadata.title = "Title"
return metadata
}
Adjust shareImage and title as needed.
EDIT
I was using my code and was incomplete. adjustedForShareSheetPreviewIconProvider() is part of UIImage, and as such, it assumes this. In my code, I'm actually calling a GLKView to return this UIImage and then use it this way in the metadata.
So if your image is something returned from the URL as an icon and is (or can be converted to) a UIImage, just add it to the metadata like my code above.
Is there any specific method to share button that detect all social media apps on your iOS phone ?
example : my phone have whatsapp and twitter installed. so when I pressed share button only whatsapp and twitter came out, not facebook and any other apps not installed on my phone.
In android there's particular intent to use that kind of method. However in iOS I still can't find it.
Note : I need it for Swift 3 programmatically
Any help is very useful, Thank you
You should try this:
#IBAction func shareImageButton(_ sender: UIButton)
{
// image to share
let image = UIImage(named: "Image")
// set up activity view controller
let imageToShare = [ image! ]
let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// exclude some activity types from the list (optional)
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ]
// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}
This question already has answers here:
Screenshot in Swift iOS?
(4 answers)
Closed 5 years ago.
I would like to enable a feature on my phone that allows users to click share, and then they would be able to text their friends a message that by default included a picture of the app (right before the user hit share). I cannot figure out how to do this - and whenever I search on how to do it all I get are tutorials/answers on how to share images in the phone's photo library or by their camera. Any idea on how to do this!
// to create an image from screenshot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// to share the image
var imagesToShare = [AnyObject]()
imagesToShare.append(image)
let activityViewController = UIActivityViewController(activityItems: imagesToShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
presentViewController(activityViewController, animated: true, completion: nil)
UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
To share your screen just before user hits Share , first you will be needing to change your screen's view into an image and sharing it.
To get change your view into an image you can add this extension in your code.
//UIView extension which converts the UIView into an image.
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Pass your ViewController's view there , for example :
let imageToShare = self.view.toImage()
let activityItems : NSMutableArray = []()
activityItems.addObject(imageToShare)
let activityVC = UIActivityViewController(activityItems:activityItems as [AnyObject] , applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
I hope this helps you out.
Im trying to implement UIActivityViewController to my app, It works correctly. However, I'd like to add Instagram to the options. When searching on the internet, I've seen this reply :
"You're able to use UIActivityViewController to show up Instagram as
long there is just an UIImage inside the activity items. It won't show
up if you add more items like text, url etc. This is a bad limitation
made by Instagram.".
Is there any way, how to add Instagram even with initial text, or should I integrate another UIActivityViewController option and turn off all of the other options (FB, Twitter, mail) to show Instagram option? Maybe through if clauses for text?
Here ist the piece of code
#IBAction func ShareRecipe(_ sender: AnyObject) {
let initialText = "-Check out this app"
let img: UIImage = ImageView.image!
let activityVC = UIActivityViewController(activityItems: [img, initialText], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
self.present(activityVC, animated: true, completion: nil)
}