如何在iOS中同步Microsoft Exchange帐户

时间:2018-10-05 06:17:05

标签: ios swift microsoft-exchange

如何在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之后。 有没有更好的解决方案,请指导。 谢谢。

0 个答案:

没有答案