How to handle mountain lion notifications clicks - objective-c

I just wanted to add some notifications to Gyazo app.
I finally able to send notifications to the notifications center http://korniltsev.ru/p/jz6m3Nm.png
however when i click on it and the app is not launched it launches in some strange way:
it shows empty window(even if i set it visibleAtLaunch to 0) and nothing happens;http://korniltsev.ru/p/jz6mvk0.png
the code i'm trying to use is here:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSUserNotification * clicked = [[aNotification userInfo]
objectForKey:NSApplicationLaunchUserNotificationKey];
if (clicked){
[NSApp terminate:self];
return;
}
...
I send notifications like this
NSUserNotificationCenter *manager = [NSUserNotificationCenter defaultUserNotificationCenter];
NSUserNotification *urlNotification = [[NSUserNotification alloc]init];
[urlNotification setTitle:appName];
[urlNotification setInformativeText:url];
[manager deliverNotification:urlNotification];
What am i doing wrong?

[NSApp terminate:self] has a lot of side effects. Are you sure these side effects are not creating your blank window? Have you tried calling [NSApp terminate:self] immediately in applicationDidFinishLaunching and making sure you application quits cleanly in every case? If it does not you will likely have to look at what you are doing in your application's document controller (if you have one) and applicationShouldTerminate.

Related

Display loading notification or gif during an operation in a Cocoa app (not cocoa touch)

