I want to share some content. It my app crashes after I send email or sms. The emails and sms received. It works good with all other types.
I added Social framework and imported Social to that class
My code:
if let myWebsite = NSURL(string: "http://www.mstcode.com")
{
let messageStr = "Learn how to build iPhone apps"
let shareItems = [messageStr, myWebsite]
let activityController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
activityController.popoverPresentationController?.sourceView = sender
}
self.presentViewController(activityController, animated: true,completion: nil)
}
Related
I've created a function that will allow the user to share content via message, email, etc. However, I've only been able to include an image and a URL as separate objects. I would like to include a small image that has the url embedded, so the user can just tap the image. Is there any way to do this?
func shareApp() {
let textToShare = "Checkout this app!"
let imageToShare = UIImage(named: “ProductIcon”)
if let myWebsite = URL(string: "http://www.productwebsite.ca")
{
let objectsToShare = [textToShare, imageToShare, myWebsite] as [Any]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.view.backgroundColor = UIColor.clear
//Excluded Activities Code
activityVC.excludedActivityTypes = [UIActivity.ActivityType.airDrop, UIActivity.ActivityType.addToReadingList]
self.present(activityVC, animated: true, completion: nil)
}
}
#IBAction func shareButtonClicked(sender: UIButton) {
let textToShare = "Swift is awesome! Check out this website about it!"
if let myWebsite = NSURL(string: "http://www.codingexplorer.com/") {
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = sender
self.presentViewController(activityVC, animated: true, completion: nil)
}
}
Hi, I add sms sharing to my app, everything works fine except I'd like to be able to CANCEL an SMS after I started it.
No cancel Button is displayed in the uinavigationbar for SMS while it is present for Mail, Twitter or Facebook.
Thanks for your help
I'm developing an iOS application using swift and firebase.
I'm trying to add sharing ability, where the user can share some information from firebase to social networkings apps.
Here's the button I added:
#IBAction func text2share(sender: AnyObject) {
let text2share = "Check out this 😍✨ \r\n Business Name: \(self.BusinessNameL.text!) \r\n Phone: \(self.PhoneNumberTV.text!) \r\n Category: \(self.CategoryL.text!) \r\n Website: \(self.Website1TV.text!) \r\n in Business Wallet app 📲"
let objects2Share = [text2share]
let activityVC = UIActivityViewController(activityItems: objects2Share, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
}
When I tried it, it worked for all social networks apps except for whatsapp and Facebook!
*Here's the error I got when I try to share it to whatsapp:
*Here's What I got when I share to Facebook:
And the link is empty!
Is there anybody know how can I solve this?
And why this happens?
It's a bug on whatsapp.
Check this
thread. It discusses the same. It seems like a very recent bug as earlier the same methods used to work. Check this answer by santhu for possible workaround or wait since they have admitted to resolve the bug.
if you try this code, you can share just the URL which I was talking about.
let textToShare = "Check out this Business Name"
let appURL = NSURL(string: "http://www.google.com")! as NSURL
let objectsToShare = [textToShare, appURL]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
//New Excluded Activities Code
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]
self.presentViewController(activityVC, animated: true, completion: nil)
if you want to share your text via facebook add excludeActivities
let activityVC = UIActivityViewController(activityItems: objects2Share, applicationActivities: nil)
let excludeActivities = [UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypeMessage, UIActivityTypeMail]
activityVC.excludedActivityTypes = excludeActivities
self.presentViewController(activityVC, animated: true, completion: nil)
I want to share an URL scheme link to social app so that when the user open social page on safari, when they click on the link, the URL scheme will call to the app I set. But when I share, it only share the text. How to share the link instead of the text.
Here is the code:
#IBAction func shareButtonAction(sender: AnyObject) {
print("share")
let myShare = "TestSafari://"
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)
self.presentViewController(shareVC, animated: true, completion: nil)
}
You probably need to put a text first then the url, in that particular order. Try this:
let string: String = ...
let URL: NSURL = ...
let shareVC = UIActivityViewController(activityItems: [string, URL])
let activityViewController = UIActivityViewController(
activityItems: [image],
applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop];
presentViewController(activityViewController, animated: true, completion: nil)
use this code for Present ActivityViewController only change activityItems to your string.
I have a UIActivityViewController for a share button. For iPhone I have it as a regular UIActivityViewController and for iPad its in a PopOverViewController. This is the code I have for it
let textToShare = "Check out this website!"
if let myWebsite = NSURL(string: "http://www.apple.com/") {
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
if let popUpVC = activityVC.popoverPresentationController {
popUpVC.permittedArrowDirections = .Any
popUpVC.sourceRect = share.frame
}
self.view?.window?.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
When I press the share button on a iPad it just crashes with a (lldb). But when I have it present from a view it works but isn't in the right position. This is the code I am using for the present from a view.
popUpVC.sourceView = self.view
Try this, you have to check if the device you are currently running on responds to popoverPresentationController because popoverPresentationController is new to iOS 8 and will crash on iOS 7. It'll also be nil on iPhone because it's only in a UIPopover on iPad.
let activityViewController = UIActivityViewController(activityItems: [myText, myUrl], applicationActivities: nil)
if activityViewController.respondsToSelector("popoverPresentationController") {
// iOS8+
view.presentViewController(activityViewController, animated: true, completion: nil)
activityViewController.popoverPresentationController?.sourceView = view
} else {
view.presentViewController(activityViewController, animated: true, completion: nil)
}