菜鸟教程小白 发表于 2022-12-11 20:22:23

ios - 与自定义类而不是 SLComposeServiceViewController 共享扩展


                                            <p><p>我目前正在使用共享扩展功能。</p>

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

<p>但我希望自定义 UIViewController 类加载从照片应用程序中选择的图像,当打开 <code>共享表上的邮件扩展名</code></p>时,它与邮件编辑器一样

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

<ul>
<li> <a href="https://martinnormark.com/present-ios-8-share-extension-as-modal-view/" rel="noreferrer noopener nofollow">https://martinnormark.com/present-ios-8-share-extension-as-modal-view/</a> </li>
<li> <a href="https://github.com/martinnormark/ShareByMail" rel="noreferrer noopener nofollow">https://github.com/martinnormark/ShareByMail</a> </li>
</ul>

<p>是的,似乎 <code>4 年前</code> 所以迁移并转换为 <code>Swift 4.1</code> 版本。在我修改了这些更改并调试它之后。它不像在 GitHub gif 上那样工作。是的,最初时,当我在共享表上触摸扩展应用程序时,我可以呈现和关闭 Controller 。但是在我解散并再次尝试呈现它之后,这一次它在 GitHub gif 上没有反应。</p>

<p>Martin 只显示和隐藏 <code>UINavigationController Y 位置值的self.view</code>。一旦我按下 <code>Cancel 或 save</code> 栏按钮,然后我尝试通过共享表上的扩展触摸打开它,它不起作用并且那里没有反应。</p>

<p>这里是 <code>NSExtensionPrincipalClass - EntryViewController</code></p> 的源代码

<pre><code>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: { () -&gt; 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 = &#34;Share this&#34;

    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) -&gt; Void in
      self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
    })
}

@objc func cancelButtonTapped(_ sender: UIBarButtonItem) {
    self.hideExtensionWithCompletionHandler(completion: { (Bool) -&gt; Void in
      self.extensionContext!.cancelRequest(withError: NSError())
    })
}

func hideExtensionWithCompletionHandler(completion:(Bool) -&gt; Void) {

    UIView.animate(withDuration: 0.20, animations: { () -&gt; Void in
      self.navigationController!.view.transform = CGAffineTransform(translationX: 0, y: self.navigationController!.view.frame.size.height)
    }, completion: nil)
}
}
</code></pre>

<p>如何查找和修复上述尝试中的问题或如何进行自定义 UIViewController 布局而不是使用 <code>SLComposeServiceViewController</code>?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在您的 ViewController 上(按钮操作方法)</p>

<h3> objective-c </h3>

<p>当你完成你的过程时调用它</p>

<pre><code> completionHandler:nil];
</code></pre>

<p>当您/用户取消您的流程时调用它</p>

<pre><code>NSError *error = ;
;
</code></pre>

<h3> swift </h3>

<p>当你完成你的过程时调用它</p>

<pre><code>self.extensionContext?.completeRequestReturningItems([], completionHandler:nil)
</code></pre>

<p>当您/用户取消您的流程时调用它</p>

<pre><code>let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
self.extensionContext!.cancelRequestWithError(cancelError)
</code></pre>

<p>如果您有任何 API 错误</p>

<pre><code>self.extensionContext!.cancelRequestWithError(error as NSError)
</code></pre>

<h3>更新</h3>

<p>在您的代码中,您没有在此方法中调用完成处理程序 <code>hideExtensionWithCompletionHandler</code></p>

<pre><code>func hideExtensionWithCompletionHandler(completion:@escaping (Bool) -&gt; Void) {
    UIView.animate(withDuration: 0.20, animations: { () -&gt; Void in
      self.navigationController!.view.transform = CGAffineTransform(translationX: 0, y: self.navigationController!.view.frame.size.height)
    }, completion: { (_) -&gt; Void in
      completion(true)
    })
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 与自定义类而不是 SLComposeServiceViewController 共享扩展,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51382467/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51382467/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 与自定义类而不是 SLComposeServiceViewController 共享扩展