0

I have JSON on server, suppose my URL is

https://abc.companyname.com/posts/list/10

When I paste that URL in browse, it asks me username and password. As I enter these credentials, it returns me some data in JSON Format like below.

[{"id":"5","picture":"myimage.jpg","name":"Usman Zafar","type":"textonly","message":"Woo this is demo and it looks good thanks adeco","image":null,"video_id":null,"datecreated":"2013-10-17 10:14:02","totalLikes":0,"totalComments":0,"commentsData":null},


{"id":"6","picture":"default.jpg","name":"Usman Zafar","type":"textonly","message":"Hello this is the demo of another post but no image","image":null,"video_id":null,"datecreated":"2013-10-17 10:31:04","totalLikes":0,"totalComments":0,"commentsData":null},


{"id":"7","picture":"default.jpg","name":"Usman Zafar","type":"textonly","message":"Hello this is the demo of another post but no image","image":null,"video_id":null,"datecreated":"2013-10-17 10:31:24","totalLikes":0,"totalComments":0,"commentsData":null},


{"id":"11","picture":"myimage_9.jpg","message":"Regukar Text comments no.772"}]}]

My Question is

How to create NSURLConnection with authentication (username+password) in iPhone Programming?

If any other data is required regarding question, let me know via comments.

Thanks

Duaan
  • 609
  • 1
  • 13
  • 29
  • Possible duplicate http://stackoverflow.com/questions/1973325/nsurlconnection-and-basic-http-authentication – Priyatham51 Oct 22 '13 at 02:18
  • I saw that, its using NSMutableString, where as I have no such object. – Duaan Oct 22 '13 at 02:20
  • NSMutableString is a mutable string—that is, a string whose contents can be edited. You can create NSMutableString from your string. Ex NSMutableString *newString = [[NSMutableString alloc] initWithString:@"yourString"]; – Priyatham51 Oct 22 '13 at 02:24
  • MGTwitterEngine is used also in that question, i have simple NSURLConnection? – Duaan Oct 22 '13 at 02:24
  • Please, I know what is Mutable. If you really know the answer, write it and help me, else I have already googled. Some send NSData to server in post, but i have no data, some use any other thing, so I post this question – Duaan Oct 22 '13 at 02:26
  • Either post it as an answer if you feel it could be useful, or remove your question if it is useless. – CaptJak Oct 22 '13 at 03:05

2 Answers2

0

This is more of application design. First, capture user name and password by showing some UI screen to user and then you need to send data in post body to server. You can convert plain key-value pair into NSData.

Look at the below piece of code to start with.

===============================================================================================

self.URLRequest = [NSMutableURLRequest requestWithURL:self.URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:999];
[self.URLRequest setHTTPMethod:[self methodStringForMethod:self.requestMethod]];
    for (NSString *aHeaderKey in self.requestHeader) {
        [self.URLRequest setValue:[self.requestHeader valueForKey:aHeaderKey] forHTTPHeaderField:aHeaderKey];
    }

    if (self.requestHeader) {
        [self.URLRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    }

    if (self.body) {
        [self.URLRequest setHTTPBody:[self keyValuePostDataFromDictionary:self.body]];
    }

    if (self.connection) {
        [self.connection cancel];
        self.connection = nil;
    }
self.connection = [[NSURLConnection alloc] initWithRequest:self.URLRequest delegate:self];
===============================================================================================

- (NSData *)keyValuePostDataFromDictionary:(NSDictionary *)iDictionary {
    return [[NSString runnerHttpPostBodyStringWithQueryParameters:iDictionary] dataUsingEncoding:NSUTF8StringEncoding];
}


- (NSString *)stringByAppendingRunnerQueryParameters:(NSDictionary *)iParameters appendQuestionMark:(BOOL)iAppendQuestionMark {
    BOOL aAppendAmpersand = YES;
    NSMutableString *aWorking = [NSMutableString stringWithString:self];

    if (iAppendQuestionMark) {
        NSRange aQueryBeginning = [self rangeOfString:@"?"];
        if (aQueryBeginning.location == NSNotFound) {
            [aWorking appendString:@"?"];
            aAppendAmpersand = NO;
        }
    } else {
        aAppendAmpersand = NO;  
    }

    for (id aKey in iParameters) {
        id aValue = [iParameters valueForKey:aKey];
        NSString *aKeyStr = [self convertRunnerObjectToURLEncodedValue:aKey];

        if (aAppendAmpersand) {
            [aWorking appendString:@"&"];
        } else {
            aAppendAmpersand = YES;
        }

        if ([aValue isKindOfClass:[NSArray class]]) {
            NSArray *aSubParamaters = (NSArray *)aValue;
            BOOL aFirstTime = YES;
            for (id aSubValue in aSubParamaters) {
                NSString *aValueString = [self convertRunnerObjectToURLEncodedValue:aSubValue];

                if (!aFirstTime) {
                    [aWorking appendString:@"&"];                   
                }
                [aWorking appendString:aKeyStr];
                [aWorking appendString:@"="];
                [aWorking appendString:aValueString];

                aFirstTime = NO;
            }

        } else {
            NSString *aValueString = [self convertRunnerObjectToURLEncodedValue:aValue];
            [aWorking appendString:aKeyStr];
            [aWorking appendString:@"="];
            [aWorking appendString:aValueString];           
        }

    }

    return [NSString stringWithString:aWorking];        
}
Abhinav
  • 37,684
  • 43
  • 191
  • 309
0

Use below code:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38