关闭共享扩展自定义 viewcontroller
dismiss share extension custom viewcontroller
我正在尝试创建一个与共享扩展一起使用的自定义视图控制器。一切正常,但我不知道如何关闭自定义视图控制器。基本上我从 safari 启动我的共享扩展并想完全关闭它并 return 回到 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())
更新 swift 4:
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
self.extensionContext!.cancelRequest(withError:NSError())
对于那些试图从 viewDidLoad 执行此操作的人,您需要设置一个小计时器,否则它将无法工作:
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(self.didSelectPost), userInfo: nil, repeats: false)
}
我正在尝试创建一个与共享扩展一起使用的自定义视图控制器。一切正常,但我不知道如何关闭自定义视图控制器。基本上我从 safari 启动我的共享扩展并想完全关闭它并 return 回到 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())
更新 swift 4:
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
self.extensionContext!.cancelRequest(withError:NSError())
对于那些试图从 viewDidLoad 执行此操作的人,您需要设置一个小计时器,否则它将无法工作:
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(self.didSelectPost), userInfo: nil, repeats: false)
}