how to open pdf on other application like ibooks - objective-c

I use UIDocumentinteractioncontroller to open pdf on other app like ibook.
But it is hard to find the doc about it.
Now I can present the open in part. but when i click on the icon of ibooks. nothing happend.
Do I need to do something in the delegate such as documentInteractionController:willBeginSendingToApplication:???

first you have to add the UIDocumentInteractionControllerDelegate in your .h file
i.e.:
#interface MyDocumentViewController : UIViewController <UIDocumentInteractionControllerDelegate>
in your .m file you add the protocol, which enables you to find out, what happens ...
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application{
NSLog(#"Send to App %# ...", application);
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application{
NSLog(#"Finished sending to app %# ...", application);
}
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller{
NSLog(#"Bye");
}
of course you have to set the delegate of the UIDocumentInteractionController to this module. I solved it like this:
-(BOOL)canOpenDocumentWithURL:(NSURL*)url inView:(UIView*)view {
BOOL canOpen = NO;
UIDocumentInteractionController* tmpDocController = [UIDocumentInteractionController
interactionControllerWithURL:url];
if (tmpDocController)
{
tmpDocController.delegate = self;
canOpen = [tmpDocController presentOpenInMenuFromRect:CGRectZero
inView:self.view animated:NO];
[tmpDocController dismissMenuAnimated:NO];
}
return canOpen;
}

Related

UIwebview open document (pdf) in other app

Hello everyone,
I am trying to open pdf file in uiwebview to other app. I can display popup like this.
I write the code like this. I put delegate also. UIDocumentInteractionControllerDelegate
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//other code
if([fileType isEqualToString:#"pdf"])
{
NSLog(#"It is pdf file.");
//other code to save to nsdocumentdirectory and then get this path
// file://localhost/var/mobile/Applications/024D40FF-4518-4526-BEAB- 2A0679FD6308/Documents/iphone_user_guide.pdf
NSURL *pdfURL = [NSURL fileURLWithPath:pathToDownloadTo];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:pdfURL];
[docController setDelegate:self];
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
}
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
I got this error.
void SendDelegateMessage(NSInvocation *): delegate (webView:didFinishLoadForFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
In some link, it say due to Javascript. I also need to use this. stringByEvaluatingJavaScriptFromString
But I have disabled that function and tested. Then, there is no error but the app crashes.
I would like to know if there is something left to transfer document.
Also, I would like to know how to add Uibarbutton like "Open in ibook or sth" in the uiwebview programmatically with some code as in safari when we open pdf file.
With Regards,

Cocoa OSX: How can I make an image draggable

I have a panel with an image on it, and I want to make it so that you can copy a file (not the image,the image is only going to server as an icon for the file) into a folder by “dragging” the image outside of the application and into any other application that accepts files being dragged into it (ex. Finder). How can I do this?
I implemented the NSDraggingSource protocol, but I’m not sure how to make the image draggable. It is currently inside of an ImageView, which is inside of an ImageViewCell.
Here is the protocol I implemented:
#import "DragNDropView.h"
#implementation
-(NSDragOperation)draggingSession:(NSDraggingSession *)session
sourceOperationMaskForDraggingContext: (NSDraggingContext) context{
switch(context){
case NSDraggingContextOutsideApplication:
return NSDragOperationCopy;
break;
default:
return NSDragOperationNone;
break;
}
}
-(void) draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint) screenPoint{
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
NSPaseBoardItem *contents = [[NSPasteboardItem alloc]
inithWithPasteboardPreopertyList:SDKFileName ofType:NSFileContentsPboardType];
[pboard writeObjects[NSArray arrayWithObjects:contents, nil]];
}
-(void)drawRect:(NSRect)dirtyRect{
SDKFileName = #"example.example";
[super drawRect:dirtyRect];
}
#end
I added the method - (id)initWithCoder(NSCode *)coder and I also added
- (BOOL)acceptesFirstMouse:(NSEvent *)event { return YES; }

Open in.. for html-pages (in Safari)

I was wondering if I could create an app which supports opening .html-pages.
For example, if the app supports .pdf, when opening a .pdf, a small gray box appears with the button "Open in myApp". Can I get something like this, but then for a webpage?
Hmm you are talking about UIDocumentInteractionController then.
Implement UIDocumentInteractionControllerDelegate in your UIViewController
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller { return self; }
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller { return self.view; }
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller { return self.view.frame; }
Then add a button to the navigation bar to popup the options box:
// example: opening a .html file
NSString *index = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
// self.controller is a UIDocumentInteractionController ivar
self.controller = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]];
self.controller.delegate = self;
CGRect rect = self.navigationController.navigationBar.frame;
rect.size = CGSizeMake(1500.0f, 40.0f); // move the box right down under the button
[self.controller presentOptionsMenuFromRect:rect inView:self.view animated:YES];
A list of the applications supporting a particular document should appear. If you didn't register your app to support a type of document you still get the option "QuickLook". All this happens on whatever application is interacting with the file (since the files themselves are not exposed on the UI).

Set Document to PDFview in controller constructor

I've created a NSWindow with a PDFView in xib file, I created a controller called MainController, there, I created a ibaction -(IBAction) openFileAction:(id) sender, it uses the method
-(void) openFile:(NSString *) path{
NSLog(#"Opening File %#",path);
PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:path]];
[pdfView setDocument: pdfDoc];
}
I linked the open menu item to openFileAction and pdf file is shown properly in PDFView after the click.
I'm doing a logic to receive a command line argument
-(MainController *) init{
[super init];
NSArray *myArgs = [[NSProcessInfo processInfo] arguments];
NSLog(#"pdf view %#", pdfView);
if ([myArgs count] >= 2 ){
[self openFile:[myArgs objectAtIndex:1]];
}
return self;
}
As you can see, I did an override in default constructor and in this context the pdfView is null then the file is not opened after the application/main Window load.
My question is, how can I open a pdf in a PDFView after the application load? Is there any hook to use after UI load?
If you want to do this when your window opens that your PDFView is in, use the windowDidLoad function in your MainController rather than trying to load it in init.
Thanks slycrel but windowDidLoad is a callback of NSWindowController. I found the solution by myself, the secret is
- (void) awakeFromNib{
//Do something after initialize UI components
}
All the best.

How do you implement the Method makeKeyAndOrderFront:?

I am making a new window open and would like to implement the method makeKeyAndOrderFront: for the window, i was wondering what code i would need to enter to do this.
Here is some of the code I've already got to open the window:
File 1 (The First Controller)
#import "PreferenceController.h"
#implementation PreferenceController
- (id)init
{
if (![super initWithWindowNibName:#"Preferences"])
return nil;
return self;
}
- (void)windowDidLoad
{
NSLog(#"Nib file is loaded");
}
File 2 (The Action Opening The Window)
#import "Prefernces_Delegate.h"
#import "PreferenceController.h"
#implementation Prefernces_Delegate
- (IBAction)showPreferencePanel:(id)sender
{
// Is preferenceController nil?
if (!preferenceController) {
preferenceController = [[PreferenceController alloc] init];
}
NSLog(#"showing %#", preferenceController);
[preferenceController showWindow:self];
}
The reason I am trying to do this is it has been suggested by a friend to solve a window opening problem.
You don't want to implement -makeKeyAndOrderFront:, you want to call it on your window in order to bring it to front and make it the key window. What does your showWindow: method do?
Somewhere after [preferenceController showWindow:self];:
[self.window makeKeyAndOrderFront:self];
or did you mean add a method to the controller?
// you should use a different method name, cause it's not the
// controller that is made key and ordered front.
- (void)makeKeyAndOrderFront:(id)IBAction {
[self.window makeKeyAndOrderFront:self];
}
Message makeKeyAndOrderFront is sent only after initiating the main event loop [NSApp run]. You may try to send the message from the main view by implementing the method viewWillDraw:
- (void)viewWillDraw
{
[window makeKeyAndOrderFront: nil];
}