Local Notification When App is Downloaded - xcode

I want to have a local notification show up as soon as the application is downloaded off the app store and is opened. Thanks.

You can do that in your app delegate's didFinishLaunchingWithOptions. You need to do the following in this method:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
....
//Get the version number of the application using this technique: http://stackoverflow.com/questions/458632/how-can-my-iphone-app-detect-its-own-version-number
NSString version = [self appVersion];
//Because you only want to display the notification on first launch so have a flag in user defaults to track that. Also note that you need to include this in your registerDefaults and set to NO
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL alreadyDisplayedNotification = [defaults boolForKey:#"alreadyDisplayedNotificationOnStartForVersion"];
if ([version isEqualToString:#"VersionForWhichYouWantNotification"] && !alreadyDisplayedNotification) {
//Display Notification...
// Set the flag in user default to track that notification has been displayed
[defaults setBool:YES forKey:#"alreadyDisplayedNotificationOnStartForVersion"];
}
.....
}

Related

What API is there for selecting iTunes library location?

How does one programatically set the iTunes library location on macOS to custom locations using e.g. C / Obj-C or Swift API?
Alternatively, environmental settings, such as modifying plists, using the defaults CLI tool, or similar approaches, are also OK for me.
Ordinarily, selecting a custom iTunes library location is done by launching iTunes while holding down the option key. I need to be able to do this in e.g. a unit testing environment / programatically.
You may be able to set it via the prefs.
This is how I access it.
-(void)loadITunesPrefLibraryPath {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *userPref = [userDefaults persistentDomainForName:#"com.apple.iTunes"];
id dataBaseLoc = [userPref objectForKey:#"Database Location"];
NSLog(#"%s dataBaseLoc is:%#", __PRETTY_FUNCTION__, dataBaseLoc);
NSLog(#"%s dataBaseLoc class is:%#", __PRETTY_FUNCTION__, [dataBaseLoc class]);
NSData* dataBaseData = (NSData*)dataBaseLoc;
BOOL staleBook = NO;
NSError* bookError = nil;
NSURL* dataBaseURL = [NSURL URLByResolvingBookmarkData:dataBaseData options:NSURLBookmarkResolutionWithoutMounting relativeToURL:nil bookmarkDataIsStale:&staleBook error:&bookError];
self.libExtDBfile = dataBaseURL;
}
Once you get the userPrefs for iTunes.
And create a BookMarkData from URL.
You might be able to set it via
[userPref setObject:newDataBaseLoc forKey:#"Database Location"];
also see next answer for possible ITLibrary framework private API access

Launch OS X Application Programmatically

How do I programmatically open an OS X app (.app) that is contained within the app I am building?
The preferred way of doing this on OS X is through the NSWorkspace class, which provides a couple of methods to launch applications. One of them, launchApplicationAtURL:options:configuration:error: allows you to specify a file URL to the application to launch. In addition of not having sandbox problems like the system() and Apple Event solution, it also gives you an easy way to manipulate how the application should be launched, eg. you can specify environment variables to be passed to the application.
Following Code snippet is used to launch an app programmatically:
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
path = [path stringByAppendingString:#"/MyApp.app"]; // App Path
NSWorkspace *ws=[NSWorkspace sharedWorkspace];
NSURL* url = [NSURL fileURLWithPath:path isDirectory:NO];
[ws launchApplicationAtURL:url
options:NSWorkspaceLaunchWithoutActivation
configuration:nil
error:nil];
You could use an Apple Script.
NSDictionary* errorDict;
NSAppleEventDescriptor* returnDescriptor = NULL;
NSAppleScript* scriptObject;
scriptObject = [[NSAppleScript alloc] initWithSource:#"try\n
run application \"Macintosh HD:Applications:_Sandbox-AppleScript0.app\"\n
on error number -609 # 'Connection is invalid' error that is spuriously reported # simply ignore\n
end try"];
if (returnDescriptor != NULL) {
// successful execution
if (kAENullEvent != [returnDescriptor descriptorType]) {
// script returned an AppleScript result
if (cAEList == [returnDescriptor descriptorType]) {
// result is a list of other descriptors
}
else {
// coerce the result to the appropriate ObjC type
}
}
}

Best location to store config, plugins for my OS X application

I'm programming an OS X application and would like to know what is considered to be the best location to store application data like config-files and plugins for my program into.
The configs aren't in defaults format since I also deploy this application on Windows.
Usually in your app's subfolder within the Application Support directory is where stuff like is expected to be stored. Apple provides a nice function in their documentation for getting a standardized NSURL for your Application Support directory.
Extracted from their documentation:
- (NSURL*)applicationDirectory
{
NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;
// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0)
{
// Append the bundle ID to the URL for the
// Application Support directory
dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
// If the directory does not exist, this method creates it.
// This method is only available in OS X v10.7 and iOS 5.0 or later.
NSError* theError = nil;
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
attributes:nil error:&theError])
{
// Handle the error.
return nil;
}
}
return dirPath;
}
You can call the subpath within NSApplicationDirectory anything you want, but they recommend using your bundle identifier, as seen in the example above.

Mavericks, SandBoxed, but deployment target to 10.8 and above using EKEventStore and calendar access

I've written this code below just to get it to compile but this will not work, because I need to have a deployment target of 10.8.
what is going on is I need access to EKEventStore , so when someone downloads this app, it runs fine in 10.8, but someone downloading in 10.9 would get errors, because the app doesn't have Privacy permission to the calendar. Since it is being compiled for 10.8, it has no access to the method requestAccessToEntityType:EKEntityTypeEvent..
how would one go about doing this?
on a related note, how do you compile code for 10.9, other code for 10.8, and call those different parts depending on the environment it is in? remembering this is for the Mac App Store, and if that is the way to go, be illustrative, as if you are talking to someone who has no idea how to begin to do this, because I don't..
thanks.
//------------------check authorization of calendars--------------
#if (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1090) || (__IPHONE_OS_VERSION_MIN_REQUIRED)
if(!eventStore) eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) //.............put this back in...
{
if (granted)
{
NSLog(#"granted permission to eventstore!");
authorizedEventStore = YES;
authorizedCalendar();
}
else
{
NSLog(#"Not granted");
authorizedEventStore = NO;
notAuthorized();
}
}];
#else
NSLog(#"not able to request");
if(!eventStore) eventStore = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent];
authorizedEventStore = YES;
authorizedCalendar();
#endif
//------------------end check authorization of calendars--------------
To have an App that runs on several OS versions:
Set your Base SDK to the latest version of the OS you support, in your case 10.9
Set the Deployment Target to the earliest OS you want your code to launch on
For all calls that don't exist in earlier versions of the OS, you must test before you call, either by using respondsToSelector: (for methods) or testing against nil (for functions and statics). You can if you like do a check for the OS version, but it's more robust to check the specific call.
see also:
How to conditionally use a new Cocoa API and
How do I include calls to methods only present in one operating system version when compiling for multiple versions?
To request access on OS X use:
EKEventStore *eventStore = nil;
if ([EKEventStore respondsToSelector:#selector(authorizationStatusForEntityType:)]) {
// 10.9 style
eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
// your completion
}];
} else {
// 10.8 style
eventStore = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent ];
}

Xcode: getting badge value to add it to a label

I have an app that receives push notification.
I would like to add the badge with the right value near a button inside the application menu (do you remember the old Facebook app?).
I'm trying to get the badge value from the notification in the AppDelegate, save it in NSUserDefault to use it in other view controllers.
NSString * badgeValue = [[userInfo valueForKey:#"aps"] valueForKey:#"badge"];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:badgeValue forKey:#"badge"];
[defaults synchronize];
The problem is that if I try to put the value in a label the app crashes
'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance
Xcode makes me save badgeValue in a string but when I put the string in the label it says is not a proper string
if ([badgeValue isKindOfClass:[NSString class]]) {
NSLog(#"it is string !!!!!!!!!!!!!!!");
// treat it as a string object
} else {
NSLog(#"it is not a string !!!!!!!!!!!!!!!");
// treat it as a number object
}
By debugging I see that badgeValue is not a real string but __NSCFNumber and if I try to convert it in a string the return value is a long strange number.
I searched everywhere for a way to get the badge value of the notification but the only option I can think of is a php query...Any idea?
What do you mean by "Xcode makes me save badgeValue in a string"? Do you get an error or a compiler warning if you try to type badgeValue as an NSNumber? The documentation and your own logging tell you that the value of the key "badge" is an NSNumber.
NSNumber * badgeValue = [[userInfo valueForKey:#"aps"] valueForKey:#"badge"];
[label1 setIntValue:badgeNumber.intValue];
Does something like this not work?
Yes, Just yesterday I tried with
NSString *string = [NSString stringWithFormat:#"%d", [badgeValue intValue]];
And it works! Thanks for help

Resources