ios - Add http:// to NSURL if it's not there -
i using web view in app, getting url text field. works if string starts "http://". trying modify code can handle situations users don't enter "http://" or "https://"
how check if url doesn't have "http://" in ? how modify url add "http://" in ?
nsstring *urlstring = textfield.text; nsurl *url = [nsurl urlwithstring:urlstring]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; [self.webview loadrequest:request];
nsstring *urlstring = @"google.com"; nsurl *webpageurl; if ([urlstring hasprefix:@"http://"] || [urlstring hasprefix:@"https://"]) { webpageurl = [nsurl urlwithstring:urlstring]; } else { webpageurl = [nsurl urlwithstring:[nsstring stringwithformat:@"http://%@", urlstring]]; } nsurlrequest *urlrequest = [nsurlrequest requestwithurl:webpageurl]; [self.mywebview loadrequest:urlrequest];
Comments
Post a Comment