开发者

Opening NSURL using string variable - iPhone

I have a variable called storedURL whi开发者_如何学Cch 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


The URL is probably nil, since "www.xxxxxxx.com" is not a url. Try

 NSString *urlstring = [NSString stringWithFormat:@"http://%@/",storedURL];
 NSURL *url = [NSURL URLWithString:urlstring];
 NSLog(@"url = %@",url);
 [[UIApplication sharedApplication] openURL:url];

If this doesn't help, please share the output (stack trace) given in the console window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