2012-01-04 289 views
0

我正在寫一個MacRuby(objective-c)應用程序,它基本上是一個默認打開特定網站(我的)的網絡瀏覽器。如何強制WebView在默認瀏覽器中打開鏈接?

但是,我不希望鏈接在WebView中打開。我寧願他們在用戶的默認瀏覽器中打開。這是迄今爲止的代碼,但它似乎並沒有調用decidePolicyForNavigation方法。

framework "WebKit" 

class AppDelegate 
    attr_accessor :window 

    def applicationDidFinishLaunching(notification) 
     load_web_view 
    end 

    def load_web_view 
     web_view = WebView.new 
     request = NSURLRequest.requestWithURL(NSURL.URLWithString("http://example.com")) 
     web_view.mainFrame.loadRequest(request) 
     window.contentView = web_view 
     web_view.frameLoadDelegate = self 
     web_view.setPolicyDelegate(self) 
    end 

    # this makes it so links open in the default browser 
    def webView(view, decidePolicyForNavigationAction:actionInformation, request:request, frame:frame, decisionListener:listener) 
     puts 'running nav policy' 
     listener.ignore 
     NSWorkspace.sharedWorkspace.openURL(request.URL) 
    end 
end 

我在做什麼錯?

回答

0

如果您的目標是用用戶的默認瀏覽器簡單打開URL,則不想直接使用WebKit - 您想使用LaunchServices API。請參閱http://developer.apple.com/library/mac/#documentation/Carbon/Reference/LaunchServicesReference/Reference/reference.html

+0

因此,使用LaunchServices API將取代我的'NSWorkspace.sharedWorkspace.openURL(request.URL)'?我仍然需要防止在WebView內打開這些外部鏈接,並且我的「策略方法」沒有被調用。你知道我做錯了什麼嗎? – Andrew 2012-01-05 00:14:53

+0

也許我誤解了這裏的大局。首先,你爲什麼要創建一個webview呢?它的目的是什麼? – jkh 2012-01-05 17:38:03

+0

這是一個圍繞我的網絡聊天室應用程序的GUI包裝。當有人在聊天室中發佈鏈接時,我希望這些鏈接在用戶的默認瀏覽器中打開,而不是離開聊天室並在800×400px的小型聊天窗口中顯示頁面。 – Andrew 2012-01-05 18:23:15

相關問題