0

I am trying to share my saved events in core data from tableViewCell but whenever I press on share, it displays the activityViewController but won't give me any options to share it with. I tried running it on my iPhone as well and same issue appears.

class EventsTableViewController: UITableViewController {

@IBOutlet var table: UITableView!

var  eventsArray: [NSManagedObject] = []

// The Managed Object Context retrieved from the app delegate

let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext



override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    //self.navigationItem.rightBarButtonItem = self.editButtonItem
}

override func viewWillAppear(_ animated: Bool) {
    gettAllRecords()
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return eventsArray.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell

    let event = eventsArray[indexPath.row]

    let eventTitle = event.value(forKeyPath: "eTitle") as? String
    let eventLocation = event.value(forKeyPath: "eLocation") as? String
    let eventDateTime = event.value(forKeyPath: "eDateTime") as? String

    cell.titleLable.text = eventTitle
    cell.locationLable.text = eventLocation
    cell.dateTimeLable.text = eventDateTime

    return cell
}


/***********************************************************************
 *
 * This function gets all records from the database and returns
 * an array of ManagedObject
 *
 **********************************************************************/

func gettAllRecords() {

    let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Event")

    do {
        eventsArray = try managedContext.fetch(fetchRequest)

        table.reloadData()

    } catch let error as NSError {

        print("Could not fetch. \(error), \(error.userInfo)")

    }
}


// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let shareActions = UITableViewRowAction(style: .normal, title: "Share") { (_ rowAction: UITableViewRowAction, _ indexPath: IndexPath) in

        let shareEvent = self.eventsArray[indexPath.row] //I feel like shareEvent is not being populated with self.eventsArray

        let activityViewController = UIActivityViewController(activityItems: [shareEvent], applicationActivities: nil)

        self.present(activityViewController, animated: true, completion: nil)

    }

    let deleteAction = UITableViewRowAction(style: .default, title: "Delete") { (_ rowAction: UITableViewRowAction, _ indexPath: IndexPath) in
        let event = self.eventsArray[indexPath.row]
        self.managedContext.delete(event)

        (UIApplication.shared.delegate as! AppDelegate).saveContext()

        self.gettAllRecords()
    }

    shareActions.backgroundColor = UIColor.gray

    return [deleteAction, shareActions]

}

This is what it shows when i try to save it

Any idea what's going on here? I tried running it on my iPhone as well and same problem appears.

Kunj Patel
  • 75
  • 3
  • 11

3 Answers3

0

I agree with your own assessment that shareEvents may be nil. Did you add a breakpoint on the line where you assign it? In Xcode, stop at the line:

let shareEvent = self.eventsArray[indexPath.row] //I feel like shareEvent is not being populated with self.eventsArray

and ensure that shareEvents is not nil.

Mozahler
  • 4,958
  • 6
  • 36
  • 56
  • @Moxahler I did that and i think it is nil. i couldn't see any data in it. How would I fix this problem?? Thank you for your reply – Kunj Patel Apr 27 '17 at 18:48
  • https://mega.nz/#!aMo1BIrZ!tJkLZ2rOyC3naJFCnDmZtlokLd2-GCn-l3P-d7jE57Y here is the project files if you need to take a look at my whole project – Kunj Patel Apr 27 '17 at 18:50
0

I'm not sure what your ShareEvent looks like but you need to share data in a format that other applications support. Put your data in a file or transform it to a known data type.

Look here for some know system types.

https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259

tmrog
  • 107
  • 1
  • 5
  • I am not quite sure what you mean! But below is the link to my project files if you need to take a closer look. Any help would be appreciated. https://mega.nz/#!aMo1BIrZ!tJkLZ2rOyC3naJFCnDmZtlokLd2-GCn-l3P-d7jE57Y – Kunj Patel Apr 27 '17 at 18:51
  • I ran your code and the shareEvent is not nil. You cannot just share any old type and expect the share extensions, app's with url schemes, or action extensions to accept your data. All components listed in the share sheet have advertised that they support certain types of data. So, you need to convert your data into some known type. For example, you could save your data to a file and share a file NSURL. – tmrog Apr 28 '17 at 10:53
  • Thank you very much! I really appreciate it! :) – Kunj Patel Apr 30 '17 at 19:29
0

The items you're offering to share are just your NSManagedObject instances. None of the sharing services know what to do with that, so none of them appear.

You can share items as images, text, or various standard file types. What you need to do is implement something that translates your NSManagedObject data into some standard format.

Create a class that adopts the UIActivityItemSource protocol. That class will take your data, turn it into some standard format to share (text, image, or whatever is appropriate for your app) and return that. Then, instead of passing your raw objects to UIActivityViewController, you pass the UIActivityItemSource object.

For instance, say your items should be shared as text. You would implement a class that adopts the UIActivityItemSource protocol, and it would contain a function that takes your data and creates a nicely formatted string.

class MyItemSource: UIActivityItemSource {

    func activityViewController(_ activityViewController: UIActivityViewController,
                            itemForActivityType activityType: UIActivityType) -> Any? {

        // Take your data and create a string
        var stringToShare = "Hello world!"
        ...
        return stringToShare
    }

    // other UIActivityItemSource methods
}

If you pass an instance of MyItemSource to UIActivityViewController, the sharing services will receive a string that says "Hello world!".

Jeremy
  • 4,339
  • 2
  • 18
  • 12