Cocoa app; open file (pdf) in Preview - objective-c

I have a file path to a pdf and I want it to open in Preview. Also, I want finder to launch highlighting the file.
On iOS I would do:
UIApplication app = [UIApplication sharedApplication];
[app openURL:url];
But I have no idea what's the alternative in Cocoa app.
Thanks

You should look at NSWorkspace
NSString* path = [[NSString alloc] initWithBytes:installerPath.str() length:installerPath.dataLength() encoding:NSUTF16LittleEndianStringEncoding];
[[NSWorkspace sharedWorkspace] openFile:path];
[path release];

Related

How to make Chrome open a URL using ScriptingBridge in Objective-C?

I have working Objective-C code that uses ScriptingBridge to make Safari open a URL. Something like:
#import "Safari.h" /* created by executing "sdef /Applications/Google\ Chrome.app | sdp -fh --basename GoogleChrome" */
if ((safariApp = [SBApplication applicationWithBundleIdentifier:#"com.apple.Safari"]) == nil) {
NSLog(#"couldn't access Google Chrome");
} else {
NSString *theUrl = [NSString stringWithUTF8String:"http://www.ford.com"];
NSDictionary *theProperties = [NSDictionary dictionaryWithObject:theUrl forKey:#"URL"];
SafariDocument *doc = [[[safariApp classForScriptingClass:#"document"] alloc] initWithProperties:theProperties];
[[safariApp documents] addObject:doc];
}
I'd like to create similar code that will do the same thing for Chrome instead of Safari. Obviously I need to change "Safari.h" to "GoogleChrome.h" and "com.apple.Safari" to "com.google.Chrome". I'm not sure how to change the last three lines - there's no definition of "GoogleDocument" in GoogleChrome.h
GoogleChromeApplication *application = [SBApplication applicationWithBundleIdentifier:#"com.google.Chrome"];
GoogleChromeWindow *window = [[[application classForScriptingClass:#"window"] alloc] initWithProperties:nil];
[application.windows addObject:window];
window.activeTab.URL = #"http://www.example.com";
[window release];
[application activate];
The only way I found to get what you need is with AppleScript.
NSString *script = #"tell application \"Google Chrome\" to \
open location \"http://www.ford.com\"";
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithSource: script];
[appleScript executeAndReturnError:nil];
This works with Safari and Firefox as well (of course you need to change \"Google Chrome\" with \"Safari\" or \"Firefox\").

Can't see pdf in UIWebView (the app is approved by Apple)

One of my app's functions is to present PDF file (from app bundle) in a UIWebView.
On my iPhone it works fine, but I've got some reports that on other devices PDF file isn't presented.
What could be the problem? Could it have something to do with iOS version on those devices?
This is how I present the file inside the app:
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:self.fileName ofType:#".pdf"];
if (!pdfPath) {
Helper* h = [[Helper alloc]init];
pdfPath = [h filesinADocumentFolder:self.fileName];
}
self.pdfUrl = [NSURL fileURLWithPath:pdfPath];
[self.webView1 loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
[self.webView1 setBackgroundColor:[UIColor clearColor]];
[self.webView1 setOpaque:NO];

How to play a Vimeo video in iOS?

I've tried to search to web, but I couldn't find a topic not older than 1 year regarding this problem, therefore;
How can I play a Vimeo video in an iOS App?
EDIT1: When using the solution I'm sometimes getting this HTTP response from Vimeo
Why?
This is my way of play a Vimeo video inside a app.
I am using iFrame to load Vimeo video inside my app.
follow this steps and you will too.
create a uiwebview and connect it to your .h file. Mine is _webView.
Add this method to your .m file.
-(void)embedVimeo{
NSString *embedHTML = #"<iframe width=\"300\" height=\"250\" src=\"http://www.vimeo.com/embed/rOPI5LDo7mg\" frameborder=\"0\" allowfullscreen></iframe>";
NSString *html = [NSString stringWithFormat:embedHTML];
[_webView loadHTMLString:html baseURL:nil];
[self.view addSubview:_webView];
}
I am using the embedded code in Vimeo video. (I hope you know what it is)
call this method inside your viewdidload
[self embedVimeo];
Run the app and you will see the video in your view. This way is perfectly working for me and i think this will help for your too.
You can use YTVimeoExtractor, works fine for me.
You can use this code
NSString *htmlStringToLoad = [NSString stringWithFormat:#"http://player.vimeo.com/video/%#?title=0&byline=0&portrait=0\%%22%%20width=\%%22%0.0f\%%22%%20height=\%%22%0.0f\%%22%%20frameborder=\%%230\%%22", videoID];
[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:htmlStringToLoad]]];
Please try this,
It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}
Use Below Code the it will work fine
NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ;
[html appendString:#"<html><head>"];
[html appendString:#"<style type=\"text/css\">"];
[html appendString:#"body {"];
[html appendString:#"background-color: transparent;"];
[html appendString:#"color: white;"];
[html appendString:#"}"];
[html appendString:#"</style>"];
[html appendString:#"</head><body style=\"margin:0\">"];
[html appendString:#"<iframe src=\"//player.vimeo.com/video/84403700?autoplay=1&loop=1\" width=\"1024\" height=\"768\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>"];
[html appendString:#"</body></html>"];
[viewWeb loadHTMLString:html baseURL:urlMovie];
I've used this code:
NSString *embedSr = #"<iframe width=\"304\"height=\"350\" src=\"http://player.vimeo.com/video/... \" frameborder=\"0\" allowfullscreen></iframe>";
[[self WebView] loadHTMLString:embedSr baseURL:nil];
I've tried the universal player, it is successful in device with iOS 5, but failed in iOS 4.2 with iPhone 3G. I don't know why. Here's the link to embed it.
Or you can embed manually from the Vimeo site, click embed, and configure the config as you wish.

NSWorkspace lauchApplication weirdness - it only works from within Xcode

#implementation button
- (IBAction)doSomething:(id)pId;
{
NSFileManager *filemgr;
NSString *currentpath;
NSString *appPath;
filemgr = [[NSFileManager alloc] init];
currentpath = [filemgr currentDirectoryPath];
appPath=[currentpath stringByAppendingString:#"/resources/systemRun.app"];
[[NSWorkspace sharedWorkspace] openFile:appPath withApplication:nil];
exit(0);
}
#end
This code works perfectly when I build & run from Xcode, or when I right click the "product" from within Xcode. However when I run the application from the finder it never launches ... Even from within the very build folder.. wtf ?
Don't use currentDirectoryPath to find your resources directory.
NSString *appPath = [[NSBundle currentBundle] pathForResource:#"systemRun" ofType:#"app"];

Relaunching application via NSTask ignores LSMinimumSystemVersionByArchitecture

I'm running into an issue with relaunching my application on 10.5. In my Info.plist I have LSMinimumSystemVersionByArchitecture set so that the application will run in 64-bit for x86_64 and 32-bit on i386, ppc, and ppc64.
I have a preference in the app that allows the user to switch between a Dock icon & NSStatusItem, and it prompts the user to relaunch the app once they change the setting using using the following code:
id fullPath = [[NSBundle mainBundle] executablePath];
NSArray *arg = [NSArray arrayWithObjects:nil];
[NSTask launchedTaskWithLaunchPath:fullPath arguments:arg];
[NSApp terminate:self];
When this executes on 10.5, however it relaunches the application in 64-bit which is not a desired result for me. From what I gather reading the docs its because the LS* keys are not read when the app is launched via command line.
Is there a way around this? I tried doing something like below, which worked on 10.6, but on 10.5 it was chirping at me that the "launch path not accessible". ([NSApp isOnSnowLeopardOrBetter] is a category that checks the AppKit version number).
id path = [[NSBundle mainBundle] executablePath];
NSString *fullPath = nil;
if (![NSApp isOnSnowLeopardOrBetter])
fullPath = [NSString stringWithFormat:#"/usr/bin/arch -i386 -ppc %#", path];
else
fullPath = path;
NSArray *arg = [NSArray arrayWithObjects:nil];
[NSTask launchedTaskWithLaunchPath:fullPath arguments:arg];
[NSApp terminate:self];
You should instead use the methods of NSWorkspace, which does take into account Info.plist keys. For example, use -(BOOL)launchApplication:(NSString*).
It's because you use spaces inside fullpath, use arguments within an array [NSArray arrayWithObjects:#"/usr/bin/arch",#"-i386",#"-ppc",path,nil].
try this code for relaunch your app:
//terminate your app in some of your method:
[[NSApplication sharedApplication]terminate:nil];
- (void)applicationWillTerminate:(NSNotification *)notification {
if (i need to restart my app) {
NSTask *task = [NSTask new];
[task setLaunchPath:#"/usr/bin/open"];
[task setArguments:[NSArray arrayWithObjects:#"/Applications/MyApp.app", nil]];
[task launch];
}
}