How to Sync Microsoft Exchange account in ios

75 views Asked by At

How to sync Microsoft Exchange Account, using Exchange Web Services in iOS app. Its discussed here: I tried this - http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/OUC-B304#fbid=

Tried this so far -

 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
    }

After run this getting erring 404. Is there any better solution to do this, Please guide. Thanks.

0

There are 0 answers