How to know an application is first run on a Mac - xcode

On Windows, we can write values into registry to know that
but how can I know if my application is the first time it runs on a mac? I need to perform some initialization task.
Thanks

You are looking for the class NSUserDefaults (see Apple Documentation)
For example:
#define kAlreadyBeenLaunched #"AlreadyBeenLaunched"
if (! [[NSUserDefaults standardUserDefaults] boolForKey:kAlreadyBeenLaunched]) {
// This is our very first launch
// Setting userDefaults for next time
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:kAlreadyBeenLaunched];
// Do your first time stuff
//<##>
}
You will use the same class to save and retrieve user preferences.
This values will be saved in ~/Library/Preferences/<your_bundle_id>.plist. This is useful to know for debugging, by looking at the file, but you should not rely on this implementation detail in your code.

There are tons of other people that already asked this.
I guess this one is the most helpful. iPhone: How do I detect when an app is launched for the first time?
Mention: It's working exactly the same way as on the iOS system.

Related

IOS 8 : CLVisit in CoreLocation

I m trying to use new CLVisit feature in my app to monitor user's visit, i followed the WWDC-14 video and implemented every thing, but locationManager:didVisit is not calling at all. I set the NSLocationAlwaysUsageDescription key, enable the "Location updates" background mode in Capabilities, and include the following in ViewDidLoad of my class :
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[_locationManager requestAlwaysAuthorization];
[_locationManager startMonitoringVisits];
There is no documentation available on min TIME required to stay at specific place for a visit or anything about DISTANCE between places.
I tried to test it using GPX file and in the building by staying at different places for more than 10 min, but locationManager:didVisit delegate didn't fired.
Please help me, how to test CLVisits, or if i missed anything in code.
I´ve been testing CLVisits with this code and works perfect to monitor my day a day
https://github.com/steveschauer/TestCLVisit

Implementing GameKit.framework on OSX, cannot authenticate localPlayer

