Swift + Alamofire - Auth并获取XML文件

时间:2016-05-04 11:52:41

标签: swift alamofire

我的网站受到用户名和密码的保护。 我需要获取XML文件,然后解析它。我尝试了很多解决方案,但没有一个能为我工作。我收到了这段代码。

  

错误:错误域= NSCocoaErrorDomain代码= 3840“操作   无法完成。 (可可错误3840.)“(遇到未知标签   第1行上的tabless)UserInfo = 0x1aadf300 {NSDebugDescription =遇到   第1行的未知标签tabless,kCFPropertyListOldStyleParsingError =   操作无法完成。 (可可错误3840。)}

这是我的代码:

 func getTimeStampOfUpdatedTables(completionHandler: (Bool) -> ()) -> (){

    let user = "abc"
    let password = "abc"

    let plainString = "\(user):\(password)" as NSString
    let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
    let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

    Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Basic " + base64String!]

    Alamofire.request(.GET, "http://example.com/info.php", encoding: ParameterEncoding.URL).responsePropertyList { response in
        print(response.result)
        if let error = response.result.error {
            print("Error: \(error)")

            // parsing the data to an array
        } else if let array = response.result.value as? [[String: String]] {

            if array.isEmpty {
                print("No data")

            } else {
                //Do whatever you want to do with the array here
                print("data here!")
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

出现该错误是因为您的API响应是XML而不是属性列表格式。 你从Alamofire请求财产清单,它无法找到pList格式,所以它给出了错误。我用PList测试了代码并且工作正常

这是属性列表格式

$ docker-compose up --build --remove-orphans
  

我已经测试了整个事情

    <?xml version="1.0" encoding="UTF-8"?>

<plist version="1.0">
    <dict>
        <key>Tabless</key>
        <dict>
            <key>Tables</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>
            <key>Tables - 2</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>
            <key>Tables - 3</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>
            <key>Tables - 4</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>
            <key>Tables - 5</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>
            <key>Tables - 6</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>
            <key>Tables - 7</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>
            <key>Tables - 8</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>
            <key>Tables - 9</key>
            <dict>
                <key>test</key>
                <string>test</string>

            </dict>

对于解析XML数据,您可以考虑SWXMLHash。 Alamofire请求可能如下:

     let user = "abc"
    let password = "abc"

    let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
    let base64Credentials = credentialData.base64EncodedStringWithOptions([])

    let headers = ["Authorization": "Basic \(base64Credentials)"]
    Alamofire.request(.GET, "http://192.168.0.100/r.php", headers: headers)
        .responsePropertyList { response in

        print(response.result)
        if let error = response.result.error {
            print("Error: \(error)")

            // parsing the data to an array
        } else if let array = response.result.value as? [[String: String]] {

            if array.isEmpty {
                print("No data")

            } else {
                //Do whatever you want to do with the array here
                print("data here!")
            }
        }
    }

答案 1 :(得分:0)

尝试bello代码: -

func getTimeStampOfUpdatedTables(completionHandler: (Bool) -> ()) -> (){

    let username = "abc"
    let password = "abc"

            let credentialData = "\(username):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
        let base64Credentials = credentialData.base64EncodedStringWithOptions([])
        let headers = ["Authorization": "Basic \(base64Credentials)"]

        let manager = Alamofire.Manager.sharedInstance

        manager.session.configuration.timeoutIntervalForRequest = 300

        manager.request(.GET, url,parameters: dicsParams, headers: headers).responsePropertyList { response in
        print(response.result)
        if let error = response.result.error {
            print("Error: \(error)")

            // parsing the data to an array
        } else if let array = response.result.value as? [[String: String]] {

            if array.isEmpty {
                print("No data")

            } else {
                //Do whatever you want to do with the array here
                print("data here!")
            }
        }
    }
}