问题
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?
回答1:
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]];
回答2:
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.
来源:https://stackoverflow.com/questions/10506237/app-crashes-up-when-opening-url-in-safari