【问题标题】:Passing NSString to Webview将 NSString 传递给 Webview
【发布时间】:2013-02-28 04:08:59
【问题描述】:

我有一个名为状态的NSString,我正在尝试传递该变量以加载webView。该应用程序在这里崩溃,但我不知道我做错了什么?有人发现请求有什么问题吗?

[webView loadRequest:[NSURLRequest requestWithURL:[NSString 
stringWithFormat:@"http://www.website.com/page.php?status=%@", status]]];

【问题讨论】:

  • 应用程序正在崩溃,我在阅读崩溃日志方面非常糟糕。
  • 您可以编辑问题并包含您收到的错误消息吗?尝试在调用之前记录status 的值。
  • 在调试器中运行应用程序。如果它崩溃,Xcode 将在控制台中显示错误。发布完整的错误。调试器还将显示导致问题的代码行。发布该代码。
  • 谢谢大家 - 你们是对的 - 作为 NSString 而不是 NSURL 传递。按建议转换

标签: iphone ios objective-c xcode uiwebview


【解决方案1】:

requestWithURL接受NSURL,所以需要将字符串转换成NSURL

试试这个,

[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.website.com/page.php?status=%@", status]]]];

【讨论】:

    【解决方案2】:

    +[NSURLRequest requestWithURL:] 期望你给它一个NSURL 对象。你给它一个NSString。不要那样做。 :)

    【讨论】:

    • 哇,我不敢相信我错过了。我想知道为什么编译器没有警告他。
    • @JackLawrence 太多人认为可以忽略警告。对于我设置的每个项目,我要做的第一件事就是打开“将警告视为错误”编译器设置。
    • @rmaddy 我被诱惑了,但我从未尝试过。
    【解决方案3】:
    NSString *urlAddress = [NSString stringWithFormat:@"http://www.website.com/page.php?status=%@", status];
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    //Load the request in the UIWebView.
    [webView loadRequest:requestObj];
    

    【讨论】:

      【解决方案4】:

      使用这个:

      [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString  stringWithFormat:@"http://www.website.com/page.php?status=%@", status]]]];
      

      【讨论】:

      • NSURL URLWithString: 这个新的
      • @Ayaz 我相信重点是在其他答案出现之后,您只是在重复其他答案。你没有理由回答。它没有提供任何尚未说明的新内容。何苦?而且您发布的代码无论如何都是无效的。它不会编译。
      猜你喜欢
      • 2014-07-30
      • 1970-01-01
      • 2011-08-17
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多