使用字符串变量打开 NSURL - iPhone
我有一个名为storedURL 的变量,其格式为www.xxxxx.com
。当用户单击按钮时,我尝试使用下面的代码在 Safari 中打开此 URL。
-(IBAction)launchWeb {
NSString *url = [NSString stringWithFormat: @"%@",
storedURL];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
但是,单击按钮时应用程序会崩溃。
任何人都可以帮助纠正我的代码吗?
编辑
mvds 的代码崩溃如下:
2011-04-02 14:01:41.542 London[16791:207] +[NSURL urlWithString:]: 无法识别的选择器发送到类 0x103dcbc 2011-04-02 14:01:41.545 London[16791:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+[NSURL urlWithString:]:无法识别的选择器发送到类 0x103dcbc” *第一次抛出时的调用堆栈: ( 0 CoreFoundation 0x00fd95a9 异常预处理 + 185 1 libobjc.A.dylib 0x0112d313 objc_异常_抛出 + 44 2 CoreFoundation 0x00fdb17b +[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3 CoreFoundation 0x00f4a966 __转发 + 966 4 核心基础 0x00f4a522 _CF_forwarding_prep_0 + 50 5 伦敦 0x00006bce -[DetailViewController launchWeb] + 126 6 UIKit 0x0022b4fd -[UIApplication sendAction:to:from:forEvent:] + 119 7 UIKit 0x002bb799 -[UIControl sendAction:to:forEvent:] + 67 8 UIKit 0x002bdc2b -[UIControl(内部)_sendActionsForEvents:withEvent:] + 527 9 UIKit 0x002bc7d8 -[UIControl TouchsEnded:withEvent:] + 458 10 UIKit 0x004be4de _UIGestureRecognizerSortAndSendDelayedTouches + 3609 11 UIKit 0x004bec53 _UIGestureRecognizerUpdateObserver + 927 12 CoreFoundation 0x00fba89b CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 27 13 核心基础 0x00f4f6e7 __CFRunLoopDoObservers + 295 14 核心基础 0x00f181d7 __CFRunLoopRun + 1575 15 核心基础 0x00f17840 CFRunLoopRunSpecific + 208 16 核心基础 0x00f17761 CFRunLoopRunInMode + 97 17 图形服务 0x0151a1c4 GSEventRunModal + 217 18 图形服务 0x0151a289 GSEventRun + 115 19 UIKit 0x00239c93 UIApplicationMain + 1160 20 伦敦 0x00001ee9 主 + 121 21 伦敦 0x00001e65 开始 + 53
I have a variable called storedURL which is in the format www.xxxxx.com
. I am trying to use the below code to open this URL in Safari when a user clicks a button.
-(IBAction)launchWeb {
NSString *url = [NSString stringWithFormat: @"%@",
storedURL];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
However the app crashes when the button is clicked.
Can anyone help correct my code ?
EDIT
mvds' code crashes with following :
2011-04-02 14:01:41.542 London[16791:207] +[NSURL urlWithString:]: unrecognized selector sent to class 0x103dcbc
2011-04-02 14:01:41.545 London[16791:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSURL urlWithString:]: unrecognized selector sent to class 0x103dcbc'
* Call stack at first throw:
(
0 CoreFoundation 0x00fd95a9 exceptionPreprocess + 185
1 libobjc.A.dylib 0x0112d313 objc_exception_throw + 44
2 CoreFoundation 0x00fdb17b +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f4a966 __forwarding + 966
4 CoreFoundation 0x00f4a522 _CF_forwarding_prep_0 + 50
5 London 0x00006bce -[DetailViewController launchWeb] + 126
6 UIKit 0x0022b4fd -[UIApplication sendAction:to:from:forEvent:] + 119
7 UIKit 0x002bb799 -[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x002bdc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
9 UIKit 0x002bc7d8 -[UIControl touchesEnded:withEvent:] + 458
10 UIKit 0x004be4de _UIGestureRecognizerSortAndSendDelayedTouches + 3609
11 UIKit 0x004bec53 _UIGestureRecognizerUpdateObserver + 927
12 CoreFoundation 0x00fba89b CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 27
13 CoreFoundation 0x00f4f6e7 __CFRunLoopDoObservers + 295
14 CoreFoundation 0x00f181d7 __CFRunLoopRun + 1575
15 CoreFoundation 0x00f17840 CFRunLoopRunSpecific + 208
16 CoreFoundation 0x00f17761 CFRunLoopRunInMode + 97
17 GraphicsServices 0x0151a1c4 GSEventRunModal + 217
18 GraphicsServices 0x0151a289 GSEventRun + 115
19 UIKit 0x00239c93 UIApplicationMain + 1160
20 London 0x00001ee9 main + 121
21 London 0x00001e65 start + 53
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该 URL 可能是
nil
,因为“www.xxxxxxx.com”不是一个 URL。尝试如果这没有帮助,请分享控制台窗口中给出的输出(堆栈跟踪)。
The URL is probably
nil
, since "www.xxxxxxx.com" is not a url. TryIf this doesn't help, please share the output (stack trace) given in the console window.