sharepoint services

I have got solution for authentication to share point web service I have use fedAuth Cookie and rtfa Cookie to pass credential

Fisrt I have load the login page in webview and to load login page of sharepoint

When user done with login i will store those Cookie with following method

func verifyCookies()
{

    var status = 0
    var cookieArray:NSArray = storage.cookies!

    for cookie in cookieArray
    {
        if cookie.name == "FedAuth"
        {
            fedAuthCookie = cookie as NSHTTPCookie
            status++
            continue;
        }
        else if cookie.name == "rtFa"
        {
            status++
            rtFaCookie = cookie as NSHTTPCookie
        }
    }

    if status == 2
    {
        self.webView.hidden = true
        self.cookieFound(rtFaCookie,fedAuthCookie: fedAuthCookie) // method mention below.
    }

}

fedAuthCookie and rtFaCookie are variable name which store that cookie

And after that I have use those cookie to pass credential to my request.

Below is my request code

func cookieFound(rtFaCookie:NSHTTPCookie,fedAuthCookie:NSHTTPCookie)
{
    var message:String = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>demo</listName><viewName></viewName><query></query><viewFields></viewFields><rowLimit></rowLimit><QueryOptions></QueryOptions><webID></webID></GetListItems></soap:Body></soap:Envelope>"

    var messageLength = String(countElements(message))
    var url = NSURL(string: "https://domain/_vti_bin/Lists.asmx")
    var therequest = NSMutableURLRequest(URL: url!)

    therequest.addValue(messageLength, forHTTPHeaderField: "Content-Length")
    therequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    therequest.addValue("http://schemas.microsoft.com/sharepoint/soap/GetListItems", forHTTPHeaderField: "SOAPAction")

    // Authentication is passed in request header

    var cookieArray = NSArray(objects: rtFaCookie,fedAuthCookie)
    var cookieHeaders = NSHTTPCookie.requestHeaderFieldsWithCookies(cookieArray)
    var requestHeaders = NSDictionary(dictionary: cookieHeaders)


    therequest.allHTTPHeaderFields = requestHeaders
    therequest.HTTPMethod = "POST"
    therequest.HTTPBody = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    var connection = NSURLConnection(request: therequest, delegate: self, startImmediately: true)
    connection?.start()


}

I have called verifyCookies() method from below webView methods

func webViewDidStartLoad(webView: UIWebView) {
    self.verifyCookies()
}
func webViewDidFinishLoad(webView: UIWebView) {
    self.verifyCookies()
}

Hope it will help someone

原文地址:https://www.cnblogs.com/lingzhao/p/4552061.html