【问题标题】:Why does this code NOT display Google.com为什么此代码不显示 Google.com
【发布时间】:2023-03-31 23:29:02
【问题描述】:

我有一个当前嵌入了帮助文件的现有应用程序;我正在尝试更改它们,以便它们访问 Internet 以获取本地化的帮助文件。这是我的代码:

//  make the popover
UIViewController* popoverContent = [UIViewController new];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 450, 500)];
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0];  //  frame color?
popoverContent.view = popoverView;

//resize the popover view shown in the current view to the view's size
popoverContent.preferredContentSize = CGSizeMake(450, 500);

NSURL *indexURL = [NSURL URLWithString:@"google.com"];

//  add the UIWebView for RichText
webView = [[UIWebView alloc] initWithFrame:popoverView.frame];
webView.backgroundColor = [UIColor whiteColor];  //  change background color here

//  add the webView to the popover
[webView loadRequest:[NSURLRequest requestWithURL:indexURL]]; //  load it...
[popoverView addSubview:webView];

//  if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
    [popoverController dismissPopoverAnimated:YES];
}

//create a popover controller to display the HELP text
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

[popoverController presentPopoverFromRect:((UIButton *)boHelpGeneralSetup).frame inView:self.view
                 permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

此代码在嵌入帮助文件时有效;为什么当我尝试从 Internet 上的网页获取文件时它不起作用?

【问题讨论】:

  • 什么时候嵌入哪个文件?
  • 一个 HTML 文件,包含网页上的 exact 措辞。这与问题无关。
  • 尝试使用 http:// 包含整个地址
  • @Mr.T 做到了! (我知道这让它更尴尬!)请重写您的评论作为答案,我会标记它,以便您获得积分。非常感谢... SD

标签: objective-c uiwebview nsurl


【解决方案1】:

来自文档:

一个 NSURL 对象由两部分组成——一个可能为 nil 的基本 URL 以及相对于基本 URL 解析的字符串。一个 NSURL 如果对象的字符串部分被完全解析,则该对象被认为是绝对的 没有基地;所有其他 URL 都被认为是相对的。

例如,当构造一个 NSURL 对象时,你可以指定 file:///path/to/web_root/ 作为基本 URL 和 folder/file.html 作为 字符串部分,如下:

在您的情况下,google.com 被视为字符串部分,但不是基地址。由于地址以 http 开头,因此您需要包含 url 的完整路径。

所以,它会是:

  NSURL *indexURL = [NSURL URLWithString:@"https://www.google.com"];

【讨论】:

  • 不客气!!!确保使用 https 而不是 http。 Apple 让用户从 ios9 开始使用 https。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2016-03-26
  • 2013-06-05
  • 2016-08-03
  • 2015-10-22
  • 1970-01-01
相关资源
最近更新 更多