如何在nsurl iphone中包含变量

时间:2011-09-25 19:36:28

标签: iphone objective-c xcode url location

这就是我所拥有的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    responseData = [[NSMutableData data] retain];
    results = [NSMutableArray array];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=38.69747988499999,-121.3570182875&radius=9900&types=bar&sensor=false&key=fake_key"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    return YES;

}

并且使用url我希望在那里有一个变量用于该位置。基本上我的应用程序是获取位置,我想实现这个到网址,所以我可以找到周围的酒吧....我将如何做到这一点

我在想

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    responseData = [[NSMutableData data] retain];
    results = [NSMutableArray array];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=%@,%@&radius=9900&types=bar&sensor=false&key=fake_key", longitude,latitude]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    return YES;

}

但它没有用

请帮助

2 个答案:

答案 0 :(得分:3)

使用来自NSString的+ stringWithFormat:

[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%@,%@&radius=9900&types=bar&sensor=false&key=fake_key", longitude,latitude]]

答案 1 :(得分:0)

所以我看到很多人使用NSURL,我相信如果他们要查看ASIFormDataRequest,他们会发现它更安全,更容易使用。要使用ASIFormDataRequest,您必须从此处下载http://allseeing-i.com/ASIHTTPRequest/How-to-use 但是一旦下载它就会让生活变得更轻松。我指出这一点的原因是因为它强制所有内容符合协议,所以你会知道你是否因为%f或%@而遇到问题,因为它会强制你使用字符串。它使调试更容易。以下是如何使用ASIFormData请求的一些示例代码

NSURL *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json"];
ASIFormDataRequest *request_ror = [ASIFormDataRequest requestWithURL:url];
[request_ror setRequestMethod:@"POST"];
[request_ror setPostValue:[NSString stringWithFormat:@"%f", longitude] forKey:@"longitude"];
[request_ror setValidatesSecureCertificate:NO];
[request_ror setTimeOutSeconds:30];
[request_ror setDelegate:self];
[request_ror startAsynchronous];