如何在iphone应用程序中的url中传递字符串值

时间:2011-08-17 04:46:25

标签: iphone objective-c

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=catId&counties-selected=Vest-Agder,Aust-Agder"]];

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
NSArray *results = [parser objectWithString:json_string error:nil];

我有这个网址我希望class-selected = 350应该从Variable获取值如何做到这一点 如果我给var而不是350,我正在做但没有得到结果。

3 个答案:

答案 0 :(得分:0)

使用NSString stringWithFormat格式化带有ivar的URL:

NSString *catId = @"350";
NSString *url = [NSString stringWithFormat:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=%@&counties-selected=Vest-Agder,Aust-Agder", catId];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

答案 1 :(得分:0)

你可以试试这个:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.businessfactors.de/bfcms/images/stories/videos/%@.mp4",strtext]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

答案 2 :(得分:0)

用于发送使用stringByURLEncode,stringByURLDecode

 - (NSString *)stringByURLDecode {

        NSMutableString *tempStr = [NSMutableString stringWithString:self];
        [tempStr replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])];

        return [[NSString stringWithFormat:@"%@",tempStr] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    }

    - (NSString *)stringByURLEncode {

        NSMutableString *tempStr = [NSMutableString stringWithString:self];
        [tempStr replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])];


        return [[NSString stringWithFormat:@"%@",tempStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    }