App crashes up when opening url in safari

℡╲_俬逩灬. 提交于 2020-01-05 12:12:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!