user1787741
user1787741

Reputation: 99

Steps to use SharePoint Rest interfaces in iOS

I am developing an app for SharePoint online and wanted to use the SharePoint Rest interfaces in my ios app. Can Some one please tell me the steps to use SharePoint Rest interfaces in iOS

Upvotes: 2

Views: 2655

Answers (4)

Radu Simionescu
Radu Simionescu

Reputation: 4676

The self accepted answer lost me lots of hours of trials and errors. It omits some key aspects like the fact that you also need to grab the rtFa cookie. And what's up with client.username = @"username" and client.password = @"Password" provided in the users code. What is that? Note that the client does not know the username or password at any moment...

AAAnyway, below is a great article which will guide you in the right direction: http://www.codeproject.com/Articles/571996/Development-of-iPhone-client-application-for-Share

And this describes how to get the cookies without using a UIWebView http://allthatjs.com/2012/03/28/remote-authentication-in-sharepoint-online/

Upvotes: 1

mattiasle
mattiasle

Reputation: 26

When the user finish the sign in process towards a Office365 Sharepoint instance, the web view will be redirected in several steps. As one of the final steps before loading the actual Sharepoint web site, the web view will be asked to load "about:blank".

Detect when you web view starts loading "about:blank" and you know when the user finished the sign in process and can close the web view. Example code below.

// Load page in web view
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  NSLog(@"WebView should start loading %@", request.URL.absoluteString);

  // Detect that the user finished the sign in process
  if ([request.URL.absoluteString isEqualToString:@"about:blank"]) {

    // Do your stuff here

    return NO;
  }

  return YES;
}

The Sharepoint instance will also set the FedAuth cookie if the authentication was successful. The cookie must be included in future requests to the server.

You do not have to append the cookie manually, this will be taken care of by the URL loading system as long as the cookies has been accepted and stored in the NSHTTPCookieStorage and you are sending the request to the same server.

From Apple documentation

The URL loading system automatically sends any stored cookies appropriate for an NSURLRequest. unless the request specifies not to send cookies. Likewise, cookies returned in an NSURLResponse are accepted in accordance with the current cookie acceptance policy.

Upvotes: 0

user1787741
user1787741

Reputation: 99

I got it, below are the steps to be followed:

  1. Include RestKit in your ios app.

  2. Create a UIView in your home screen and load the login page.

    load http: //server name/Pages/default.aspx in the UIWebView

  3. In webViewDidFinished method find out the Fed Auth token and append it with the request URL

     - (void)webViewDidFinishLoad:(UIWebView *)webView {
       //Retrive HTTPOnly Cookie
       NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
       NSArray *cookiesArray = [storage cookies];
    
       //Search for Fed Auth cookie
       for (NSHTTPCookie *cookie in cookiesArray) {
    
        if ([[cookie name] isEqualToString:@"FedAuth"]) {
        /*** DO WHATEVER YOU WANT WITH THE COOKIE ***/
    
        NSLog(@"Found FedAuth");
    
    
        NSURL *url=[NSURL URLWithString:@"http://my server/_vti_bin/listdata.svc"];
        RKClient *client = [RKClient clientWithBaseURL:url];
        client.requestQueue.requestTimeout = 10;
        client.cachePolicy = RKRequestCachePolicyNone;
        client.authenticationType = RKRequestAuthenticationTypeHTTPBasic;
        client.username = @"username";
        client.password = @"Password";
    
        NSString *cookieVale=[cookie value];
        NSString *getResourcePath=[@"?" stringByAppendingFormat:@"%@",cookieVale];
    
        [client get:getResourcePath delegate:self];
    
    
        break;
    }
    }
    
    }
    
  4. And here you can find the response.

    - (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    
    
    id xmlParser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeXML];
    NSError *error = nil;
    id parsedResponse = [xmlParser objectFromString:[response bodyAsString] error:&error];
    RKLogInfo(@"Parsed response : %@, error:%@",parsedResponse,error);
    
    
    
    if ([response isSuccessful]) {
    NSLog(@"%d",[response isCreated]);
    // Response status was 200..299
    if ([response isCreated] && [response isJSON]) {
        // Looks like we have a 201 response of type application/json
        RKLogInfo(@"The JSON is %@", [response bodyAsJSON]);
    }
    } else if ([response isError]) {
    // Response status was either 400..499 or 500..599
    RKLogInfo(@"Ouch! We have an HTTP error. Status Code description: %@", [response localizedStatusCodeString]);
    }
    }
    

Upvotes: 1

sainiuc
sainiuc

Reputation: 1697

Send the FedAuth cookie with all your subsequent Requests.

Once authenticated, you can call the REST API, documentation here: http://msdn.microsoft.com/en-us/library/fp142385(v=office.15).aspx#bk_determining

Upvotes: 0

Related Questions