Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have come across a few posts in different places, but do not think they fully cover what I am looking for.
By tapping a bar button item called 'Share', I would like my app to take a screenshot of the current view in the app and pop the sharing window up to share it with Twitter, Facebook and the rest of them.
You can add share button
var shareButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "shareButtonPressed:")
self.navigationItem.rightBarButtonItem = shareButton
and populate share options
func shareButtonPressed(sender: AnyObject) {
NSLog("shareButton pressed")
let stringtoshare: String = "This is a string to share"
//add current screen shot image to share
let imagetoshare = captureScreen()
let activityItems: [AnyObject] = [stringtoshare, imagetoshare]
let activityVC: UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo]
self.presentViewController(activityVC, animated: TRUE, completion: { _ in })
}
//Capture screen shot image
func captureScreen() -> UIImage
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}
Please let me know if you find any trouble in this, I am not able to test this code snip (as I am not at system) but this is how i used to handle in apps.
Related
I am using share sheet to open some of the native apps - i.e. Mail, Reminders, Notes, Messages. Below is my code to open share sheet after clicking on the share button.
I am looking for a way to identify a user click on share sheet i.e. an API that gets called when user selects an option on the share sheet. I searched and couldn't find anything, so not sure if there is a way to identify user selection?
func presentShareSheet(content: String, subject: String = "", shareButton: UIView? = nil, presentingVC: UIViewController? = nil) -> Guarantee<Void> {
return Guarantee { seal in
let itemsToShare: [Any] = [someItems]
let activityViewController = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = shareButton
activityViewController.completionWithItemsHandler = { activityType, completed, items, error in
// update color of navigation bar
seal(())
}
if let vc = presentingVC ?? UIApplication.shared.topMostViewController() {
vc.present(activityViewController, animated: true)
} else {
seal(())
}
}
}
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)
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
in my iOS app I want to upload an image as a profile photo. So, I need something like an Icon to upload the image and then a popup or such a user interface to make a photo by camera or to upload it from the photo album on device. It should be like Facebook.
I created my app with XCode 6 and Swift. Are there any ideas, solutions or other information?
THX!
I've solved my problem with following code:
func takePhoto(sender: AnyObject) {
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
var picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerControllerSourceType.Camera
var mediaTypes: Array<AnyObject> = [kUTTypeImage]
picker.mediaTypes = mediaTypes
picker.allowsEditing = true
self.presentViewController(picker, animated: true, completion: nil)
}
else{
NSLog("No Camera.")
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let selectedImage : UIImage = image
}