1

I am trying to replace my custom IP address with the existing URL IP address. How can replace the my IP address with the existing IP. Please find the code snippet below

 - (IBAction)btnSubmit:(UIButton *)sender {

        NSString *ipAdd = [_txtIPAddress text];
        NSLog(@"My entered IP Address is %@", ipAdd);

        NSLog(@"Current pointing server value is %@", [ConnManager activeEndpoint]);

        NSLog([App appDelegate].isConfigured  ? @"Yes" : @"No");

        NSArray *listItems = [[ConnManager activeEndpoint] componentsSeparatedByString:@"/"];

        ConnManager *conn = [[ConnManager alloc] init];

        NSMutableString *configuredUrl = nil;
        [configuredUrl setString:@""];

        for ( NSInteger i =0; i < listItems.count; i++) {
            if(i != 2) {
                NSMutableString * arrayElement = [(NSMutableString*)listItems objectAtIndex:i];
                NSMutableString *str = [NSMutableString stringWithFormat: @"%@/", arrayElement];
                [configuredUrl appendString:str];
            } else {
                NSMutableString *configstr = [NSMutableString stringWithFormat: @"%@", ipAdd];

                NSLog(@"ConfigString %@", configstr);


//How to replace the my ip address with the array index[2]
 configuredUrl = [configuredUrl stringByAppendingFormat:configstr];
            NSLog(@"Configured Url after apppending %@", configuredUrl);

Thanks in advance

2 Answers 2

1

Try this shorter version, it replaces everything after the line NSLog([App appDelegate].isConfigured ? @"Yes" : @"No");

  NSMutableArray *listItems = [[[ConnManager activeEndpoint] pathComponents] mutableCopy];
  listItems[2] = ipAdd;
  NSString *configuredUrl = [NSString pathWithComponents:listItems];
  NSLog(@"%@", configuredUrl);

or, if activeEndpoint is an URL, still easier

 NSURL *url = [NSURL URLWithString:[ConnManager activeEndpoint]];
 NSString *configuredURL = [[[NSURL alloc] initWithScheme:[url scheme] host:ipAdd path:[url path]] absoluteString];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. It works but I am getting URL as http:/xx.x.xxx.xx/xx/xx. How can I insert one more slash after http:
0

It is often easier to leave the parsing of URLs to a system class. If you can, I would suggest using NSURLComponents. For example:

NSURLComponents *URLComponents = 
     [NSURLComponents componentsWithString:[ConnManager activeEndpoint]];
URLComponents.host = [_txtIPAddress text];
NSString *configuredURL = URLComponents.string;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.