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

时间:2022-09-07 07:45:35

I have included the 1Password extension in my Browser app by following this question.

我已按照此问题在浏览器应用中添加了1Password扩展程序。

However, I already have an iOS 8 Share Sheet (ActivityViewController) on my app and would like to include the 1Password inside this share sheet inside of using it's own. My app is in Swift but the 1Password is in Objective-C.

但是,我已经在我的应用程序上有一个iOS 8共享表(ActivityViewController),并希望将1Password包含在此共享表中使用它自己的内部。我的应用程序在Swift中,但1Password在Objective-C中。

Here's the code I'm using for the ActivityViewController:

这是我用于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)

        }
    }
}

And here's how the 1Password Extension code:

以下是1Password扩展代码:

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

I tried using the code for the extension in the applicationActivities part of the UIActivityViewController, but it doesn't work (Build Failed).

我尝试在UIActivityViewController的applicationActivities部分使用扩展代码,但它不起作用(Build Failed)。

Is it possible to have the 1Password extension inside the ActivityViewController?

是否可以在ActivityViewController中包含1Password扩展?

Thanks,

PastaCoder

1 个解决方案

#1


I have contacted Rad Azzouz from AgileBits Support through Github (you can find the detailed issue here) and we managed to solve the issue.

我通过Github联系了AgileBits支持的Rad Azzouz(你可以在这里找到详细的问题),我们设法解决了这个问题。

Here is the code used for the ActivityViewController with the 1Password extension.

以下是用于具有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)
    })
}

#1


I have contacted Rad Azzouz from AgileBits Support through Github (you can find the detailed issue here) and we managed to solve the issue.

我通过Github联系了AgileBits支持的Rad Azzouz(你可以在这里找到详细的问题),我们设法解决了这个问题。

Here is the code used for the ActivityViewController with the 1Password extension.

以下是用于具有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)
    })
}