Mac-equivalent to iOS's "shouldStartLoadWithRequest"

Discussion in 'Mac Programming' started by Danneman101, Jan 10, 2011.

  1. Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    I have a webview in my app loading a local html-file that I wish to communicate with the obj-c of the app.

    I used to be able to trigger the "shouldStartLoadWithRequest"-function in iOS to by pressing any <a href>-link, but that function seems to be absent in MacOS. What would be the equivalent of that function in MacOS?
     
  2. jiminaus, Jan 10, 2011
    Last edited: Jan 10, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    There is no direct equivalent AFAIK.

    If you don't want to be able to cancel the navigation, just react to it. You could use webView:didStartProvisionalLoadForFrame:. It would be defined in the object you set as the webView's frameLoadDelegate.

    There's also webView:resource:willSendRequest:redirectResponse:fromDataSource: defined in an object you set as the webView's resourceLoadDelegate. This lets you return an altered NSURLRequest that WebView will ultimate instead of the original URLRequest.

    Or do you want the link to actually trigger Objective-C code instead of navigating to another page? If you do, look at the CallJS sample. It shows how you can have a button or link send a message to an Objective-C object.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    I think webView:decidePolicyForNavigationAction:request:frame:decisionListener: might be what you want. It works differently, requiring you to use [listener ignore] or [listener use], instead of returning YES or NO.
     
  4. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #4
    Well, what I need to retrieve is the NSURLRequest of the current webpage that's loaded to the webview. With shouldStartLoadWithRequest this would be the code:

    Code:
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    	NSString *path = [[request URL] path];
    	// do stuff with path
    }
    
    The closest thing of the suggestions seems to be the "webView:resource:willSendRequest:redirectResponse:fromDataSource:" since it seems to give a NSURLRequest as well, and that way I don't have to adjust my code more than necessary.

    Can't seem to get it to trigger from pressing a html-link though. The webView:didStartProvisionalLoadForFrame does work but doesn't give a NSURLRequest, so that's a no go... Maybe I'm setting things up incorrectly. Here's my code:

    Code:
    - (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
    {
    	NSString *path = [[request URL] path];
    	// do stuff with path
    }
    
    I've tried logging to see if the function is executed on an html-link press, but no :/
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    WebView has multiple delegates. It sounds like you've only set the frameLoadDelegate. To use webView:resource:willSendRequest:redirectResponse:fromDataSource:, you'll need to set the resourceLoadDelegate.

    BTW If you can always get the URL by sending mainFrameURL to the webView.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Did you set it, via setResourceLoadDelegate: ?
     
  7. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #7
    In the MainMenu.xib, selecting "webView", I dragged the "resourceLoadDelegate" to my app delegate in the Outlets. This apparently worked for the frameLoadDelegate, at least.
     
  8. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #8
    I finally did manage to get decidePolicyForNavigationAction to work. I must have inserted a typo or something because the setResourceLoadDelegate was set up ok. Anyway, I can now get a hold of the the url of a clicked <a href>-tag inside a html-file in my webview and have specific functionality execute as a response to the particular url.

    For anybody interested in how it's implemented here is the code - perhaps it will be of some help:

    Code:
    - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
    {	
    	// GET:	Whole URL
    	NSString *path = [[request URL] path];				// "/Users//info//Documents//myapp//build//Debug//myapp.app//Contents//Resources//alert_FUNCTION1"
    
    	// Trims the path to the last bit (here "alert_FUNCTION1")
    	NSString *pathTrimmed = [path lastPathComponent]; 
    	
    
    	// IF-ELSE STATMENETS:
    	if ([pathTrimmed isEqualToString:@"alert_FUNCTION1"]) {
    		// do something
    	}
    
    ...etc...
    
    }
    
    Thank you guys for helping me out here :)
     

Share This Page