method to pass a dynamic string to a urlwithstringformat methos ios - ios

i have a class which connect with a web service, and i created this method :
+(listingObject *) getDetailsForId : (NSString *) id {
NSURL *url=[NSURL urlwithstringFormat : #"htt://mywebservic&id=%#",id];
}
it does not work plz some help

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://example.com/%#", id]]
...is a contrived example. It may be easier for readability to create your string on a separate line.

Related

NSURL set from variables

A pretty simple question i am sure, but i don't know how to go about doing such a thing. At current i $post variable 1 and variable 2 to the server and return some son data this is pretty slow but it worked, then it all went down hill when i started to have execution issues on the server side. Rented server so can't change php.ini file. So i have came up with this idea as a work around.
Would it be possible to direct to my download source, if the link itself contained variables collected from my label.text rather than writing each individual link out for each group (127 to be precise). For instance variable1 would equal "hell" and variable to would equal "red" therefore my NSURL would point to www.test.com/hell/red.php
Is this possible or is there some other way of doing this?
NSURL *url = [NSURL URLWithString:#"http://www.test.com/$variable1/$variable2.php"];
So now i know that this is possible, is some form or way, how can i resolve the following errors that i am receiving? For what i understand i simply can't have a / between the two variables and i can't have the .json file extension included on the end of the url.
If you require anymore code don't hesitate to ask.
Thank you very much in advance for any help!
The string you are creating the NSURL with is a normal NSString. To use variables in a NSString, you can use:
[NSString stringWithFormat:#"http://www.test.com/%#/%#.php", variable1, variable2];
%# is a placeholder for a string. variable1 and variable2 must be NSStrings
The line that creates could look like this:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.test.com/%#/%#.php", variable1, variable2]];
Or:
NSString * urlString = [NSString stringWithFormat:#"http://www.test.com/%#/%#.php", variable1, variable2];
NSURL * url = [NSURL URLWithString:urlString];
You can try this one:
NSString *urlString = [NSString stringWithFormat:#"http://www.test.com/%#/%#.php", var1, var2];
NSURL *url = [NSURL URLWithString:urlString];

NSURL URLWithString returns nil

I am stuck in a very obvious method of NSURL class URLWithString I am passing a string and while creating URL it returns nil, my string is perfect. When ever I uses same string in browser then it is working fine. I am using following method to convert NSString to NSURL:
NSURL *url = [NSURL URLWithString:urlString];
//urlString is my perfect string
I have also tried to encode my string first by using following
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// using this line my output string when I log url is "url%10%10%10%10%10%10..."
%10 becomes the suffix of my url with around 100+ repetition.
If any one has idea what %10 is or what can be done to overcome this problem.
here is my urlString:
Use below code to resolve your problem.
NSString *str = msgDetail[#"thumbnail"];
NSString* webStringURL = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:webStringURL];
yes what was wrong in my string were many unidentified characters due to copying from internet site, if anyone of the reader facing this issue can copy and paste your string as see the count. And confirm.
Thanks
Try below solution
NSURL *url = [NSURL URLWithString:[urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Check it..!

Can't set String to URLWithString

I'm having an issue with a action to a Facebook page.
The currentVriend is a NSObject Class.
I get the error: too many arguments to method call expected 1 have 2
-(void)goFB
{
NSURL *url = [NSURL URLWithString:#"fb://profile/%g",self.currentVriend.fbid];
[[UIApplication sharedApplication] openURL:url];
}
What am I'm doing wrong?
Maybe make another String?
Thanks
I believe you are missing the correct NSString method
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"fb://profile/%g",self.currentVriend.fbid]];

IOS - How do I add segments to NSURL?

Suppose you have a URL that looks like http://localhost:5867 and your wanted to append '/something' to the url. How do I append segments to this URL?
I am trying this:
//_baseURL is NSURL* and documentid is NSString*
NSURL* url = [_baseURL URLByAppendingPathComponent:documentid];
Oddly.. xcode(4.6.2) will not output the resulting URL to console.
NSLog(#"%#", [url absoluteString]);//nothing output
Thanks!
Edit
I am working in a workspace. The code that I am debugging is a static lib which is in the workspace. Could this be the culprit of all this weirdness??
Fixed
I think the reason I was having problems was because my workspace was not configured correctly and I was actually executing an older version of my static lib. This explains why I wasn't getting NSLog'ing and why the result were unexpected. I found this post which helped me understand the workspace and how to configure it for a static library. Thanks a lot for everyone's time!
There is a class message,
+ (id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL
So,
NSURL *base = [NSURL URLWithString: #"http://localhost:5867"];
NSURL *child = [NSURL URLWithString: #"something" relativeToURL:base];
NSURL *base = [NSURL URLWithString:#"http://localhost:5867"];
NSURL *url = [base URLByAppendingPathComponent:#"something"];
NSLog(#"%#", [url absoluteString]);
This works for me and prints http://localhost:5867/something, so check your variables (maybe one of them is nil or an empty string?).

NSURL URLWithString the url is always nil

My code is like this :
NSString *urlStr = [NSString stringWithFormat:#"https://xxx.xxx.xx.xx/Pages/Service/FMService.svc/FileAudit?user=%#&pass=%#&fileId=%d&isAudited=1&opinion=%#",appDelegate.user.userName,appDelegate.user.password,self.todo.fileId,self.opinion];
NSLog(#"URL=%#",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
And the url = (NSURL *) 0*00000000#"< nil >"
how can I work it out? thank you in advance!
For debugging purposes, I would build up urlStr piece by piece instead of combining five NSStrings all at once. Put in an NSLog(#"%#", urlStr) statement after each addition to urlStr. This will reveal which piece of the puzzle is causing the problem.

Resources