如何在我的 Swift ActivityViewController 中包含一个 Objective-C 扩展?

How to include an Objective-C Extension in my Swift ActivityViewController?

我已按照此 question.

在我的浏览器应用程序中包含 1Password 扩展程序

但是,我的应用程序上已经有一个 iOS 8 Share Sheet (ActivityViewController),我想在使用它自己的共享 sheet 中包含 1Password。我的应用程序在 Swift 但 1Password 在 Objective-C。

这是我用于 ActivityViewController 的代码:

@IBAction func _shareButton(sender: UIButton) {

    var shareContent = _searchBar.text

    if let myWebsite = NSURL(string: "\(_searchBar.text)")
    {
        let objectsToShare = [myWebsite]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            // The app is running on an iPad, so you have to wrap it in a UIPopOverController
            var popOver: UIPopoverController = UIPopoverController(contentViewController: activityVC)
            let rect = CGRect(origin: CGPoint(x: 559, y: 44), size: CGSize(width: 38, height: 32))
            // if your "share" button is a UIBarButtonItem
            popOver.presentPopoverFromRect(rect, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)

        } else {
            self.presentViewController(activityVC, animated: true, completion: nil)

        }
    }
}

1Password 扩展代码如下:

@IBAction func onePasswordButton(sender: AnyObject) {
    OnePasswordExtension.sharedExtension().fillItemIntoWebView(self._webView, forViewController: self, sender: sender, showOnlyLogins: false, completion: {(Bool) in
        // error handling
    })
}

我尝试使用 UIActivityViewController applicationActivities 部分中的扩展代码,但它不起作用(构建失败)。

是否可以在 ActivityViewController 中使用 1Password 扩展?

谢谢,

PastaCoder

我已经通过 Github 联系了 AgileBits Support 的 Rad Azzouz(您可以找到详细的问题 here),我们设法解决了这个问题。

这是用于扩展名为 1Password 的 ActivityViewController 的代码。

@IBAction func showShareSheet(sender: AnyObject) -> Void {
    var onePasswordExtension = OnePasswordExtension.sharedExtension()

    // Create the 1Password extension item.
    onePasswordExtension.createExtensionItemForWebView(self.webView, completion: {(extensionItem, error) -> Void in
        if extensionItem == nil {
            println("Failed to create an extension item: <%@>", error)
            return
        }
        // Initialize the 1Password extension item property
        self.onePasswordExtensionItem = extensionItem

        var activityItems: NSArray = [ self ]; // Add as many custom activity items as you please

        // Setting up the activity view controller
        var activityViewController = UIActivityViewController(activityItems: activityItems as [AnyObject], applicationActivities: nil)

        if sender.isKindOfClass(UIBarButtonItem) {
            self.popoverPresentationController?.barButtonItem = sender as! UIBarButtonItem
        }
        else if sender.isKindOfClass(UIView) {
            self.popoverPresentationController?.sourceView = sender.superview
            self.popoverPresentationController?.sourceRect = sender.frame
        }

        activityViewController.completionWithItemsHandler = {(activityType, completed, returnedItems, activityError) -> Void in
            if onePasswordExtension.isOnePasswordExtensionActivityType(activityType) {
                if (returnedItems.count > 0) {
                    onePasswordExtension.fillReturnedItems(returnedItems, intoWebView: self.webView, completion: { (success, returnedItemsError) -> Void in
                        if success == false {
                            println("Failed to fill login in webview: <%@>", returnedItemsError)
                        }
                    })
                }
                else {
                    // Code for other custom activity types
                }
            }
        }

        self.presentViewController(activityViewController, animated: true, completion: nil)
    })
}