tags:

views:

24

answers:

2

i am a new iphone programmer

i am making a web based application for that a took one text box (to take web address from user) and a button which will be pressed by user to go to his web address .. Now when user type his url such as http://www.google.com then it works fine but problem is that when user type only google.com or www.google.com it doesn't work.

i know the reason...why this is happening...

for this i decide to add (http://) by programming here another problem was arised... if the user write the whole web address then again it will fail ...

here is my code for button click

-(IBAction)go { NSMutableString *str;

    str = [NSMutableString stringWithFormat:@"http://www.%@",name.text];    
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];

}

here name is textfield ans webview is an object of webView..... it works fine but i want some more as asked above...

please help....

A: 

You are trying to create a web browser in your application??

Anyways.. a complicated for loop which checks whether the first 7 characters of you string are 'http://' or first 3 are 'www.' etc etc should do.. but how many cases are you gonna try?? i mean someone might want to open a secured link with 'https' and your code might append another http:// before it.. so make sure you have thought of all the test case before you finalize this approach...

lukya
+1  A: 

Try this.....

-(IBAction)go {

NSMutableString *str =[[NSMutableString alloc]initWithString:name.text];

if(![str hasPrefix:@"http://www."])
{
    if([str hasPrefix:@"www."])
            [str insertString: @"http://" atIndex: 0];
    else
            [str insertString: @"http://www." atIndex: 0];

}
if(![str hasSuffix:@".com"])
{
    [str appendString:@".com"];
}
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[str release];

}

this will check "http://" , "www." and also ".com" and handle if user did not entered any one of them.... This will work surely TRY IT......

Ranjeet Sajwan
I have already done this but this is the more easy method you provide........ thanks....
Online