santa
santa

Reputation: 147

Encryption iOS Xcode

I have a problem with webservice which is private. I mean in order to access to this webservice i need username.. how can i reach inside of my webservice .Do you have any suggestion?

To have something visual -- http://www.test.com/event (This is an example)

-(void)startConnection
{
    NSString *urlString = GET_EVENT_URL_STRING;

    NSURL *url = [NSURL URLWithString:urlString];

    if (!url)
    {
        NSString *reason = [NSString stringWithFormat:@"Could not create URL from string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

        theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];

    // Set the HTTP method of the request to POST
    [theRequest setHTTPMethod:@"POST"];


    if (!theRequest)
    {
        NSString *reason = [NSString stringWithFormat:@"Could not create URL request from string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (!theConnection)
    {
        NSString *reason = [NSString stringWithFormat:@"URL connection failed for string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

    if (theConnection)
    {
        myData = [[NSMutableData alloc]init];
    }
}

When i clicked this link for example, i have one alert with username and password screen ..when i enter the information i can access.. This code for connect my webservice, how can i manage this?

Upvotes: 0

Views: 835

Answers (1)

picciano
picciano

Reputation: 22701

You need to add an authentication header to your theRequest object.

NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self username], [self password]];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]];
[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"];

Upvotes: 1

Related Questions