How to share TableView-output? - swift

In an app I'm building, see screenshot, I want to make use of the sharing option. The user can make lists and reorder the rows. I would than like to be able to share the complete TableView but I don't know how to implement the code in the UIActivityViewController. How do I get the full TableView output as an activityItem?
The code I'm using.
#IBAction func shareButton(_ sender: Any) {
let activityVC = UIActivityViewController(activityItems: [how to get the TableView output here?], applicationActivities: nil)
self.present(activityVC, animated: true, completion: nil)
}
Image: app-screenshot
I just started in this fantastic Swift-world and are proud what I made by now, but I really need a helping hand on this one! Thanks in advance!

Related

UIActivityViewController iOS12.2: strange activity type titles

I'm implementing sharing functionality in my app. I'm sharing some text.
I found that some activity types (such as Mail, Message) in sharing action sheet has strange names (see image below).
On iOS11 everything is OK.
Does anyone know how to fix that?
Code sample:
let activityViewController: UIActivityViewController = UIActivityViewController(
activityItems: ["Hey, look at this URL: https://stackoverflow.com"],
applicationActivities: nil)
activityViewController.completionWithItemsHandler = { [weak self] _, _, _, _ in
//some actions on completion
}
present(activityViewController, animated: true, completion: nil)
The names of apps/actions in the share sheet are not under your control unless you were the one who created the action. I would expect these strange names to disappear in a release build / when it's on testflight so could be worth giving that a shot.

UIActivityViewController - Can't AirDrop a more than one object type simultaneously

I'd like to be able to AirDrop a text file and an image at the same time using the UIActivityViewController. The code below works fine to send both file types via iMessage or eMail, but it fails when I try to use AirDrop. The code works fine for AirDropping 2 images or 2 text files, but not for one of each.
#IBAction func shareImage(_ sender: UIButton)
{
// can't seem to AirDrop a mixture of file types. ie. can send 2 images, or 2 data files, but not an image and a data file
let fileToSend: NSURL = NSURL(fileURLWithPath: dataFile!)
let image = imageView.image!
let objectsToShare = [fileToSend, image] as [Any]
let controller = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
controller.excludedActivityTypes = [UIActivityType.postToFacebook, UIActivityType.postToTwitter, UIActivityType.postToWeibo, UIActivityType.print, UIActivityType.copyToPasteboard, UIActivityType.assignToContact, UIActivityType.saveToCameraRoll, UIActivityType.postToFlickr, UIActivityType.postToTencentWeibo]
self.present(controller, animated: true, completion: nil)
}
AirDrop to an iOS device does not support sending multiple different types, but sending to a mac does. Until Apple changes that there is no code change that you can do to "fix" this.

DropBox Chooser and documentsPicker for Swift developers 3.0

While the Chooser implementations for iOS are present Here. It is however limited to Objective-C. Is it possible to create a chooser in swift manually?
(A dropBox chooser)
I am also unable to sufficiently call the documentspicker functions, where one can pull any document from any app the user may have installed.
Thank you in advance
⭐Solved
From your project's capabilites. First enable both the iCloud serivces and the key Sharing, now import MobileCoreServices in your class. Finally extended the following three classes inside your class.
UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Now implement the following functions..
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let myURL = url as URL
print("import result : /(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
How to call all of this? Add the following bit of code to your clickable function..
func clickFunction(){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
Click your button. The following menu will pop up ..
In the case of DropBox. Upon clicking on any item. You will be redirected to your app. And the Url will be printed.
Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.
kUTTypePDF
Also if you feel like customizing your own menu bar. Add the following code and customize your own function inside the handler
importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })
Enjoy it.

UIActivityViewController share only text

