是否可以在共享扩展中打开完整视图控制器,而不是弹出窗口(默认)

Is it possible to open full view controller in Share extension, instead of popup (default)

我正在研究 Share extension 并且我有一个与此相关的查询。

是否可以在 Share 扩展中打开全视图控制器,我们也可以在 Share extension 中使用 Storyboard 添加 UITableView 或其他 IBoutlet

提前致谢

步骤 1: 添加新的 UIViewController 命名为 MainPageViewController.

Step2:MainInterface Storyboard 中添加新的 View Controller,将其 class 更改为 MainPageViewController 在 Storyboard 的 Custom Class 部分中,在 Identity 部分中将 Storyboard ID 设置为 MainPageViewController

Step3:打开分享扩展的info.plist

默认情况下它看起来像这样-

<key>NSExtension</key>
    <dict>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>NSExtensionActivationRule</key>
            <string>TRUEPREDICATE</string>
        </dict>
        <key>NSExtensionMainStoryboard</key>
        <string>MainInterface</string>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.share-services</string>
    </dict>

现在使用 NSExtensionPrincipalClass 键代替默认的 NSExtensionMainStoryboard 键,所以最终结果将是

<key>NSExtension</key>
        <dict>
            <key>NSExtensionAttributes</key>
        <dict>
            <key>NSExtensionActivationRule</key>
            <string>TRUEPREDICATE</string>
        </dict>
            <key>NSExtensionPrincipalClass</key>
            <string>HomeViewController</string>
            <key>NSExtensionPointIdentifier</key>
            <string>com.apple.share-services</string>
        </dict>

其中 HomeViewController 是我们新的入口点控制器。

Step4:现在,最重要的我们需要fix weird issue related with module naming .

要修复它,我们需要在 HomeViewController 文件的顶部添加 @objc(HomeViewController)

Step5: 同样为了动画演示,我们需要在viewWillAppear

中添加动画代码

See below reference code:

import UIKit

@objc(HomeViewController)

class HomeViewController : UINavigationController {

     init() {
        let viewController:UIViewController = UIStoryboard(name: "MainInterface", bundle: nil).instantiateViewController(withIdentifier: "MainPageViewController") as UIViewController
        super.init(rootViewController: viewController)
    }

    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
        })
    }
}

其中 MainPageViewController 是我们要显示的您的 MainViewController 的标识符。

Step6: 要关闭动画我们可以在 MainPageViewController class:

func hideExtensionWithCompletionHandler(completion:@escaping (Bool) -> Void) {
    UIView.animate(withDuration: 0.20, animations: {

        self.navigationController!.view.transform = CGAffineTransform(translationX: 0, y: self.navigationController!.view.frame.size.height)
    }, completion: completion)
}

在保存或取消按钮和块内调用上面的函数,我们可以调用 completeRequestcancelRequest(withError:)

func saveButtonTapped(sender: UIBarButtonItem) {
        self.hideExtensionWithCompletionHandler(completion: { (Bool) -> Void in
            self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
        })
    }

现在,做你想做的事,做双面人;-)