ActivityViewController 不会显示任何共享选项? Swift - 3

ActivityViewController Won't show any options to share? Swift - 3

我正在尝试从 tableViewCell 共享我在核心数据中保存的事件,但每当我按下共享时,它都会显示 activityViewController 但不会给我任何共享选项。我也在我的 iPhone 上尝试了 运行,但出现了同样的问题。

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]

}

知道这里发生了什么吗?我也在我的 iPhone 上尝试了 运行,但出现了同样的问题。

我同意您自己的评估,即 shareEvents 可能为零。 您是否在分配它的行上添加了断点?在 Xcode 中,停在行:

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

并确保 shareEvents 不为零。

我不确定您的 ShareEvent 是什么样子,但您需要以其他应用程序支持的格式共享数据。将您的数据放入文件或将其转换为已知数据类型。

在此处查看一些已知的系统类型。

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

您要分享的项目只是您的 NSManagedObject 个实例。 None 的共享服务知道如何处理,所以 none 的共享服务出现了。

您可以将项目共享为图像、文本或各种标准文件类型。您需要做的是实施将您的 NSManagedObject 数据转换为某种标准格式的东西。

创建一个采用UIActivityItemSource协议的class。 class 将获取您的数据,将其转换为某种标准格式以供共享(文本、图像或任何适合您的应用程序的格式),然后 return。然后,不是将原始对象传递给 UIActivityViewController,而是传递 UIActivityItemSource 对象。

例如,假设您的项目应以文本形式共享。您将实现一个采用 UIActivityItemSource 协议的 class,并且它将包含一个函数,该函数获取您的数据并创建一个格式良好的字符串。

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
}

如果您将 MyItemSource 的实例传递给 UIActivityViewController,共享服务将收到一个字符串 "Hello world!"。