SOAPEngine


Version Language Platform License codebeat badge Twitter: @DaniloPriore

这个通用的 SOAP 客户端允许您使用您的 iOS 应用、Mac OS X 应用和 Apple TV 应用访问网络服务。

使用此框架,您可以创建支持 SOAP 客户端协议的 iPhoneiPadMac OS XApple TV 应用。此框架能够使用 SOAP 标准协议执行远程网络服务的方法。

功能


iOS 的要求


Mac OS X 的要求


Apple TV 的要求


局限性


已知问题


Xcode 8.x 或更高版本的安全性


从新的 Xcode 8 开始,应用程序需要额外的设置,如果此设置不存在,您将看到如下日志消息

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

要解决此问题,请在 info.plist 中添加几个键,步骤如下

  1. 打开您项目的 info.plist 文件。
  2. 添加一个名为 NSAppTransportSecurity 的键,类型为 Dictionary。
  3. 添加一个名为 NSAllowsArbitraryLoads 的子键,类型为 Boolean,并将其值设置为 YES,如下图所示。

NSAppTransportSecurity

参考链接: http://stackoverflow.com/a/32631185/4069848

如何使用


使用 Delegates(委托)

import SOAPEngine64

class ViewController: UIViewController, SOAPEngineDelegate {

	var soap: SOAPEngine = SOAPENgine()

	override func viewDidLoad() {
		soap.delegate = self
		soap.actionNamespaceSlash = true
		soap.setValue("Genesis", forKey: "BookName")
		soap.setIntegerValue(1, forKey: "chapter")

		// standard soap service (.asmx)
		soap.requestURL("http://www.prioregroup.com/services/americanbible.asmx",
	    		soapAction: "http://www.prioregroup.com/GetVerses")
	}

	func soapEngine(_ soapEngine: SOAPEngine!, didFinishLoadingWith dict: [AnyHashable : Any]!, data: Data!) 
	{
		let dict = soapEngine.dictionaryValue()
		print(dict)
	}
}

使用 Block programming(块编程)

import SOAPEngine64

class ViewController: UIViewController {

	var soap: SOAPEngine = SOAPENgine()

	override func viewDidLoad() {
		super.viewDidLoad()
		soap.actionNamespaceSlash = true
		soap.setValue("Genesis", forKey: "BookName")
        	soap.setIntegerValue(1, forKey: "chapter")
        
        	soap.requestURL("http://www.prioregroup.com/services/americanbible.asmx",
	    		    soapAction: "http://www.prioregroup.com/GetVerses",
		completeWithDictionary: { (statusCode: Int?, dict: [AnyHashable: Any]?) -> Void in
                            
			let book:NSDictionary = dict! as NSDictionary
			let verses = book["BibleBookChapterVerse"] as! NSArray
			print(verses)

		}) { (error: Error?) -> Void in
			print(error!)
		}
	}
}

使用 Notifications(通知)

import SOAPEngine64

class ViewController: UIViewController {

	var soap: SOAPEngine = SOAPENgine()

	override func viewDidLoad() {
		super.viewDidLoad()

		NotificationCenter.default.addObserver(self, 
			selector: #selector(soapEngineDidFinishLoading(_:)), 
			name: NSNotification.Name.SOAPEngineDidFinishLoading, 
			object: nil)

		soap.actionNamespaceSlash = true
		soap.setValue("Genesis", forKey: "BookName")
		soap.setIntegerValue(1, forKey: "chapter")

		// standard soap service (.asmx)
		soap.requestURL("http://www.prioregroup.com/services/americanbible.asmx",
	    	soapAction: "http://www.prioregroup.com/GetVerses")
	}

	@objc func soapEngineDidFinishLoading(_ notification: NSNotification) {
		let engine = notification.object as? SOAPEngine
		let dict = engine()
		print(dict)
	}
}

Synchronous(同步) 请求

import SOAPEngine64

class ViewController: UIViewController {

	var soap: SOAPEngine = SOAPENgine()

	override func viewDidLoad() {
		super.viewDidLoad()
		soap.actionNamespaceSlash = true
		soap.setValue("Genesis", forKey: "BookName")
		soap.setIntegerValue(1, forKey: "chapter")

		// standard soap service (.asmx)
		do {
			let result = try soap.syncRequestURL("http://www.prioregroup.com/services/americanbible.asmx", 
						 soapAction: "http://www.prioregroup.com/GetVerses")
			print(result)
		}
		catch {
			print(error)
		}	
	}
}

SOAP Authentication(SOAP 身份验证) 的设置

soap.authorizationMethod = .AUTH_BASICAUTH; // basic auth
soap.username = "my-username";
soap.password = "my-password";

Social OAuth2.0 令牌的设置

// token authorization
soap.authorizationMethod = .AUTH_SOCIAL;
soap.apiKey = "1234567890"; // your apikey https://dev.twitter.com/
soap.socialName = ACAccountTypeIdentifierTwitter; // import Accounts

无需 SSL/HTTPS 的 Encryption/Decryption(加密/解密) 数据

soap.encryptionType = ._ENCRYPT_AES256; // or SOAP_ENCRYPT_3DES
soap.encryptionPassword = "my-password";

带有 Attributes(属性) 的参数

// book
var book = ["name": "Genesis"] as! NSMutableDictionary
var attr = ["order": "asc"]
// chapter
var child = soap.dictionary(forKey: "chapter", value: "1", attributes: attr)
book.addEntries(from: child!)
// book attributes
soap.setValue(book, forKey: "Book", attributes: ["rack": "2"])

它构建了一个如下所示的请求

<Book rack="2">
	<name>Genesis</name>
	<chapter order="asc">1</chapter>
</Book>

优化


首先,如果您注意到请求响应速度变慢,请尝试更改名为 actionNamespaceSlash 的属性的值。其次,当使用名为 requestWSDL 的方法时,会执行三个步骤

  1. 使用 http 请求检索 WSDL
  2. 处理以识别 soapAction
  3. 使用 http 请求调用方法 http 请求

这是未优化的,非常慢,您可以改用以下优化方法

  1. 直接从 WSDL 手动检索 SOAPAction(使用您喜欢的浏览器一次)。
  2. 使用名为 requestURL 的方法代替 requestWSDL,无需 WSDL 扩展名。

在您的应用中安装


Swift Package Manager

SOAPEngine 可作为 Swift 包使用。存储库 URL 对于通过 Xcode 在您的应用中添加包是有效的。

Cocoapods

阅读 “Getting Started”指南

Cocoapods 和 Swift

阅读 Integrating SOAPEngine with a Swift project

标准安装

阅读 “Standard Installation”指南

许可证

试用
仅限模拟器
单应用
单个 bundle-id
企业版
多 bundle-id
下载 购买 12,99€ 购买 77,47€