1

I'm trying to open url in safari with this code:

- (IBAction)webButton:(id)sender {

    NSString *url = @"www.google.com";

    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];


}

But every time app crashes up.

Has someone been in similar situation?

Here is ss off crash: http://dl.dropbox.com/u/77033905/urlInSafariCrashesUp.png

UPDATE:

NSString *recipients = @"mailto:first@example.com?subject=Hello from Croatia!";
    NSString *body = @"&body=It is sunny in Croatia!";

    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

This is for opening mail but same over sharedApplication. It crashes up to.

UPDATE 2: Console log: argv char ** 0xbffff520 *argv char * 0xbffff658 **argv char '/' argc int 1

UPDATE 3: It calls IBAction but crashes up. When I try this code in root view it works. I addedd and connected in IB button and everything is ok with that.

Is there problem with calling UIApplication sharedApplication in subview? Should I call on different way?

UPDATE 4:

I figure it out that problem is even when i call empty IBAction in subview, so problem obviously is not in UIApplication but in calling IBAction in subview.

- (IBAction)webButton:(id)sender {

  // empty

}

UPDATE 5: Solution: How to call IBAction in subview?

Community
  • 1
  • 1
iWizard
  • 6,816
  • 19
  • 67
  • 103

2 Answers2

3

You are not providing a valid URL, an URL is always of the form scheme:<host part>.

// This is correct and will work:
[[UIApplication sharedApplication] openUrl:[NSURL URLWithString:@"http://www.google.com"]]

// Updated with body and subject:
NSMutableString* url = [NSMutableString stringWithString:@"mailto:"];
[url appendString:@"first@example.com"];
[url appendFormat:@"?subject=%@", [@"Hello from Croatia" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[url appendFormat:@"&body=%@", [@"This is a body" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
Sveinung Kval Bakken
  • 3,715
  • 1
  • 24
  • 30
  • 1
    I think you have something else going wrong, I use this same code all the time and theres nothing wrong with it. – Mick MacCallum May 08 '12 at 21:03
  • It crashes up again with you code to. I'm calling above code in my subview, maybe is this the problem. Because when I try to call this code in root view it work's. – iWizard May 09 '12 at 08:00
  • @CroiOS You need to make it more clear what you are trying to accomplish, and exactly what is going wrong. Your updates keep changing your question. – Mick MacCallum May 09 '12 at 11:10
  • Yes, my updates changing question because i tought that problem was in UIApplication but it's not. And on that i open new question because it's not same like this. http://stackoverflow.com/questions/10515259/how-to-call-ibaction-in-subview – iWizard May 09 '12 at 11:34
1

Does it crash if you do something like

NSString *url = @"http://www.google.com";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

I believe you need the "http://" in there.

Joe
  • 2,987
  • 15
  • 24