Skip to content

Instantly share code, notes, and snippets.

@arashkashi
Created August 20, 2019 07:34
Show Gist options
  • Save arashkashi/bcffde1e35c7e406de52d9dff0127d41 to your computer and use it in GitHub Desktop.
Save arashkashi/bcffde1e35c7e406de52d9dff0127d41 to your computer and use it in GitHub Desktop.
Add share controller
//
// TestViewController.swift
// Balance
//
// Created by ArashK on 2019-08-01.
// Copyright © 2019 Congruent Tech. UG. All rights reserved.
//
import UIKit
import CloudKit
import CoreData
class MyShareController: UICloudSharingController {
var onDisappear: (() -> ())? = nil
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
onDisappear?()
}
}
class MyShareControllerWrapper: UIViewController {
var vc: MyShareController? = nil
@IBAction func onTapToClose(_ sender: Any) {
self.dismiss(animated: true) {
}
}
fileprivate func initShareController() {
let vc = self.getShareController(share: self.share!)
DispatchQueue.main.async { [weak self] in
vc.onDisappear = { [weak vc] in
vc?.view.removeFromSuperview()
vc?.removeFromParent()
self?.dismiss(animated: true) {}
}
self?.addChild(vc)
self?.view.addSubview(vc.view)
vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
vc.didMove(toParent: self)
vc.view.setNeedsLayout()
}
self.vc = vc
}
override func viewDidLoad() {
super.viewDidLoad()
initShareController()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let valid = self.vc, vc?.parent == nil {
valid.removeFromParent()
valid.view.removeFromSuperview()
initShareController()
}
}
var share: CKShare? = nil
func getShareController(share: CKShare) -> MyShareController {
let result = MyShareController(share: share, container: CKContainer.default())
result.availablePermissions = [.allowReadWrite]
result.popoverPresentationController?.sourceView = self.view
result.delegate = self
return result
}
}
extension MyShareControllerWrapper: UICloudSharingControllerDelegate {
func cloudSharingController(_ csc: UICloudSharingController, failedToSaveShareWithError error: Error) {
print(error.localizedDescription)
}
func itemTitle(for csc: UICloudSharingController) -> String? {
return "OOOOItem title"
}
}
@0xjorgev
Copy link

This was helpful, thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment