Thursday, August 20, 2015

Objective c SharePoint login service

If you are trying to connect to SharePoint from an iOS application using Objective c, this article is for you. Unfortunately there is no enough information on the web regarding SharePoint connection from an iOS application using Objective c. Where as you can find well enough and better tutorials for an .Net application because, both are from same giant organisation like Microsoft. 

So it's easy for an .Net developer to quick start integrating SharePoint CMS in their .Net application. But, for an iOS developer it's a night mare. All we have is SharePoint SOAP services to communicate with the SharePoint.

Though, we are having REST services to connect to SharePoint, I am not supposed to talk about those because I don't have any experience on that. Here, I am going to give a quick start to connect to SharePoint. I mean first in place to get authenticated to SharePoint.

In SharePoint you can set different authentication mechanisms in the sever settings. Here I am going to show you a sample code to get connected to a SharePoint which is having BASIC authentication mechanism.

Let's talk about BASIC authentication first. If the SharePoint that you are trying to connect to is having a BASIC authentication mechanism in the server settings, You have to encode the user credentials while passing it to the server through a service for authentication.



                         




Here is the Objective c sample code to convert user credentials for BASIC authentication. 


    NSString *authStr = [NSString stringWithFormat:@"%@:%@", username, password];
    NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
    NSString *authValue = [authData base64EncodedStringWithOptions:0];

What I am doing in the above code is, I am taking username and password to a string with the separator as colon(:). Then I am doing the ASCII encoding of the string to convert it to
NSData. Finally I am doing base64 encoding and generating a BASIC authentication string of user credentials.

Here is the code to get user authentication to SharePoint. 


NSError *error = nil;
NSURLResponse *response;
            
NSURL *authenticationlURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] 
                                                             objectForKey:@"SP_AUTHENTICATION_URL"]];            
            
            
NSString *authStr = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [authData base64EncodedStringWithOptions:0];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:authenticationlURL];
[request setValue:[NSString stringWithFormat:@"Basic %@",authValue] 
                                                                  forHTTPHeaderField:@"Authorization"];
[request setTimeoutInterval:60];
            
NSData *responseData = [NSURLConnection sendSynchronousRequest:request 
                                                  returningResponse:&response error:&error];

I am using normal NURLConnection class sendSynchronousRequest method only. I am giving the url as SharePoint base URL. We need to pass the 'Autherization' header with the value of the BASIC authentication string that we have generated.

That's all in coding. All you have to check is whether the status code of the response is coming as 200 or not to know whether the authentication is success or not. The response gives you some other values also. But at this point we don't need to worry about those values. If the status code is 200, it's like authentication is successful.

Hope this article is helpful to newbies to SharePoint.

Feel free to comment in case of any queries or comments.


No comments:

Post a Comment