关闭共享扩展名自定义ViewController [英] dismiss share extension custom viewcontroller
本文介绍了关闭共享扩展名自定义ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建要与共享扩展一起使用的自定义视图控制器.一切正常,但我不知道如何关闭自定义视图控制器.基本上,我从safari启动我的共享扩展程序,并希望完全忽略它并返回到safari视图.我知道这应该不难,但是我是新来的共享扩展.下面是我的基本代码.谢谢.鲍勃
//
// ShareViewController.swift
// ShareExtension
import UIKit
import Social
import MobileCoreServices
class ShareViewController: UIViewController {
private var url: NSURL?
@IBAction func backButton(_ sender: Any) {
print("back button pressed")
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
private func getURL() {
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? NSDictionary else { return }
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary,
let urlString = results["URL"] as? String,
let url = NSURL(string: urlString) {
self.url = url
}
}
})
} else {
print("error")
}
}
}
解决方案
您应使用以下两个调用之一结束共享扩展:
self.extensionContext!.completeRequestReturningItems(nil, completionHandler: nil)
self.extensionContext!.cancelRequestWithError(NSError())
苹果共享扩展文档 >
更新为快速4:
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
self.extensionContext!.cancelRequest(withError:NSError())
I am trying to create a custom view controller to be used with a share extension. Everything works, but I don't know how to dismiss the custom view controller. Basically I launch my share extension from safari and want to completely dismiss this and return back to the safari view. I know this should not be hard, but I am new to share extensions. Below is my base code. Thanks. Bob
//
// ShareViewController.swift
// ShareExtension
import UIKit
import Social
import MobileCoreServices
class ShareViewController: UIViewController {
private var url: NSURL?
@IBAction func backButton(_ sender: Any) {
print("back button pressed")
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
private func getURL() {
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? NSDictionary else { return }
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary,
let urlString = results["URL"] as? String,
let url = NSURL(string: urlString) {
self.url = url
}
}
})
} else {
print("error")
}
}
}
解决方案
You should end your sharing extensions work with one of these two calls:
self.extensionContext!.completeRequestReturningItems(nil, completionHandler: nil)
self.extensionContext!.cancelRequestWithError(NSError())
updated for swift 4:
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
self.extensionContext!.cancelRequest(withError:NSError())
这篇关于关闭共享扩展名自定义ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文