2

I'm creating a simple web browser as an exercise.

-(IBAction)go:(id)sender {
NSString *query = [_urlField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@",query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
}

I would like it to know how I could add a UIActivityIndicator while loading my webView.

2nd question:

Is it possible to remove the http:// if the user enters it? I know you can do this in PHP like this:

if(strpos($URL, "http://") !== false)

How could I use it in Objective C?

Thanks you all for helping me.

Thiebout
  • 171
  • 3
  • 15

1 Answers1

2

For the first questions, check out this post.

And the second:

NSString* urlWithoutProtocol = [urlWithProtocol stringByReplacingOccurrencesOfString:@"http://" withString:@""];
Community
  • 1
  • 1
IluTov
  • 6,807
  • 6
  • 41
  • 103
  • Thanks @NSAddict. But what if I want to add various NSStrings to replace items.. I needed to remove my first line of code to get it working and get urlWithProtocol into urlField.text. I also edited NSString *query – Thiebout Nov 17 '12 at 18:37
  • What do you mean? Can you make an example? – IluTov Nov 17 '12 at 18:39
  • I used this code NSString *query = [_urlField.text stringByReplacingOccurrencesOfString:@"http://" withString:@""]; But now I want also to remove @"www." into @"" – Thiebout Nov 17 '12 at 18:40
  • Then just do [urlWithProtocol stringByReplacingOccurrencesOfString:@"www." withString:@""] again – IluTov Nov 17 '12 at 18:41
  • But what should I place before that line of code? I cant use NSString *query again.. – Thiebout Nov 17 '12 at 18:43
  • of course you can :) query = [query stringByReplacingOccurrencesOfString:@"www." withString:@""]; – IluTov Nov 17 '12 at 18:49
  • you must not define the query variable again – IluTov Nov 17 '12 at 18:49
  • "NSString *query" says to the compiler to make a variable which should hold a NSString object reference. If you have defined it once in a certain scope, you can never define it again. Then you can just use it's name to get or set it. – IluTov Nov 17 '12 at 18:53
  • Feel free to upvote answers if they helped you, users earn points like this ;) lol – IluTov Nov 17 '12 at 19:03
  • I tried to vote you up but I don't have enough reputation.. Perhaps if you vote me up ;-) – Thiebout Nov 17 '12 at 19:17
  • haha, it's not going to be enough. Voted you up though. thanks – IluTov Nov 17 '12 at 19:35
  • NSString *query = [urlWithProtocol stringByReplacingOccurrencesOfString:@"http://www." withString:@""]; query = [urlWithProtocol stringByReplacingOccurrencesOfString:@"http://" withString:@""]; – IluTov Nov 17 '12 at 19:37
  • Because there might be other parts of the url with "www." Like http://www.ilijatovilo.ch/?redirect=www.someotherwebsite.com – IluTov Nov 17 '12 at 19:38
  • @USer Begininning: A very good learning approach.As asking questions....reallyy very good..too for NSAddict up vote for such a nice conversation. – Arpit B Parekh Nov 17 '12 at 19:44