开发者

Looking for an equivalent for UIWebViewDelegate for porting an iOS app to OSX

I'm trying to port an iOS App to OSX and there is one thing I don't get. The iOS app uses UIWebView, to be more precise a开发者_如何学运维 UIView the implements the UIWebViewDelegate:

@interface Dialog : UIView <UIWebViewDelegate> {

and implements those three delegate Methods:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request

  navigationType:(UIWebViewNavigationType)navigationType {

- (void)webViewDidFinishLoad:(UIWebView *)webView {

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

Can someone give me a hint how I can port this to the normal OSX Framework? I know there is the WebView but it has 4 Delegates as far as I could figure it out and none of these has delegate Methods that sounds like those 3.

Thank you


For the first one, you'll probably want to use a WebPolicyDelegate. And for the other two, there are corresponding methods in WebFrameLoadDelegate:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
- (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame


Regarding the iOS UIWebView webView:shouldStartLoadWithRequest:navigationType:, to accomplish this using the OSX WebView:

Set the WebPolicyDelegate delegate for your WebView instance:

self.webview.policyDelegate = self;

Then implement the – webView:decidePolicyForNavigationAction:request:frame:decisionListener: method in your delegate:

-(void)webView:(WebView *)webView
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
        request:(NSURLRequest *)request
          frame:(WebFrame *)frame
decisionListener:(id < WebPolicyDecisionListener >)listener
{
    int actionKey = [[actionInformation objectForKey: WebActionNavigationTypeKey] intValue];
    if (actionKey == WebNavigationTypeOther)
    {
        [listener use];
    }
    else
    {
        //
        // Here is where you would intercept the user navigating away 
        // from the current page, and use `[listener ignore];`
        //

        NSLog(@"\n\nuser navigating from: \n\t%@\nto:\n\t%@",
              [webView mainFrameURL],
              [[request URL] absoluteString]);

        [listener use];
    }

}


-(void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener

I am using this instead of shouldStartLoadWithRequest, this is working well

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