【问题标题】:iPhone: Open a url ProgrammaticallyiPhone:以编程方式打开网址
【发布时间】:2010-12-12 00:35:29
【问题描述】:

我是 iPhone 新手。 我想在我的应用程序中打开一个 url。 我怎样才能完成这项任务?请建议我并提供一些有用的链接。

【问题讨论】:

标签: iphone url


【解决方案1】:

如果你想打开并从 URL 中获取数据,你可以使用 NSString:

NSString *ans = [NSString stringWithContentsOfURL:url];

如果你想从一个 URL 获取一个 XML,你可以直接使用 NSXMLParser:

NSURL *url = [[NSURL alloc] initWithString:urlstr];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
// parse here
[parser release];
[url release];

另一方面,如果打开你的意思是在嵌入式浏览器中打开一个 URl,你可以使用 UIWebView 类。

【讨论】:

    【解决方案2】:

    更新(2016 年): 现在最好的方法是实例化并呈现一个 SFSafariViewController。这为用户提供了 Safari 的安全性和速度,并且无需离开您的应用即可访问他们可能已经设置的任何 cookie 或 Safari 功能。

    如果您想在 Safari 中打开 URL(并退出您的应用程序),您可以使用 openURL method of UIApplication

    如果您希望在应用内部处理它,请使用 WKWebView。

    【讨论】:

      【解决方案3】:

      显然上面给出的链接已经过时了。这是UIApplication 类的更新链接。

      快速简单的代码sn-p是:

      // ObjC
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.google.com"]];
      
      // Swift
      UIApplication.shared.open(URL(string: "http://www.google.com")!, options: [:], completionHandler: nil)
      

      【讨论】:

        【解决方案4】:
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"https://medium.com/the-traveled-ios-developers-guide/swift-3-feature-highlight-c38f94359731#.83akhtihk"]]) {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://medium.com/the-traveled-ios-developers-guide/swift-3-feature-highlight-c38f94359731#.83akhtihk"]];
                    }
                    else{
                        [SVProgressHUD showErrorWithStatus:@"Please enable Safari from restrictions to open this link"];
                    }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-29
          • 1970-01-01
          • 2010-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多