這個通用 SOAP 用戶端可讓您使用 iOS 應用程式、Mac OS X 應用程式和 Apple TV 應用程式存取 Web 服務。
使用此框架,您可以建立支援 SOAP 用戶端協定的 iPhone、iPad、Mac OS X 和 Apple TV 應用程式。該框架能夠使用 SOAP 標準協定在遠端 Web 服務上執行方法。
Swift 4 :該程式庫目前是用 Objective-C 編寫的,當您匯入 swift 函式庫時,您將收到如下建置The use of Swift 3 @objc inference in Swift 4 mode is deprecated
deprecated 。
對於靜默此警告,需要在目標的建置設定中將Swift 3 @objc Inference
設定為預設值。但這還不是全部;用於建立請求的類別必須使用@objcMembers
和NSObject
聲明,例如:
class MyClass { ... }
let param = MyClass ( )
// ...
// ...
let soap = SOAPEngine ( )
soap . setValue ( param , forKey : " myKey " )
// ...
// ...
MyClass 的聲明必須變成:
@ objcMembers class MyClass : NSObject { ... }
從新的 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 中新增一些按鍵,步驟如下:
info.plist
檔案。NSAppTransportSecurity
的鍵作為字典。NSAllowsArbitraryLoads
的子項作為 Boolean,並將其值設為 YES,如下圖所示。 參考連結:http://stackoverflow.com/a/32631185/4069848
與代表:
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 )
}
}
使用塊編程:
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! )
}
}
}
帶有通知:
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 )
}
}
同步請求:
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 驗證的設定:
soap . authorizationMethod = . AUTH_BASICAUTH; // basic auth
soap . username = " my-username " ;
soap . password = " my-password " ;
社交OAuth2.0令牌的設定:
// token authorization
soap . authorizationMethod = . AUTH_SOCIAL;
soap . apiKey = " 1234567890 " ; // your apikey https://dev.twitter.com/
soap . socialName = ACAccountTypeIdentifierTwitter; // import Accounts
無需 SSL/HTTPS 的加密/解密資料:
soap . encryptionType = . _ENCRYPT_AES256; // or SOAP_ENCRYPT_3DES
soap . encryptionPassword = " my-password " ;
帶有屬性的參數:
// 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
的方法時,將執行三個步驟:
這沒有優化,非常慢,您可以使用以下優化:
SOAPEngine 可作為 Swift 套件使用。儲存庫 URL 對於透過 Xcode 在應用程式中新增套件有效。
閱讀“入門”指南
閱讀將 SOAPEngine 與 Swift 專案集成
閱讀“標準安裝”指南
審判 只是模擬器 | 單一應用程式 單包 ID | 企業 多捆綁 ID |
---|---|---|
下載 | 購買 12,99€ | 購買 77,47€ |