Iphone 如何通过身份验证从服务器获取JSON数据?

Iphone 如何通过身份验证从服务器获取JSON数据?,iphone,ios,json,Iphone,Ios,Json,我在服务器上有JSON,假设我的URL是 https://abc.companyname.com/posts/list/10 当我在浏览中粘贴该URL时,它会询问我用户名和密码。当我输入这些凭证时,它会以JSON格式返回一些数据,如下所示 [{"id":"5","picture":"myimage.jpg","name":"Usman Zafar","type":"textonly","message":"Woo this is demo and it looks good thanks ad

我在服务器上有JSON,假设我的URL是

https://abc.companyname.com/posts/list/10
当我在浏览中粘贴该URL时,它会询问我用户名密码。当我输入这些凭证时,它会以JSON格式返回一些数据,如下所示

[{"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"}]}]
我的问题是

如何在iPhone编程中使用身份验证(用户名+密码)创建NSURLConnection?

如果需要关于问题的任何其他数据,请通过评论让我知道


谢谢

这是更多的应用程序设计。首先,通过向用户显示一些UI屏幕来捕获用户名和密码,然后您需要将post正文中的数据发送到服务器。您可以将普通键值对转换为NSData

请看下面的代码

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

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];        
}
使用以下代码:

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

NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];

可能的重复我看到了,它使用NSMutableString,因为我没有这样的对象。NSMutableString是一个可变字符串,也就是说,它的内容可以编辑。您可以从字符串创建NSMutableString。Ex NSMutableString*newString=[[NSMutableString alloc]initWithString:@“yourString”];MGTwitterEngine也用于该问题,我有简单的NSURLConnection?请,我知道什么是可变的。如果你真的知道答案,写下来帮助我,否则我已经在谷歌上搜索过了。有些人在post中将NSData发送到服务器,但我并没有数据,有些人使用任何其他东西,所以我发布了这个问题