2011-09-25 27 views
0

一個變量,這是我有什麼我如何包括在NSURL iphone

- (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; 

} 

但它沒有工作

請從NSString的幫助

回答

3

使用+ 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]]
+0

一些示例代碼; 任何機會,你會知道我爲什麼會嗎? –

+0

你有什麼錯誤? – Sean

+0

如何申報經緯度?如果它們被聲明爲NSNumber引用,那麼上面應該可以工作,但是如果它們是原始的浮點類型,則可能需要在格式字符串中使用%f(對於float)而不是%@ –

0

所以我看到很多使用NSURL的人,我相信如果他們要查看一個ASIFormDataRequest,他們會發現它更安全,更容易使用。爲了使用ASIFormDataRequest,你必須從這裏下載它http://allseeing-i.com/ASIHTTPRequest/How-to-use 然而,一旦下載,它使生活變得更容易。我指出這一點的原因是因爲它強制一切符合協議,所以你會知道是否因%f或%@而存在問題,因爲它強制你使用一個字符串。它使調試更容易。這裏是當我使用,我現在在 [NSURLConnection的頁頭] initWithRequest:要求委託:自我]收到錯誤如何使用ASIFormData請求

​​