我目前正在使用共享扩展功能。
在这里搜索很多东西时,似乎总是使用SLComposeServiceViewController将图像与某些文本内容共享到特定应用。
但是我希望自定义UIViewController类加载有照片应用中的选定图像,并且在打开mail extension on share sheet
时它与邮件编辑器相同
所以我得到了这个样本,我按照下面的git hub链接(第二个)中的说明进行操作:
是的,似乎是4 years ago
,所以迁移并转换为Swift 4.1
版本。在我修改了这些更改并对其进行调试之后。它不能像在GitHub gif上那样工作。是的,在初始时间,当我触摸共享表上的扩展应用程序时,我可以展示和关闭控制器。但是,在我撤消并再次尝试展示它之后,这次它在GitHub gif上没有反应。
马丁仅显示和隐藏self.view of UINavigationController Y position value
。按下Cancel or save
栏按钮后,我尝试按扩展名在共享工作表上打开它,但它不起作用,也没有反应。
这里是NSExtensionPrincipalClass - EntryViewController
import UIKit
@objc(EntryViewController)
class EntryViewController : UINavigationController {
init() {
super.init(rootViewController: ShareViewController())
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.size.height)
UIView.animate(withDuration: 0.25, animations: { () -> Void in
self.view.transform = CGAffineTransform.identity
}, completion: nil)
}
}
import UIKit
import Social
class ShareViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.navigationItem.title = "Share this"
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.cancelButtonTapped(_:)))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(self.saveButtonTapped(_:)))
}
@objc func saveButtonTapped(_ sender: UIBarButtonItem) {
self.hideExtensionWithCompletionHandler(completion: { (Bool) -> Void in
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
})
}
@objc func cancelButtonTapped(_ sender: UIBarButtonItem) {
self.hideExtensionWithCompletionHandler(completion: { (Bool) -> Void in
self.extensionContext!.cancelRequest(withError: NSError())
})
}
func hideExtensionWithCompletionHandler(completion:(Bool) -> Void) {
UIView.animate(withDuration: 0.20, animations: { () -> Void in
self.navigationController!.view.transform = CGAffineTransform(translationX: 0, y: self.navigationController!.view.frame.size.height)
}, completion: nil)
}
}
如何查找和解决以上尝试的问题,或者如何使用自定义UIViewController布局而不是使用SLComposeServiceViewController
?
答案 0 :(得分:0)
在视图控制器上(按钮操作方法)
在您完成流程后调用此
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
当您/用户取消流程时调用此
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil];
[self.extensionContext cancelRequestWithError:error];
在您完成流程后调用此
self.extensionContext?.completeRequestReturningItems([], completionHandler:nil)
当您/用户取消流程时调用此
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
self.extensionContext!.cancelRequestWithError(cancelError)
如果您有任何API错误
self.extensionContext!.cancelRequestWithError(error as NSError)
在您的代码上,您没有使用此方法hideExtensionWithCompletionHandler
func hideExtensionWithCompletionHandler(completion:@escaping (Bool) -> Void) {
UIView.animate(withDuration: 0.20, animations: { () -> Void in
self.navigationController!.view.transform = CGAffineTransform(translationX: 0, y: self.navigationController!.view.frame.size.height)
}, completion: { (_) -> Void in
completion(true)
})
}