I would like to open a PDF within a Cocoa application. I tried the following to open the PDF using Preview.
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:#"filepath"]];
But I would like to open the PDF attached to a window, similar to Xcode's Help window. How do you achieve this using PDFKit?
I imported QuartzFrameWork, and added a PDFView in the xib. The following code did the job for me:
NSURL *url = [NSURL fileURLWithPath:#"filePath"]
PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:url];
[_pdfView setDocument:pdfDoc];
You can use PDFView for opening PDF files within your application.
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/PDFKitGuide/PDFKit_Prog_Intro/PDFKit_Prog_Intro.html#//apple_ref/doc/uid/TP40001863-CH203-SW1
in .h file
#interface MyViewController:UIViewController <UIDocumentInteractionControllerDelegate>
#property(nonatomic, retain) UIDocumentInteractionController *documentInteractionController;
in .m file
#synthesize documentInteractionController;
- (void)viewDidLoad{
[super viewDidLoad];
documentInteractionController = [[UIDocumentInteractionController alloc] init];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
-(void) openPdf{
NSString *filePath=filePath = [[NSBundle bundleForClass:[self class]] pathForResource:#"filename" ofType:#"pdf"];
NSURL *URL = [NSURL fileURLWithPath:filePath];
if (URL) {
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
[self.documentInteractionController setDelegate:self];
[self.documentInteractionController presentPreviewAnimated:YES];
}
}
Related
I've been through all the posts in StackOverflow but I can't get a full example of how to get a video from the bundle and reproduce it in a controller.
I've tried many things but my screen appeared blank or black.
In the last amend, my video remains like this:
no error is thrown on the log. This is my code:
.h (please ignore the name of the controller, it will be finally a camera)
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#interface NativeCameraViewController : UIViewController
#end
.m
#import "NativeCameraViewController.h"
#interface NativeCameraViewController ()
#property (nonatomic, retain) AVPlayerViewController *avPlayerViewcontroller;
#end
#implementation NativeCameraViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = self.view;
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"video" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:stringPath];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:fileURL];
self.avPlayerViewcontroller = playerViewController;
[self resizePlayerToViewSize];
[view addSubview:playerViewController.view];
view.autoresizesSubviews = TRUE;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) resizePlayerToViewSize
{
CGRect frame = self.view.frame;
NSLog(#"frame size %d, %d", (int)frame.size.width, (int)frame.size.height);
self.avPlayerViewcontroller.view.frame = frame;
}
#end
and that's what I've got so far.
My video is correctly on the Bundle, called video.mov:
I've tried both linking and copying. Same result.
What am I doing wrong??
Thank you very much in advance.
I'd like to avoid using MPMoviePlayerController since it's deprecated in iOS9
It was actually my fault...
I was trying to reproduce an alias.
When you have 20.000 videos and 30.000 resources this things happen.
Instead of using AVPlayer, use MPMoviePlayerController class:
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];
Seems to be similar questions asked but none of the solutions seem to work for me. Cant for the life of me understand why my code doesnt work so here it is.
I dont get any errors but also get no sound.
#interface MainApp ()
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
-(void)playSoundFX:(NSString*) soundFile;
#end
#implementation MainApp
- (void)viewDidLoad {
[super viewDidLoad];
// Play sound
[self playSoundFX:#“sound.mp3"];
}
// Play sound
- (void)playSoundFX:(NSString*)soundFile {
// Setting up path to file url
NSBundle* bundle = [NSBundle mainBundle];
NSString* bundleDirectory = (NSString*)[bundle bundlePath];
NSURL *url = [NSURL fileURLWithPath:[bundleDirectory stringByAppendingPathComponent:soundFile]];
// Make an audioPlayer object and play file
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer setVolume:100.0f];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
}
#end
*********vvvvvvvvv Amended Code that works vvvvvvvvv**********
#interface MainApp ()
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
-(void)playSoundFX:(NSString*) soundFile;
#end
#implementation MainApp
- (void)viewDidLoad {
[super viewDidLoad];
// Play sound
[self playSoundFX:#“sound.mp3"];
}
// Play sound
- (void)playSoundFX:(NSString*)soundFile {
// Setting up path to file url
NSBundle* bundle = [NSBundle mainBundle];
NSString* bundleDirectory = (NSString*)[bundle bundlePath];
NSURL *url = [NSURL fileURLWithPath:[bundleDirectory stringByAppendingPathComponent:soundFile]];
// Make an audioPlayer object and play file
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[self.audioPlayer setVolume:100.0f];
if (self.audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[self.audioPlayer play];
}
#end
I am execute your code on my iPhone. It is work properly.Please Select a device and run your code. I hope it will execute properly with sound.
You got to segregate the functionalities..
Loading audio file in viewDidLoad and play it on play action, some thing like this
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
{
AVAudioPlayer *_audioPlayer;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Construct URL to sound file
NSString *path = [NSString stringWithFormat:#"%#/sound.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
// Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playSoundTapped:(id)sender
{
// When button is tapped, play sound
[_audioPlayer play];
}
My app creates a pdf, that can be opened in the regular menü and then viewed, but not in the app. How to programatically open pdf in the ipad reader?
You will have to set the delegate in your header's ViewController:
#interface YourViewController : UIViewController <UIDocumentInteractionControllerDelegate>
Then you'll have to implement this method:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
And then, when you catch which pdf has to be opened:
NSString *sPathPDF = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"name.pdf"]]; //path example for a local stored pdf
NSURL *urlPDF = [NSURL fileURLWithPath:sPathPDF];
UIDocumentInteractionController *dicPDF = [UIDocumentInteractionController interactionControllerWithURL: urlPDF];
[dicPDF setDelegate:self];
[dicPDF presentPreviewAnimated: YES];
This is my Code that all together crashes my app
The bit that assigns the webview its value: It works fine until the last line. When it SIGBARTS.
[cell setLabelText:[cellTitle objectAtIndex:indexPath.row]];
[cell imagesetter:[imageTitle objectAtIndex:indexPath.row]];
[cell desSetter:[desTitle objectAtIndex:indexPath.row]];
[cell changeProductWeb:[webTitle objectAtIndex:indexPath.row]];
The Array
webTitle = [[NSArray alloc] initWithObjects:
#"http://www.google.com/",
#"http://www.google.com/",
#"http://www.google.com/",
#"http://www.google.com/",
nil];
The action
- (IBAction) changeProductWeb:(NSString *)str{
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
This is the only bit the crashes the program. When I remove it it wont crash.
You probably need to call [webView retain] at some point. You would get a SIGABART like that if your webView is not being held onto by your class.
A simple solution is to use this in your .h file:
#property (nonatomic,retain) UIWebView *webView;
Then in your .m file go:
#synthesize webView;
Then, wherever you refer to your webview, make sure to add "self"
[self.webView loadRequest:requestObj];
I try to display a simple webpage inside my UIWebView without a Nib. The problem is when I click on my buttun a new page blanck page appear but nothing is display.
Did I miss something?
- (void)loadView {
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.webView = web;
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [[[NSURL alloc] initWithString:urlAddress] autorelease];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[topView addSubview:self.webView];
[web release];
}
thank you,
If this is the exact code you're using then it can't work: the webView added to topView that is never put on screen anywhere.
You probably want to add the webView to the controller view, but a better place to do that might be viewDidLoad, where self.view can be used safely.
This code works for me:
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[[UIWebView alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [[[NSURL alloc] initWithString:urlAddress] autorelease];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.view addSubview:self.webView];
}
Using the Storyboard -
Step 1: Drag and drop a UIWebView in the View
Step 2: Then go to the .h file (for that particular view) and create an IBOutlet of a UIWebView. For example-
// MainViewController.h
#interface MainViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIWebView *myWebView;
#end
Step 3: Go to the storyboard and create a connection from outlet, myWebView (This can be found in the inspector area of Xcode) to the UIWebView by doing a control drag.
Step 4: Now when we have the connection, we just need to go to the .m (for that particular view) and add the following code -
//MainViewController.m
#implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlNameInString = #"https://www.google.com";
NSURL *url = [NSURL URLWithString:urlNameInString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:urlRequest];
}
#end
Here is My Solution.
Create a ViewController with WebView in Interface Builder and connect webview as IBOutlet
This code is simple and works fine
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *request = [NSURLRequest requestWithURL:#"http://www.google.com.ua"];
[webView loadRequest:request];
}