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

試著忘記壹切 提交于 2019-12-16 18:06:25

问题


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

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.

Here's the code I'm using for the 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:

@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).

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

Thanks,

PastaCoder


回答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.

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

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


来源:https://stackoverflow.com/questions/30017304/how-to-include-an-objective-c-extension-in-my-swift-activityviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!