我正在尝试使用UIActivityViewController实现共享功能。使用Swift 5,Xcode 11.2.1,iOS 13.2。
我使用过UITableViewController,在行的单击中,我调用了share()方法,如下所示:
class SettingsTableViewController: UITableViewController {
var tableData = ["Share with friends"]
// MARK: - Table view methods
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "settingsCell", for: indexPath)
let row = indexPath.row
cell.textLabel?.text = tableData[row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
share()
}
func share() {
print("Share with friends")
let items: [Any] = ["This app is my favorite", URL(string: "https://www.apple.com")!]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
self.present(ac, animated: true)
}
}
点击“与朋友分享”行时出现以下错误。奇怪的是,错误不会总是发生。
2019-11-18 14:28:23.951523+0530 ShareSheetDemo[425:18210] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x282b740f0 _UIActivityActionCellTitleLabel:0x11fd94510.height >= 22 (active)>",
"<NSLayoutConstraint:0x282b692c0 V:|-(15)-[_UIActivityActionCellTitleLabel:0x11fd94510] (active, names: '|':UIView:0x11fd92fb0 )>",
"<NSLayoutConstraint:0x282b69540 V:[_UIActivityActionCellTitleLabel:0x11fd94510]-(15)-| (active, names: '|':UIView:0x11fd92fb0 )>",
"<NSLayoutConstraint:0x282b74280 'UIView-Encapsulated-Layout-Height' UIView:0x11fd92fb0.height == 50.5 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x282b740f0 _UIActivityActionCellTitleLabel:0x11fd94510.height >= 22 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-11-18 14:28:25.925309+0530 ShareSheetDemo[425:18241] [ShareSheet] connection invalidated
我看到iOS 9 here和here中存在错误。我无法就这些问题发表评论,因为我没有所需的声誉,因此无法询问是否已解决这些问题。 如这些问题所述-
此外,当我在实际设备上运行应用程序时出现错误。模拟器没有给出错误。
有人可以帮助我解决此错误吗?还是此问题已知且未解决?谢谢!!
答案 0 :(得分:0)
我不知道这是否可以解决您的问题,但是我们团队中的其他工程师之一发现,将present(_:animated:)
调用包装在调度块中似乎可以解决问题(至少在我们的应用中)。
DispatchQueue.main.async {
self.present(controller, animated: true)
}
答案 1 :(得分:0)
这是Apple的错误,而不是您的错误。忽略控制台消息并继续。