iWizard
iWizard

Reputation: 7104

App crashes up when opening url in safari

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:[email protected]?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?

Upvotes: 1

Views: 1974

Answers (2)

Sveinung Kval Bakken
Sveinung Kval Bakken

Reputation: 3824

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:@"[email protected]"];
[url appendFormat:@"?subject=%@", [@"Hello from Croatia" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[url appendFormat:@"&body=%@", [@"This is a body" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

Upvotes: 3

Joe
Joe

Reputation: 2987

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.

Upvotes: 1

Related Questions