1

如何使用 iOS 应用程序中的 Exchange Web 服务同步 Microsoft Exchange 帐户。它在这里讨论:我试过这个 - http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/OUC-B304#fbid=

到目前为止尝试过 -

 func connectToServer() -> Bool {
        let basicURL: String = "http://\(self.servername)/autodiscover/autodiscover.xml"

        let poxOLKAutoDRequest = "<?xml version=\"1.0\"encoding=\"utf-8\"?> <Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook.requestschema/2006\" <Request> <EmailAddress>\(usernmTF.text!)</EmailAddress> <AcceptableResponseSchema>http://schemas.microsoft.com/exhange/autodiscover/outlook/responseschema/2005a</AcceptableResponseSchema> </Request> </Autodiscover>"

        let url: URL = URL.init(string: basicURL)!

        //AutoD expects an HTTP POST with request body
        let request: ASIHTTPRequest = ASIHTTPRequest.init(url: url)
        request.username = self.usernmTF.text!
        request.password = self.passwdTF.text!
        request.requestHeaders = ["Content-Type": "text/xml; charset=utf-8"]
        request.timeOutSeconds = 90
        request.shouldUseRFC2616RedirectBehaviour = true

        request.appendPost(poxOLKAutoDRequest.data(using: .utf8)!)

        request.startSynchronous()

        let error = request.error
        if (!(error != nil)) {
            let response = request.responseData()
//            let doc = XMLParser.init(data: response!)

            var doc = GDataXMLDocument()
            do {
                doc = try GDataXMLDocument.init(data: response!)

            } catch {
                print(error)
            }

            if doc != nil {
                let ns: NSDictionary = ["rs": "http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006", "ol": "http://schemas.microsoft.com/exchange/autodiscover//outlookresponseschema/2006a"]

                let userData = NSArray()
                let protocolData = NSArray()


                do {
                    let Data = try  doc.nodes(forXPath: "//rs:Autodiscover/ol:Response/ol:User/ol:AutoDiscoverSMTPAddress", namespaces: ns as! [AnyHashable : Any])
                    _ = self.userData(data: Data)

                } catch {
                    print(error)
                }
                if error == nil {
                    let userEmail: GDataXMLElement = userData.object(at: 0) as! GDataXMLElement
                    print(userEmail.stringValue())
                }

                do {
                    let Data = try  doc.nodes(forXPath: "//rs:Autodiscover/ol:Response/ol:Account/ol:Protocol/[ol:Type=\"EXPR\"]", namespaces: ns as! [AnyHashable : Any])
                    self.protocolData(pr_Data: Data)

                } catch {
                    print(error)
                }
                if error == nil {
                    for proto in protocolData{
                        let aProto = proto as! GDataXMLElement
                        let asURLs = aProto.elements(forName: "ASUrl")! as NSArray
                        if asURLs.count > 0 {
                            let theURL: GDataXMLElement = asURLs.object(at: 0) as! GDataXMLElement
                            self.setServerUrl(url: URL.init(string: theURL.stringValue())!)
                        }
                    }
                }

            }
            return true
        }
        return false
    }

运行此错误404后。有没有更好的解决方案来做到这一点,请指导。谢谢。

4

0 回答 0