问题
I want to make a request to diffrent servers I only want to fill in the url or ip address in a textfield. I build different solutions but they all faild. I think with this one i'm close.
This is what I build so far:
NSString *part0 = @"http://";
NSString *part1 = [NSString stringWithFormat:@"%@" , _serveradres.text ];
NSString *part2 = @"/API/";
NSString *compURL = [NSString stringWithFormat:@"%@" , part0 , part1 , part2 ];
NSURL *url = [NSURL URLWithString:compURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
Only the address is diffrent, the api's are all ways on the same place.
Later on i want to make a switch that can choose http or https
回答1:
You should change this:
NSString *compURL = [NSString stringWithFormat:@"%@" , part0 , part1 , part2 ];
To this:
NSString *compURL = [NSString stringWithFormat:@"%@%@%@" , part0 , part1 , part2 ];
Because now your compURL
would be just http://
Hope it helps
回答2:
Do like this
NSString *compURL = [NSString stringWithFormat:@"http://%@/API/", serverAddr];
回答3:
No need for all those instantiated NSString
's:
NSString *compURLString = [NSString stringWithFormat:@"http://%@/API/", _serveradres.text]; // Probably ought to be self.serveradres.text instead.
NSURL *url = [NSURL URLWithString:compURLString];
*Note - I changed the name for your pointer to compURL to compURLString. The habit of naming your variables after what they do AND what they are will save you headache later.
来源:https://stackoverflow.com/questions/16437346/building-up-a-url-in-objective-c