Custom View Controller
class ShareViewController: UIViewController {
override func viewDidLoad() {
let extensionItems = extensionContext?.inputItems as! [NSExtensionItem]
for extensionItem in extensionItems {
if let itemProviders = extensionItem.attachments {
for itemProvider in itemProviders {
해당 객체가 있는지 식별
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
itemProvider.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil, completionHandler: { result, error in
var image: UIImage?
if result is UIImage {
image = result as? UIImage
}
if result is URL {
let data = try? Data(contentsOf: result as! URL)
image = UIImage(data: data!)!
}
if result is Data {
image = UIImage(data: result as! Data)!
}
})
}
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
// itemProvider.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil, completionHandler: { result, error in
let data = NSData.init(contentsOf:result as! URL)
DispatchQueue.main.async {
if let urlStr = result {
self.textView.text = "\(urlStr)"
}
}
})
}
}
}
}
}
@IBAction func btnSend(_ sender: UIButton) {
// 처리후 종료
if let userDefaults = UserDefaults(suiteName: "group.com.tiekle.Action-Extension-Shared.Share") {
if let text = textView.text {
userDefaults.set(text, forKey: "text")
}
if let memo = memoTextView.text {
userDefaults.set(memo, forKey: "memo")
}
}
self.hideExtensionWithCompletionHandler(completion: { _ in
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
})
}
@IBAction func btnDismiss(_ sender: UIButton) {
self.hideExtensionWithCompletionHandler(completion: { _ in
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
})
}
func hideExtensionWithCompletionHandler(completion: @escaping (Bool) -> Void) {
UIView.animate(withDuration: 0.3, animations: {
self.navigationController?.view.transform = CGAffineTransform(translationX: 0, y:self.navigationController!.view.frame.size.height)
}, completion: completion)
}
}
Swift
복사
SLComposeServiceViewController
class ShareViewController: SLComposeServiceViewController {
override func isContentValid() -> Bool {
// Do validation of contentText and/or NSExtensionContext attachments here
// contentText : 유저가 공유하기 창을 눌러 넘어온 문자열 값(상수)
// if let currentMessage = contentText {
// let currentMessageLength = currentMessage.count
// // charactersRemaining : 문자열 길이 제한 값(상수)
// charactersRemaining = (100 - currentMessageLength) as NSNumber
//
// print("currentMessage : \(currentMessage) // 길이 : \(currentMessageLength) // 제한 : \(charactersRemaining)")
// if Int(charactersRemaining) < 0 {
// print("100자가 넘었을때는 공유할 수 없다!")
// return false
// }
// }
return true
}
override func didSelectPost() {
if let item = extensionContext?.inputItems.first as? NSExtensionItem {
if let itemProvider = item.attachments?.first as? NSItemProvider {
if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil, completionHandler: { (url, error) -> Void in
if let shareURL = url as? NSURL {
print(shareURL)
}
self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
})
}
}
}
}
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
override func configurationItems() -> [Any]! {
let item = SLComposeSheetConfigurationItem()
item?.title = "메모"
item?.tapHandler = {
let viewController = ShareViewController()
viewController.placeholder = "메모를 작성하세요"
self.pushConfigurationViewController(viewController)
}
// item?.tapHandler : 유저가 터치했을 때 호출되는 핸들러
return [item]
}
override func isContentValid() -> Bool {
if contentText.isEmpty {
return false
}
return true
}
override func didSelectPost() {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
override func configurationItems() -> [Any]! {
let item = SLComposeSheetConfigurationItem()!
item.title = "Title"
item.value = contentText
item.tapHandler = {
let viewController = ShareViewController()
self.pushConfigurationViewController(viewController)
}
let memo = SLComposeSheetConfigurationItem()!
memo.title = "memo"
memo.value = ""
memo.tapHandler = {
let viewController = ShareViewController()
viewController.placeholder = "메모를 작성하세요"
self.pushConfigurationViewController(viewController)
}
return [item, memo]
}
}
Swift
복사