如何在iPhone上点击按钮打开URL

38

我有一个URL存储在NSString变量urlone中,它是从XML获取的。但当我运行应用程序时,会出现“excess bad error”的错误。

  // urlone contains  http://www.forasinvest.com/v2/index.php

  NSString*myurl=urlone;
  NSURL *url = [NSURL URLWithString:myurl];
  [[UIApplication sharedApplication] openURL:url];

假设 urlone 是有效的,那应该没有问题。你确定 urlone 没问题吗? - Mike K
在跟踪时,它说超出范围,而我已经在头文件类中声明了它。 - jogi alamni
NSString *myurl = urlone; NSLog(urlone); NSURL *url = [NSURL URLWithString:myurl]; [[UIApplication sharedApplication] openURL:url]; - jogi alamni
在通过 NSLog 进行跟踪时,它显示的是 URL。 - jogi alamni
3个回答

77

我认为它应该能够很好地工作,好的,只需复制以下内容以进行简单测试

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.forasinvest.com/v2/index.php"]];

2
我猜你在urlone的初始化中忘记加上@了。 - Najeebullah Shah

15
NSString *pURL = @"xyz.cpm";

if( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:pURL]])
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:pURL]];

12

这是最新代码,适用于iOS 11.3,并且如果您不想看到弃用警告,请使用此一行代码:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"https://example.com"] options:@{} completionHandler:nil];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接