I want to share just simple text using UIActivityViewController
I am using swift, with xcode 6.3
The code is very simple, work great for photos, but not just text,
I don't want to include any web URL with the objectsToShare, just clean text
Here is the code:
var objectsToShare: ["some text to share"]!
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
//handler for completed task
activityVC.completionHandler = {(activityType, completed:Bool) in
if !completed {
println("cancelled")
return
}
reportCompleted() //report to server it's done.
}
//Excluded Activities Code
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeCopyToPasteboard ]
//activate the share view
self.presentViewController(activityVC, animated: true, completion: {
(complete) in
println(complete)
})
Now I know that it's all depends on the user have Facebook - not only on his phone, but also on his setting.
(I tried that directly from my phone 6, which has Facebook setting, and not from the simulator)
This code once worked fine, and still working good for images, and not text, but from some reason, it stop working for text sharing, Maybe the text itself has something to do with, maybe my code is not good, or maybe Facebook, change something.
Thank you for any thought about the subject.
To make thing more strange I found 2 more strange things.
The code of publish text is working on a simulator (as long as you add in the setting facebook)
If I add to the web url it also work (in device) if I remove the web url it is not working.
//code that work:
var myWebsite = NSURL(string: "http://www.someurl.com/")
let textToShare = "Swift is awesome! Check out this website about it!"
objectsToShare = [textToShare , myWebsite!]
//continue regular.
//code that doesn't work:
let textToShare = "Swift is awesome! Check out this website about it!"
objectsToShare = [textToShare ]
It seems that it is not a bug but facebook policy update:
"After discussing this with our team regarding stripping the pre-filling message area of text, this is actually BY DESIGN with the new share extension.
Pre-filling texts violates Platform Policy 2.3 ..... So in all enforcing policy 2.3 is a feature and not a bug. "
https://developers.facebook.com/bugs/949486035103197/
I read and the problem isn't iOS 8.3, is FacebookApp v29 and they haven't fix it on today update. If you uninstall your FacebookApp it works.
I can´t post images, but if you try this code with Facebook app installed and without it you will see the difference
func didPressShare(sender: AnyObject) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("This is without facebook on device")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
As they said: "Sorry for the delay everyone but we have this fix scheduled for v31. The reason for the delay is the cycle for getting updates approved for the App Store takes time. We appreciate your patience."
https://developers.facebook.com/bugs/949486035103197/
They say it will be fixed on next update.
I just tested on my example, using this code:
#IBAction func shareMoment_Action(sender: AnyObject) {
let firstActivityItem = "This is a simple text"
var activityVC = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)
activityVC.excludedActivityTypes = [
UIActivityTypePostToWeibo,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop
]
activityVC.completionWithItemsHandler = {(activityType: String!, completed: Bool, arrayOptions: [AnyObject]!, error: NSError!) in
println(activityType)
}
self.presentViewController(activityVC, animated: true, completion: nil)
}
And share is working fine for every option selected!
It is working well on my side. Please check your code again!
I have been facing the same problem but have not been able to fix it. This is what I found out so far:
Bug can be fixed when removing Facebook.app from device (but it's not a solution)
Bug seems to persist on devices with Facebook.app >v28.0 & iOS8.3
Facebook.app v28.0 & iOS8.3 still works
Facebook.app v29.0 & iOS8.2 still works
Sharing URL's seems to be affected too by this bug
It's hard to tell if Facebook actually has something to do with this bug, as they have a policy of entering bogus release notes for updates.
Since the sharing feature is not a main feature in my app, I will wait until the release of iOS8.4 and/or Facebook.app v30.0
After doing some research, I was able to find the following solution to the problem:
func shareTapped() {
let vc = UIActivityViewController(activityItems: [detailImageView.image!, "Check out these photos! http://www.photolib.noaa.gov/nssl"], applicationActivities: [])
presentViewController(vc, animated: true, completion: nil)
So in ActivityItems, you can pass an image then comma and then pass a string variable or open " and enter your text directly " in the enable above this worked with Facebook and Twitter in the UIActivityViewController.

calling a UIActivity ViewController in SKScene

I created a game in Sprite Kit using swift and I am at the point where I would like to implement a 'share' button, where when you press it the Activity View Controller pops up and let you decide how you would like to share something.
In the share, a screenshot needs to be taken and a standard text needs to be added. So far I came up with this next code, however I need to call this in a SKScene. This code only allows me to call it while I'm in a ViewController. Any help would be much appreciated.
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)
//New Excluded Activities Code
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]
//
self.presentViewController(activityVC, animated: true, completion: nil)
}
As UIActivityViewController is called on ViewController you have to create a method in ViewController presenting the UIActivityViewController. Then on the scene call this method.