使用自定义类而不是 SLComposeServiceViewController 共享扩展

Ram*_*Ram 5 ios swift ios8-share-extension share-extension

我目前正在使用共享扩展功能。

在这里,当我搜索很多时,似乎总是使用 SLComposeServiceViewController 将带有一些文本内容的图像共享到特定应用程序。

但我希望自定义 UIViewController 类加载了从照片应用程序中选择的图像,在打开时它就像邮件编辑器一样 mail extension on share sheet

所以我得到了这个示例,并按照以下 git hub 链接(第二个)中的说明操作:

是的,它似乎4 years ago是迁移并转换为Swift 4.1版本。在我修改了这些更改并对其进行调试之后。它不像在 GitHub gif 上那样工作。是的,在初始时间,当我在共享表上触摸扩展应用程序时,我可以显示和关闭控制器。但是在我驳回并再次尝试呈现之后,这次它没有在 GitHub gif 上做出反应。

Martin 只显示和隐藏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)
}
}
Run Code Online (Sandbox Code Playgroud)

如何查找和修复上述尝试中的问题或如何自定义 UIViewController 布局而不是使用SLComposeServiceViewController

Mat*_*san 1

在您的视图控制器上(按钮操作方法)

目标C

完成流程后调用此方法

[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
Run Code Online (Sandbox Code Playgroud)

当您/用户取消流程时调用此方法

NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil];
[self.extensionContext cancelRequestWithError:error];
Run Code Online (Sandbox Code Playgroud)

迅速

完成流程后调用此方法

self.extensionContext?.completeRequestReturningItems([], completionHandler:nil)
Run Code Online (Sandbox Code Playgroud)

当您/用户取消流程时调用此方法

let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
self.extensionContext!.cancelRequestWithError(cancelError)
Run Code Online (Sandbox Code Playgroud)

如果您有任何 API 错误

self.extensionContext!.cancelRequestWithError(error as NSError)
Run Code Online (Sandbox Code Playgroud)

更新

在您的代码中,您没有在此方法中调用完成处理程序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)
    })
}
Run Code Online (Sandbox Code Playgroud)