Integrating SOAPEngine with a Swift project

Prioregroup.com edited this page Nov 13, 2016 · 1 revision

As Apple introduced Swift, their new programming language, you probably wonder how easily you can integrate it with existing Objective-C libraries that are available via CocoaPods.

You begin with creating a Podfile:

pod 'SOAPEngine', '~> 1.0.0'

and running pod install to install pods in your project.

Nice, so far so good. Cocoapods successfully integrated with Xcode project. All we need to do now is to import our pod header file into Swift project. How do you do that?

Definitely not by adding #import <SOAPEngine64/SOAPEngine.h> to a Swift file as it would result in this error:

Expected expression
Expected identifier in import declaration

To expose Objective-C files to Swift you must use Objective-C bridging header, as Mix and Match section of Using Swift with Cocoa and Objective-C documentation explains.

To do so, you must create a new Objective-C header file, let’s call it Example-Bridging-Header.h, and add it to the project. Then, you need to set Objective-C Bridging Header for your target:

Xcode Bridging Header

and finally add import statement to the bridge header:

#import <SOAPEngine64/SOAPEngine.h>

Now you can use your pods in Swift.

        var soap = SOAPEngine()
        soap.userAgent = "SOAPEngine"
        soap.actionNamespaceSlash = true
        soap.version = VERSION_1_1
        soap.responseHeader = true // use only for non standard MS-SOAP service
        
        soap.setValue("param-value", forKey: "param-name")
        soap.requestURL("http://www.my-web.com/my-service.asmx",
            soapAction: "http://www.my-web.com/My-Method-name",
            completeWithDictionary: { (statusCode : Int, 
            					 dict : [AnyHashable : Any]?) -> Void in
                
                var result:Dictionary = dict! as Dictionary
                print(result)
                
            }) { (error : Error?) -> Void in
                
                print(error)
        }

References

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.