Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Xcode WebView download functionality, right click issues

Hi


I've built a simple XCode 4project with one window containing a WebView. It opens html files, with the index file (and most other html) local to the project. So I have a fast light app 'containing' a website with a very simple UI for that website.


Everything works fine - links to the other pages are followed nicely. I even figured out 'go back' functionality in the menus.


There are only two problems that I can't solve, and they both relate to being able to get Word Templates and Zip files out of the app, either by clicking on the link, right-clicking and choosing download, or dragging a link to the file to the desktop - the usual way of doing these things in Safari.


In a basic cocoa WebView, when clicking links other than html, PDF or images, nothing happens. A right-click on a link gives rise to a pop-up list (Open Link, Open Link in New Window, Download Linked File, Copy Link), but nothing happens when the user chooses 'Download Linked File' - only Copy Link works. Dragging a link to the desktop (as in Safari) makes a .fileloc alias (this is quite good for Word Templates, because they open in Word when launched, but don't do anything if they are a zip file).


I'm trying to get some download functionality working. Ideally a Drag would put a copy of the linked file on the desktop. And ideally the Download Linked File would work, even if only to the Desktop, but if that was not possible, the entire right-click-on-link ability would be blocked.


I appreciate that download functionality has to be coded somehow, and I'm seeking the simplest thing possible.


I've included the code so far. The Xib has only one window with only one webView on it. There is a 'go back' button that is visible if there is something to go back to, and it works. The webView is linked to the testAppDelegate by dragging the frameLoadDelegate, downloadDelegate, and policyDelegate to the App Delegate icon, whice made it possible for the 'sender didFinishLoadForFrame:' code to work.


From this link:

http://stackoverflow.com/questions/9095245/how-do-i-allow-downloads-from-a-webvi ew-using-the-standard-download-system-on-a

I found some code with the decision lister stuff. With this I can get the mime type.


But that's as far as I can get.


I would be very grateful indeed if someone could help me sort out a solution to these tricky problems. I appreciate the complexity of doing so!


I have put a zip archive of my XCode here: http://dl.dropbox.com/u/9505117/XCode_Test.zip


Thanks for your help!


Chris.




My testAppDelegate.h file:


#import <Cocoa/Cocoa.h>

#import <WebKit/WebKit.h>


@interface testAppDelegate : NSObject <NSApplicationDelegate>{

@private

NSWindow *MyWindow;

NSButton *backButton;

WebView *webView;

}


@property (nonatomic, retain) IBOutletNSWindow *MyWindow;

@property (nonatomic, retain) IBOutletWebView *webView;

@property (nonatomic, retain) IBOutletNSButton *backButton;


- (IBAction)goBack:(id)sender;

- (IBAction)goBackMenu:(id)sender;


@end





My testAppDelegate.m file:


#import "testAppDelegate.h"


@implementation testAppDelegate


@synthesize MyWindow;

@synthesize backButton;

@synthesize webView;


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{

// NSString *urlString = @"http://www.apple.com";

// Insert code here to initialize your application

NSString *resourceDir = [[NSBundle mainBundle] resourcePath];

NSArray *pathComponents = [NSArray arrayWithObjects:resourceDir, @"html", @"index.html", nil];

NSURL *indexUrl = [NSURL fileURLWithPathComponents:pathComponents];

NSURLRequest *req = [NSURLRequest requestWithURL:indexUrl];

[[webView
mainFrame] loadRequest:req];

}


- (IBAction)goBack:(id)sender{

[webView goBack];

}


- (IBAction)goBackMenu:(id)sender {

[webView goBack];

}


- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame

{

if ([webView canGoBack]) {

//NSLog (@"can go back");

[backButton setHidden:NO];

}

else{

[backButton setHidden:YES];

// NSLog (@"can't go back");

};

}


- (void)webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type

request:(NSURLRequest *)request

frame:(WebFrame *)frame

decisionListener:(id < WebPolicyDecisionListener >)listener

{

if([type isEqualToString:@"application/zip"])

{

//NSLog (type);

[listener download];

}

else{

//NSLog (type);

[listener use];

};

}


- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender

{

returnYES;

}



Phew!

Posted on Apr 3, 2012 2:02 AM

Reply

There are no replies.

Xcode WebView download functionality, right click issues

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.