在 safari 中打开 url 时应用程序崩溃 [英] App crashes up when opening url in safari

查看:77
本文介绍了在 safari 中打开 url 时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码在 safari 中打开 url:

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]];


}

但每次应用崩溃时.

有人遇到过类似的情况吗?

Has someone been in similar situation?

这里是 ss off 崩溃:http://dl.dropbox.com/u/77033905/urlInSafariCrashesUp.png

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

更新:

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]];

这用于打开邮件,但与 sharedApplication 相同.它崩溃了.

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

更新 2:控制台日志:argv 字符 ** 0xbffff520*argv 字符 * 0xbffff658**argv 字符 '/'argc int 1

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

更新 3:它调用 IBAction 但崩溃了.当我在根视图中尝试此代码时,它可以工作.我添加并连接了 IB 按钮,一切正常.

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.

在子视图中调用 UIApplication sharedApplication 有问题吗?我应该换个方式打电话吗?

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

更新 4:

我发现问题是即使我在子视图中调用空的 IBAction,所以问题显然不在 UIApplication 中,而是在子视图中调用 IBAction.

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

}

更新 5:解决方案:如何在子视图中调用IBAction?

推荐答案

您没有提供有效的 URL,URL 始终采用 scheme: 形式.

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]];

这篇关于在 safari 中打开 url 时应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