【问题标题】:Load an URL into a webview from outside the application does not work从应用程序外部将 URL 加载到 web 视图中不起作用
【发布时间】:2011-04-25 17:09:27
【问题描述】:

我正在尝试从应用程序外部将 URL 加载到 webview 中。我已经正确设置了我的 info.plist 以支持 http、https。我的应用程序出现在处理程序列表(safari 首选项盘)中。 这是我的代码

我的 awakefromnib 里有这个

        NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
    [em 
     setEventHandler:self 
     andSelector:@selector(getUrl:withReplyEvent:) 
     forEventClass:kInternetEventClass 
     andEventID:kAEGetURL];

然后

//get the URL from outside the application
- (void)getUrl:(NSAppleEventDescriptor *)event 
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{

    // Get the URL
    NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] 
                        stringValue];

    [self initWithUrl:urlStr]; 
}

//Can be used as intializer to init the webview with a page
-(void)initWithUrl:(NSString *)url
{
    //load the lading home page
    [[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];



}

如果我 nslog 显示它的 url(所以它正确地得到它)。我的下一个方法也被调用。这种方法在我的应用程序内部工作正常。

问题在于,当我单击应用程序外部的链接时(一旦应用程序被选为默认浏览器)。我弹出窗口,但它没有加载 URL。它什么也不做。 任何想法 ?

【问题讨论】:

  • 还有什么问题?有任何错误、发生了什么行为以及您的预期是什么?
  • 哇,抱歉,是的,我忘记了这个问题。因此,点击浏览器外部的 URL 会打开它,但 webiew 只是不加载 URL。

标签: objective-c cocoa http webview httphandler


【解决方案1】:

什么是webview的“mainFrame”方法?那是自定义类吗?您是否将 Web 视图的框架添加到窗口的框架中?

【讨论】:

  • 我只是在 Apple 文档中举了一个例子,它包括在 webview 对象的主框架中加载所需的 URL:
  • 硬编码一些基本的 html,例如将正文背景设置为红色,看看是否出现。然后你就会知道它是在加载 webView 而不是 url,或者它甚至没有加载 webView。
猜你喜欢
  • 2013-04-08
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多