2016-08-17 1 views
0

https://somesite.com/search/name?name=%SEARCH_KEYWORD%のObjective-C - フォーマットGETリクエスト</p> <p>私の要求のURLが見えるようになっているGET要求のURLに、そのような%としてJOHNの%を文字列パラメータを渡すためのルールは何

は試してみてください#1 :私はこの

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://somesite.com/search/name?name=%%%@%%",SEARCH_KEYWORD]]; 

O/Pでした:ゼロ

は#2を試してください。

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://somesite.com/search/name?name=%JOE%"]]; 

** O/P:

https://somesite.com/search/name?name=JOE 

任意の提案ですか?

答えて

2

あなたはURLを構築するためにNSURLComponentsを使用することができます。

のObjective-C:

NSURLComponents *components = [NSURLComponents componentsWithString:@"https://google.com"]; 
components.query = @"s=%search keywords%" 

NSURL *url = components.URL; 

スウィフト(生産の!に注意、私は遊び場でテストするために使用):

let components = NSURLComponents(string: "https://google.com")! 
components = "s=%search keywords%" 

let url = components! 
print(url) // "https://google.com?s=%25search%20keywords%25" 

さらに複雑なクエリが必要な場合NSURLComponentにはqueryItemsというプロパティがあります。

+0

ありがとうございました。魅力的でした。 –

-3

間違いをしていますが、ただ1つの%@を使用してください。 例:

NSString *string = @"JOE"; 
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"https://somesite.com/search/name?name=%@",string]]; 
関連する問題