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.
Related
I need to exclude .saveToCameraRoll from a UIActivityViewController but it is ignoring excludedActivityTypes.
activityVC = UIActivityViewController(activityItems: [imgUrl], applicationActivities: activities)
activityVC!.excludedActivityTypes = [.saveToCameraRoll]
I also tried creating a "custom" UIActivty and exclude it:
let saveToCameraRollActivity = UIActivity.ActivityType.init(rawValue: "\(UIActivity.ActivityType.saveToCameraRoll.rawValue)")
activityVC!.excludedActivityTypes = [saveToCameraRollActivity]
Both ways have no effect, the Save Image option is still displayed. What is the point of being able to exclude activity types if they are ignored?
The header says:
// default is nil. activity types listed will not be displayed
excludedActivityTypes: [UIActivity.ActivityType]?
I need to use my own Save Image because standard one is not respecting the file name (even though it is shown with the file icon and I am using a URL).
I am using this function and hide "save video" button :--
func otherShare(url : URL) {
let imageToShare = [ url ]
let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.copyToPasteboard, UIActivity.ActivityType.saveToCameraRoll]
self.present(activityViewController, animated: true, completion: nil)
}
I'm trying to share my app using UIActivityViewController but I can't reproduce the same effect as when I share an app from the App Store, meaning :
When I clicked the share button in the App Store I have something that looks like this :
But when I try to share my app I have this :
The code that I used was :
if let logo = UIImage(named: "myLogo"), let websiteURL = URL(string: "https://itunes.apple.com/app/idxxxxxxxxxx") {
let objectsToShare = ["My App Name", websiteURL, logo] as [Any]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: [])
if let popoverController = activityVC.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
present(activityVC, animated: true)
}
The if let popoverController = ... loop is for preventing a crash when using iPads.
What do I have to change in order to have the effect as the App Store? (to have an image with a title and a subtitle)
Moreover, once I share the app with Messages for instance, this is the difference :
How can I have the same effect? (A single image with the title and subtitle, an as a bonus, a video). I'm not sure if this is an iOS 13 problem, since all similar questions don't have the same app sharing popover.
You have to use the new LinkPresentation framework.
Which essentially involves UIActivityItemSource conformance then retrieving the metadata that will encompass the Activity View and the data you are sharing. Data can be retrieved locally or downloaded.
ExampleController: UIViewController {
var metadata: LPLinkMetadata?
func share() {
let activityView = UIActivityViewController(activityItems: [self], applicationActivities: nil)
present(activityView, animated: true)
}
...
}
extension ExampleController: UIActivityItemSource {
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return metadata
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return metadata
}
func activityViewControllerLinkMetadata(_: UIActivityViewController) -> LPLinkMetadata? {
metadata = LPLinkMetadata()
metadata.title = "Title"
metadata.originalURL = URL(string: "Description")
metadata.url = metadata.originalURL
// Using a locally stored item
metadata.iconProvider = NSItemProvider(object: UIImage(named: "image")!)
metadata.imageProvider = NSItemProvider.init(contentsOf:
Bundle.main.url(forResource: "image", withExtension: "JPG"))
return metadata
}
}
Docs:
https://developer.apple.com/documentation/uikit/uiactivityitemsource/3144571-activityviewcontrollerlinkmetada
WWDC Presentation:
https://developer.apple.com/videos/play/wwdc2019/262/
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
You can use this.
I have an application which collects the video assets of a users camera roll, using the Photos Framework and methods requestImage & requestAVAsset
I'm allowing the user to share the asset using the UIActivityViewController class and the URL from the AVAsset. My problem is that for some strange reason it won't allow me to share the video.
On the app the error message I get on the UIActivityViewController is
The item cannot be shared. Please select a different item.
Any pointers or advise is appreciated, thanks!
Here is how I present the UIActivityViewController:
let selectedVideo = self.selectedVideo //(AVAsset)
let videoURLPath = selectedVideo.url // URL
DispatchQueue.main.async {
let activityItems: [Any] = [videoURLPath]
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.airDrop, UIActivity.ActivityType.assignToContact,]
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = self.view.frame
self.present(activityViewController, animated: true, completion: nil)
}
Here's how the video URL looks:
file:///var/mobile/Media/DCIM/107APPLE/IMG_7966.MP4
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.
My app is not Live yet. I got the app ID from the App Store Connect. I want to share the app link on social media apps. I used the UIActivityViewController:
let string1 = "itms-apps://itunes.apple.com/app/idXXXXXXX"
let url = NSURL(string: string1)
let shareItems = [UIApplication.sharedApplication().openURL(url!)]
let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
Problem: It is not showing some social media apps like WhatsApp.
Solution for Swift 4 or better:
This solution also works on iPad (the solution above crashes on iPad):
if let urlStr = NSURL(string: "https://apps.apple.com/us/app/idxxxxxxxx?ls=1&mt=8") {
let objectsToShare = [urlStr]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
if let popup = activityVC.popoverPresentationController {
popup.sourceView = self.view
popup.sourceRect = CGRect(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 4, width: 0, height: 0)
}
}
self.present(activityVC, animated: true, completion: nil)
}
This is used to open the site, not to share the app:
[UIApplication.sharedApplication().openURL(url!)]
Do this instead:
if let name = URL(string: "https://itunes.apple.com/us/app/myapp/idxxxxxxxx?ls=1&mt=8"), !name.absoluteString.isEmpty {
let objectsToShare = [name]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.present(activityVC, animated: true, completion: nil)
} else {
// show alert for not available
}
for sample see this
The solutions here are all good but it's worth considering implementing the UIActivityItemSource protocol and LinkPresentation framework.
My solution achieves the following:
App icon and title showing at the top of the UIActivityViewController
Direct link to App Store for AirDrop ActivityType
Custom text for messages and emails, including an opportunity to add a link to the app on Google Play if required
Subject for emails
Doesn't use the LPMetaDataProvider fetch request (as described in this WWDC 2019 262 video) so faster to load
0. Init the UIActivityViewController:
Set the items to self:
let activityVC = UIActivityViewController(activityItems: [self], applicationActivities: nil)
Exclude certain ActivityTypes which don't apply:
activityVC.excludedActivityTypes = [.addToReadingList, .assignToContact, .markupAsPDF, .openInIBooks, .saveToCameraRoll]
For iPad set the popoverPresentationController.sourceView or .barButtonItem (this is ignored on iPhone):
activityVC.popoverPresentationController?.sourceView = myButton
Present it:
present(activityVC, animated: true, completion: nil)
1. Implement the required UIActivityItemSource methods
https://developer.apple.com/documentation/uikit/uiactivityitemsource
You must implement the placeholder method which according to the docs:
Placeholder objects do not have to contain any real data but should be configured as closely as possible to the actual data object you intend to provide.
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}
And the actual data, returning a link to the app for AirDrop and text for everything else:
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
if activityType == .airDrop {
return URL(string: "APP_STORE_URL")!
}
return "Check out the APP_NAME on the App Store: APP_STORE_URL or on the Google Play Store: PLAY_STORE_URL"
}
2. Implement the subject method
From the docs:
For activities that support a subject field, returns the subject for the item.
func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
return "EMAIL_SUBJECT" // e.g. App name
}
3. Implement the LPLinkMetaData method
From the docs:
Returns metadata to display in the preview header of the share sheet.
#available(iOS 13.0, *)
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.title = "APP_NAME"
return metadata
}
iPhone solution for Swift 5+
let url = URL(string: "https://apps.apple.com/us/app/id1535629801")!
let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(vc, animated: true)