I have an app in which I am uploading data from a device, while the device is being read I would like to display a little loading notification or overlay. Does Cocoa have something like this built in? If I'm not mistaken I believe cocoa touch used to. Basically on a button event that starts the device reading I would also like to fire off the overlay.
The code I would add to is below:
- (IBAction)uploadEvent:(id)sender
{
// start loading overlay
char *encodedBuffer = NULL;
HEM6310FDriver *driver = [[HEM6310FDriver alloc] init];
int getDataFromDevice = [driver getData:encodedBuffer user: 1];
if (getDataFromDevice == Success) {
int status = upload(driver.deviceData);
//remove loading overlay
}
else{
// do something else and remove overlay
}
The appropriate thing to do is to add an NSProgressIndicator to your view hierarchy and display and animate when the request begins.
If it is a determinate progress, use callbacks to update.
You should also setEnabled: NO on views and controls that need the data until progress is complete.
You might also look at the new NSProgress API
Create a NSPanel. It can be as simple as you want. Maybe just a label that says "Loading" and a NSProgressView in indeterminate mode. You can then attach it to the window like this:
[[NSApplication sharedApplication] beginSheet:loadingPanel
modalForWindow:self.window
modalDelegate:self
didEndSelector:nil
contextInfo:nil];
When you want to dismiss it, do this:
[[NSApplication sharedApplication] endSheet:loadingPanel returnCode:0];
[loadingPanel orderOut:nil];

iOS7 sending SMS from inside the app has a misaligned modal view

I'm writing an app where I need to allow the user sending SMS from within the app.
It works fine on iOS6 but not on iOS7.
When I'm trying to send a text message, the modal view appears but there is a strange gap between the "to" field and the list of possible contacts.
After selecting the first contact, the "to" field slides up and disappears and then I see past messages and my new message but with the same gap again.
I'm attaching two images showing the issue:
Here is the code I'm using:
if([MFMessageComposeViewController canSendText]) {
NSArray *recipents = nil;
NSString *message = #"Let's go";
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipents];
[messageController setBody:message];
[self presentModalViewController:messageController animated:YES];
}
Please tell me if know you know how to fix this issue.
Thanks!
Additional info:
This bug only happens in iOS 7.x not in in iOS 6.x
Also, if I use the MFMailComposeViewControllerDelegate to send emails, it works just fine (although they are both implemented by MessageUI.h)...
Ok, I figured it out.
The issue happens because I have a custom UINavigationController.
In my AppDelegate, I had the following code:
UIImage *navBarImage = [UIImage imageNamed:#"BarIos7.png"];
[[UINavigationBar appearance] setBackgroundImage:navBarImage forBarMetrics:UIBarMetricsDefault];
Once I removed it, everything started working fine.

iOS 6 local notifications fail when the phone is locked with app open

I have an app with basic alarm functionality. In my applicationWillResignActive: method I have it setup to create notifications to set off the alarm. This works pretty great, and I believe this is the proper way to do it (let me know if you think there is a better way).
Only in the specific situation, ONLY ON iOS 6, when the application is not "quit" (the home button is never pressed) but the user merely locks the phone or the phone auto locks, the notifications don't go off.
I have traced through the code, and the notifications are indeed being created and it worked perfectly in iOS 5.
Here is my code:
- (void)applicationWillResignActive:(UIApplication *)application
{
[UIApplication sharedApplication].idleTimerDisabled = NO;
[alarm setupForBackground];
if ([alarm isRunning]) {
[alarm stop];
}
}
Here is the notification creation method:
- (void)setupForBackground
{
UILocalNotification* alarmNotification = [[UILocalNotification alloc] init];
if (alarmNotification) {
alarmNotification.fireDate = alarmDate;
alarmNotification.timeZone = [NSTimeZone defaultTimeZone];
alarmNotification.repeatInterval = 0;
alarmNotification.soundName = #"NotificationSound.aif";
[[UIApplication sharedApplication] scheduleLocalNotification:alarmNotification];
}
}
I have been searching for an answer for a while, and I could not find anything stating something about notification changes. Thanks for any help.
I have a semi-solution. Apparently if you add an AlertBody to the notification, then it works.
My belief is that this is a bug in iOS 6. As I mentioned it worked in iOS 5, the documentation makes no mention of having such a requirement, and the notification does work without the AlertBody if the application is quit (the home button is pressed).
Still curious to see if my understanding is correct and if I should file a bug report with Apple.
Thoughts anybody?

IOS FacebooSDK 3.0 FBLoginVIew in modal viewController

I have modal view controller displayed on rightBarButtonItem click. I'm using FbLoginView in this controller as in sample ios-Facebook SDK 3.0 Error 5 When Posting Status Update.
But i'm unable to show modal view controller more than one time.
I tried to release FBLoginView on ViewDidUnload but it always crashes on second atempt to open modal view controller.
Got the same problem and deal with it for couple days already. And finally this is my solution:
if (!FBSession.activeSession.isOpen) {
theLoginView = [[FBLoginView alloc] init];
theLoginView.frame = CGRectOffset(theLoginView.frame,
([[UIScreen mainScreen] bounds].size.width-theLoginView.frame.size.width)/2,
([[UIScreen mainScreen] bounds].size.height-theLoginView.frame.size.height)/2 -50);
theLoginView.delegate = self;
[self.view addSubview:theLoginView];
[theLoginView sizeToFit];
}
//Only close the session when application is terminating, this will save the token information:
- (void)applicationWillTerminate:(UIApplication *)application {
[FBSession.activeSession close];
}
//And keep the FBSession within the app until the user want to logout:
[FBSession.activeSession closeAndClearTokenInformation];
Right now for me its working completely fine. Hope this help.
The FB SDK doesn't seem to like you creating more than one FBLoginView. Maybe you can if you properly terminate the session, but I found it easier just to create the LoginView once and keep it around.
I did this as follows:
1) in my .m modal view controller file, I created a static variable
static FBLoginView* loginView;
2) When loading the modal view controller in my viewDidLoad, instead of
FBLoginView *loginview = [[FBLoginView alloc] initWithPermissions:
[NSArray arrayWithObject:#"status_update"]];
loginview.frame = CGRectOffset(loginview.frame, 10, 10);
I added a check to find if its already initialized, like this:
if (!loginView) {
loginView = [[FBLoginView alloc] initWithPermissions:
[NSArray arrayWithObject:#"status_update"]];
loginView.frame = CGRectOffset(loginView.frame, 10, 10);
}
Beyond that, I just followed the example of FB's HelloFacebook project.
Not pretty code, but it seems to work.
I had the same problem. Try to add something like this:
if(!yourFBLoginView)
{
yourFBLoginView = [FBLoginView alloc] init...];
}
And/or do not forget to close your active session when you dismissing your modalViewController.
if ([[FBSession activeSession] isOpen])
{
[[FBSession activeSession] close];
}
I think the answer for me (a variant of what was said) was only that I needed to have:
[FBSession.activeSession closeAndClearTokenInformation];
in the:
(void)applicationWillTerminate:(UIApplication *)application
function. The problem specifically for me was, while I was testing...I was constantly terminating the app without actually logging out the user without ever destroying the FBSession, so that when I went back into the app to test what I had changed - my Facebook user was still logged in, and thus some of the conditionals were being incorrectly met. I think this is very important for anyone who is testing (and I'm actually thinking that you should have that line in there anyway) to make sure to clear the session every time the application terminates to avoid this problem...I can imagine a scenario where my app just crashed on somebody and now they are reopening it and they experience the crash because the session was never cleared.

Dismissing/Updating Local Notification Programmatically in iPhone sdk4

I am working on an app that runs in the background with the backgroundmode set to location. In the didUpdateToLocation: method, I want to generate local notification. I want the app to show the notification only when previous notification has been viewed. Another option is to show only the latest notification and dismiss all the previous notifications programmatically (i.e. without user interaction). Please guide me how is it possible?
Try this:
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0) {
[app cancelAllLocalNotifications];
}