I'm attempting to implement GameKit in my OSX game. Unfortunately I can't find much information about how to do this; all tutorials seem to be iOS (though the documentation clearly states "Game Center is available on iOS and OS X").
Everything is compiling fine; the problem comes when I try to authenticate the local user:
[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(id viewController, NSError *error) {
if(error) {
DLog(#"Error: %#",error);// This is always returning an error
}
else if(viewController) {
// WHAT DO I DO HERE??
}
}];
I have 2 problems:
First, the handler always gets an error: Error Domain=GKErrorDomain Code=6 "The requested operation could not be completed because local player has not been authenticated." UserInfo=0x10103bc70 {NSLocalizedDescription=The requested operation could not be completed because local player has not been authenticated.}.
Second, I don't know how to present the view controller.
On the iPhone, this code works fine: there's no error, and I simply present the viewController (which is the login screen).
I'm sorry, and I realize that my answer is almost a year late, but in case it may still be relevant for others who are still asking this question like myself. Ed Marty is mostly correct, but what I've discovered that works for me is this.
[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(NSViewController < GKViewController > viewController, NSError *error) {
if(error) {
NSLog(#"Error: %#",error);
}
else if(viewController) {
GKDialogController *presenter = [GKDialogController sharedDialogController];
presenter.parentWindow = myWindow;
[presenter presentViewController:viewController];
}
}];
The main difference is using NSViewController conforming to GKViewController instead of id.(Also, I used NSLog instead of DLog, but that isn't too important).
"I have found, however, that this is completely useless, and it presents the login dialog before it even calls the handler."
To make sure that it works, set up a new Game Center account through your app. When you run your program and it loads a window for you to sign in, press "Create New Apple ID" even if you already have an Apple ID. The button may not work, so if that's the case, open up Game Center and press "Create New Apple ID." Either way, your objective is to create a "Sandboxed" Game Center account, which you can learn more about here: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/TestingYourGameCenter-AwareGame/TestingYourGameCenter-AwareGame.html#//apple_ref/doc/uid/TP40008304-CH17-SW1
You can tell if the account is Sandboxed if when you are reading the terms and conditions, the word "Sandbox" appears on a yellow banner in the top left. Once again, I'm sorry I'm late, but hopefully this clears the topic for all future viewers.
P.S. I'm sorry that the code did not format properly.
I have found that the GameKit documentation for OSX is woefully lacking, misleading, and sometimes downright wrong. That said, here's what you're supposed to do, according to this document:
[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(id viewController, NSError *error) {
if(error) {
DLog(#"Error: %#",error);// This is always returning an error
}
else if(viewController) {
GKDialogController *presenter = [GKDialogController sharedDialogController];
presenter.parentWindow = myWindow;
[presenter presentViewController:viewController];
}
}];
I have found, however, that this is completely useless, and it presents the login dialog before it even calls the handler.

xCode ios How to write a mutable array to a file on the desktop?

I have a one-time need to create a file from an iOS mutable array. The array will be a short animated drawing.
That will be input to an app.
I write the array, and redraw it in the app, so I know that it's getting populated.
I've tried to do something as simple as this:
- (void) writeFile {
//CREATE FILE
NSLog(#"%s", __FUNCTION__);
[writeArray writeToURL:[NSURL fileURLWithPath:[#"~/Desktop/animation.data" stringByExpandingTildeInPath]] atomically:NO];
}
but I must be doing something wrong, as no file appears..
As the file is small (4-8K), maybe I should take a different approach?
Any help will be appreciated.
Of course this won't work on the device, but I assume you're just trying to work in the Simulator for some kind of testing. fileURLWithPath: doesn't expand ~. You need a full path here. None of the path searching routines is going to point into your user folder in any case, since that doesn't exist on iOS.

Weird Xcode behavior

Lately something weird has been happening to my projects in xcode: I've been trying to learn a lot of new stuff, and doing so by testing things out in different simple cocoa apps (written by me, from scratch). sometimes I will get a code that doesn't have any error messages, but when i run it, i will stop at some break-point. I then conclude that I have probably done something wrong, and restores the code back to the form it was before the error, but from then on out it is impossible to get the code to run. even if i restore the code to a state that i am 100 % sure that has worked before, it just stops at the same break-point. in order to fix this problem, i have to copy my code from the class this has happened to, delete the class, make a new one with the exact same name, paste the exact same code back in the class, and voila, it works again. what on earth is happening? my newest problem code goes like this:
-(IBAction)openFile:(id)sender {
NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
NSURL *fileURL;
[openPanel setCanChooseFiles:YES];
[openPanel setCanChooseDirectories:NO];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setAllowedFileTypes:[NSArray arrayWithObject:#"txt"]];
if ( [openPanel runModal] == NSOKButton ) {
fileURL = [openPanel URL];
}
[openPanel release];
}
I know this code has worked before. It is currently my only method, and it activates when i press open in the menu. If I delete everything inside the method, so that pressing open should do nothing it stops at a break-point inside the method anyway. I have had exactly the same kind of problem before with openGL codes, and with a method that used c syntax to do file reading. Does anybody know what kind of horrible mistake I'm making over and over again?
A Breakpoint is something you yourself set explicitly to tell Xcode to pause the program at this exact line. Superficially it might look like the program crashed, but in reality it's just waiting for you to tell it to go on.
This page looks like it has a nice explanation of the breakpoint interface in Xcode. (This is from a framework called Cocos2D, but ignore that. You should stick to ordinary Cocoa until you know what you're doing.)

Setting printer-specific options on an NSPrintOperation without a panel

this question has bothered me on and off for about a year, and I thought perhaps someone else would have experience with a similar situation.
Goal: on Mac OS X 10.6-7, to print multiple NSViews to EPSON Stylus Pro 4880 printers using a defined resolution and 'high speed' setting, without showing a print panel.
Current situation: I can create successful NSPrintOperations for each NSView, but if I do not show a print panel, it appears the printer's default resolution is used, which is far too high, and slow, for my needs.
Best solution I have so far: I have tried showing the print panel and defining a Mac OS 'preset' which has the correct print resolution and high speed settings already enabled. The downside here is that the Mac preset overrides the number of copies I have set via NSCopies, which is a problem. The other difficulty of course is having someone always around to press the 'OK' button a few thousand times a day.
Where I'm up to
When the NSPrintOperation runs its panel, it has to set the EPSON-specific printer settings somewhere, but I cannot find where it is saved. They don't appear to be set in [NSPrintInfo printSettings].
I have looked at the PPD for the printer, but I can't find the high speed setting anywhere, and the default resolution defined in the PPD is not actually used as the default when printing. It appears EPSON has their own driver settings which are not taken from the PPD I have, and I am not sure how to set them manually.
Basically, running the NSPrintOperation with a print panel and preset overrides all the settings, including the ones I don't want to override. Running it without the print panel leaves all the settings as default, which is not what I want. Can anyone point me in the right direction to find a solution in between those two?
After the NSPrintOperation's runOperation runs with the dialog, look in PMPrintSettings, the printer-specific parameters may be there. I suppose you could persist PMPrintSettings for the future somehow and load the via updateFromPMPrintSettings.
This is unfortunately the best solution I have found so far though I hate to call it 'best', or even a 'solution'. It comes back to this: run an operation with a panel, and then programmatically 'click' the Print button.
[op runOperationModalForWindow: self.window delegate: self didRunSelector: nil contextInfo: nil];
NSPanel *panel = (NSPanel*)self.window.attachedSheet;
for (NSView *view in ((NSView*)panel.contentView).subviews)
{
if (view.class == [NSButton class])
{
NSButton *button = (NSButton*)view;
if ([button.title isEqualToString: #"Print"])
[button performClick: self];
}
}
or
op.runOperationModalForWindow(window, delegate: nil, didRunSelector: nil, contextInfo: nil)
(window.attachedSheet?.contentView.subviews.filter({ $0 is NSButton }) as [NSButton]).filter({ $0.title == "Print" }).first?.performClick(self)
The downside obviously is a window is needed, while I was hoping to run this as a headless server application. I have tried working with Core Printing and PMPrinter/PMPrintSettings and so forth to no avail. The only thing I have not yet tried is talking to CUPS directly. Maybe I'll save that for a rainy day!

Resources