URL scheme - Qt and mac - cocoa

I'm trying to implement a custom URL scheme for my application. I've added the necessary lines for my Info.plist. After calling the specified url (eg.: myapp://) the application launches.
If I want to handle the URL, I've found these steps:
#interface EventHandler : NSObject {
}
#end
#implementation EventHandler
- (id)init {
self = [super init];
if (self) {
NSLog(#"eventHandler::init");
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:#selector(applicationDidFinishLaunching:)
// name:NSApplicationWillFinishLaunchingNotification
name:NSApplicationDidFinishLaunchingNotification
object:nil];
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:#selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
}
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
NSLog(#"%#", url);
}
#end
The above code is working if the application is running, but if the URL gets called and the application was terminated, the event is not caught. I think this is because this: NSApplicationDidFinishLaunchingNotification.
Changing it to NSApplicationWillFinishLaunchingNotification causes that non events caught. Maybe Qt handles it before me, but I can't find a workaround for the problem.

I was also trying to get my Qt-based application handle a custom URL scheme on the Mac and went down the same path as the original poster. It turns out that Qt4 already supports URL events on the Mac, and there's no need to write Objective-C code to receive them. This is in fact the reason that you didn't receive any URL events when you set the event handler in response to NSApplicationWillFinishLaunchingNotification: Qt registers its own handler afterward.
When a URL with your custom scheme is triggered, your Qt application will receive a FileOpenEvent. Note that it is the QApplication instance which receives the event. You can catch it by making your application subclass QApplication or by installing an event filter on the standard QApplication. I opted for this second approach.
Here's the eventFilter method of my custom event filter class, FileOpenEventFilter. It just emits the signal urlOpened when the event contains a non-empty URL. It also saves the last opened URL in case my main window isn't completely initialized when the event arrives (which happens in my app when it's not already running when the custom URL is clicked.)
bool FileOpenEventFilter::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::FileOpen)
{
QFileOpenEvent* fileEvent = static_cast<QFileOpenEvent*>(event);
if (!fileEvent->url().isEmpty())
{
m_lastUrl = fileEvent->url().toString();
emit urlOpened(m_lastUrl);
}
else if (!fileEvent->file().isEmpty())
{
emit fileOpened(fileEvent->file());
}
return false;
}
else
{
// standard event processing
return QObject::eventFilter(obj, event);
}
}

I register my handler in my application delegate's applicationWillFinishLaunching: method, and I don't miss any events. You're probably initializing your EventHandler object too late to get that notification. If you want to keep it as a separate class, that's ok, but you should create your object and register it with NSAppleEventManager within the applicationWillFinishLaunching: method of your application delegate.

Related

Parse.com registering for push notification after user login

I know that to register Parse.com push notifications I have to set it all in appdelegate file. But I am wondering if it is possible to override channels and register to multiple channels after user login in a viewcontroller class.
You should be able to by doing something like the following.
if(![currentInstallation channels]) {
[currentInstallation setChannels:#[#"WHATEVER1", #" WHATEVER2"]];
NSLog(#"Set Channel");
} else {
[currentInstall addUniqueObject:#"objectone" forKey:#"channels"];
[currentInstall addUniqueObject:#"objecttwo" forKey:#"channels"];
} [currentInstall saveInBackgroundWithBlock:(BOOL succeeded, NSError *error) {
if(!error){
NSLog(#"subscribed user to both channels");
} else {
NSLog(#"error subscribing to both channels: %#", error);
}
}];
All you have to do is run a shared instance of UIApplication anywhere in your application and then call the registration methods. Like this:
let application = UIApplication.sharedApplication()
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
This will present the push notifications dialog asking if they want to allow notifications and then make the call back to the appropriate app delegate method of either didFailToRegisterForRemoteNotificationsWithError or didRegisterForRemoteNotificationsWithDeviceToken
If you are testing on a simulator and not on an actual device didFailToRegisterForRemoteNotificationsWithError will always be called.

adding a Core Data object from a segue

in getting familiar with core data i have found myself puzzled by the question of what to pass various view controllers (VCs) when trying to add data.
for example, in the CoreDataRecipes project that apple provides as an example (http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html) they use the following approach
when the user wants to add a recipe to the list of recipes presented in the master table view, and hits the Add button, the master table view controller (called RecipeListTableViewController) creates a new managed object (Recipe) as follows:
- (void)add:(id)sender {
// To add a new recipe, create a RecipeAddViewController. Present it as a modal view so that the user's focus is on the task of adding the recipe; wrap the controller in a navigation controller to provide a navigation bar for the Done and Save buttons (added by the RecipeAddViewController in its viewDidLoad method).
RecipeAddViewController *addController = [[RecipeAddViewController alloc] initWithNibName:#"RecipeAddView" bundle:nil];
addController.delegate = self;
Recipe *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:#"Recipe" inManagedObjectContext:self.managedObjectContext];
addController.recipe = newRecipe;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[addController release];
}
this newly created object (a Recipe) is passed to the RecipeAddViewController. the RecipeAddViewController has two methods, save and cancel, as follows:
- (void)save {
recipe.name = nameTextField.text;
NSError *error = nil;
if (![recipe.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self.delegate recipeAddViewController:self didAddRecipe:recipe];
}
- (void)cancel {
[recipe.managedObjectContext deleteObject:recipe];
NSError *error = nil;
if (![recipe.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self.delegate recipeAddViewController:self didAddRecipe:nil];
}
i am puzzled about this design approach. why should the RecipeListViewController create the object before we know if the user wants to actually enter a new recipe name and save the new object? why not pass the managedObjectContext to the addRecipeController, and wait until the user hits save to create the object and populate its fields with data? this avoids having to delete the new object if there is no new recipe to add after all. or why not just pass a recipe name (a string) back and forth between the RecipeListViewController and the RecipeAddController?
i'm asking because i am struggling to understand when to pass strings between segues, when to pass objects, and when to pass managedObjectContexts...
any guidance much appreciated, incl. any links to a discussion of the design philosophies at issue.
Your problem is that NSManagedObjects can't live without a context. So if you don't add a Recipe to a context you have to save all attributes of that recipe in "regular" instance variables. And when the user taps save you create a Recipe out of these instance variables.
This is not a huge problem for an AddViewController, but what viewController do you want to use to edit a recipe? You can probably reuse your AddViewController. But if you save all data as instance variables it gets a bit ugly because first you have to get all data from the Recipe, save it to instance variables, and when you are done you have to do the reverse.
That's why I usually use a different approach. I use an editing context for editing (or adding, which is basically just editing).
- (void)presentRecipeEditorForRecipe:(MBRecipe *)recipe {
NSManagedObjectContext *editingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
editingContext.parentContext = self.managedObjectContext;
MBRecipe *recipeForEditing;
if (recipe) {
// get same recipe inside of the editing context.
recipeForEditing = (MBRecipe *)[editingContext objectWithID:[recipe objectID]];
NSParameterAssert(recipeForEditing);
}
else {
// no recipe for editing. create new one
recipeForEditing = [MBRecipe insertInManagedObjectContext:editingContext];
}
// present editing view controller and set recipeForEditing and delegate
}
Pretty straight forward code. It creates a new children context which is used for editing. And gets a recipe for editing from that context.
You must not save the context in your EditViewController! Just set all desired attributes of Recipe, but leave the context alone.
After the user tapped "Cancel" or "Done" this delegate method is called. Which either saves the editingContext and our context or does nothing.
- (void)recipeEditViewController:(MBRecipeEditViewController *)editViewController didFinishWithSave:(BOOL)didSave {
NSManagedObjectContext *editingContext = editViewController.managedObjectContext;
if (didSave) {
NSError *error;
// save editingContext. this will put the changes into self.managedObjectContext
if (![editingContext save:&error]) {
NSLog(#"Couldn't save editing context %#", error);
abort();
}
// save again to save changes to disk
if (![self.managedObjectContext save:&error]) {
NSLog(#"Couldn't save parent context %#", error);
abort();
}
}
else {
// do nothing. the changes will disappear when the editingContext gets deallocated
}
[self dismissViewControllerAnimated:YES completion:nil];
// reload your UI in `viewWillAppear:`
}

Use FSEvents in sandboxed app

I'm trying to use FSEvents in my sandboxed app to monitor some directories. I implemented the following class:
#implementation SNTracker
- (id)initWithPaths:(NSArray *)paths {
self=[super init];
if (self) {
trackedPaths=paths;
CFTimeInterval latency=1.0;
FSEventStreamContext context={0,(__bridge void *)self,NULL,NULL,NULL};
FSEventStreamRef eeventStream=FSEventStreamCreate(kCFAllocatorDefault,&callback,&context,(__bridge CFArrayRef)trackedPaths,kFSEventStreamEventIdSinceNow,latency,kFSEventStreamCreateFlagUseCFTypes|kFSEventStreamCreateFlagWatchRoot|kFSEventStreamCreateFlagFileEvents);
FSEventStreamScheduleWithRunLoop(eeventStream,[[NSRunLoop mainRunLoop] getCFRunLoop],kCFRunLoopDefaultMode);
FSEventStreamStart(eeventStream);
}
return self;
}
static void callback(ConstFSEventStreamRef streamRef,void *clientCallBackInfo,size_t numEvents,void *eventPaths,const FSEventStreamEventFlags eventFlags[],const FSEventStreamEventId eventIds[]) {
NSLog(#"asd");
}
The problem is that "asd" never gets printed (i.e. the callback function is never called). When I disable "Enable App Sandboxing" in the Summary of my main target in Xcode, the callback gets called. Am I doing something wrong? The only entitlement I've given to the sandboxed app is read-write access to user selected files.
And the usere has selected the path you are trying to monitor via FSEvent? Since if he hasn't, you won't be allow to access it and thus also not monitor it. A path can only be monitored as long as you are allowed to access it.

Invoke a method from another class, without reinitializing it

I've a ViewController where I call a method from another class (TCP class), where I make a TCP connection to a server, that gives me a response. And I want to, when that TCP class, get's the response from the server, call another method from the ViewController.
Problems:
I'm a noob.
I'm initializing and allocating that first
Viewcontroller on the TCP, and all my vars are reseted (something
that I don't want).
So... What can I do to make it right? I just want to call a method from a different class, that is already allocated in memory.
Tks!
You could set up the ViewController as an observer to the TCP class. This is a link that explains an implementation of the observer pattern in Obj-C. (Very similar to what I use but in a nice write up.)
http://www.a-coding.com/2010/10/observer-pattern-in-objective-c.html
I usually like to separate the persistence layer from the interface as well. I use observers or KVO to notify my business logic and view controllers that something changed.
You can also send the information through the Notification Center that is provided if you prefer...
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
Basic Code Example:
#implementation ExampleViewController
//...
- (void)viewDidLoad
{
[super viewDidLoad:animated];
[TCPClass subscribeObserver:self];
}
- (void)viewDidUnload
{
[super viewDidUnload:animated];
[TCPClass unsubscribeObserver:self];
}
- (void)notifySuccess:(NSString*)input
{
//Do whatever I needed to do on success
}
//...
#end
#implementation TCPClass
//...
//Call this function when your TCP class gets its callback saying its done
- (void)notifySuccess:(NSString*)input
{
for( id<Observer> observer in [NSMutableArray arrayWithArray:observerList] )
{
[(NSObject*)observer performSelectorOnMainThread:#selector(notifySuccess:) withObject:input waitUntilDone:YES];
}
}
//maintain a list of classes that observe this one
- (void)subscribeObserver:(id<Observer>)input {
#synchronized(observerList)
{
if ([observerList indexOfObject:input] == NSNotFound) {
[observerList addObject:input];
}
}
}
- (void)unsubscribeObserver:(id<Observer>)input {
#synchronized(observerList)
{
[observerList removeObject:input];
}
}
//...
#end
//Observer.h
//all observers must inherit this interface
#protocol Observer
- (void)notifySuccess:(NSString*)input;
#end
Hope that helps!

Calling Cocoa IBAction from Carbon Code

I'm trying to call a Cocoa IBAction from Carbon code...
I've set up global keys using this tutorial.
The hot keys are working fine, but I need to fire an IBAction when the global key has been pressed.
I keep getting errors when I use
[self functionName]
How do I call the function?
I've read about passing the Cocoa controller to the carbon method. How would I do this? or what is the best way?
I assume you're calling [self functionName] in a Carbon Event handler callback. That's not an Objective-C method, so of course self is not defined.
When you install a Carbon Event handler, one of the parameters is a "user data" pointer. You can pass an Objective-C object pointer in this parameter, so that your event handler will get it, and you can say something like [(MyController*) inUserData functionName]. Of course, to make this work, your handler must be in an Objective-C or Objective-C++ source file.
you can pass one of these as your user data while keeping the program safe for c++ translations:
/* include the necessary C header, located in objc/ (objc/objc.h?) */
/* of course, definitions with objc messaging belong in your .mm file */
class t_ibaction_invocation {
/* you may want to retain d_target or d_optionalArgument, and release at destruction */
enum { RetainArguments = 0 };
public:
/* IBAction takes the form: [target action:optionalArgument]; */
t_ibaction_invocation(id target, SEL action, id optionalArgument) : d_target(target), d_action(action), d_optionalArgument(optionalArgument) {
assert(this->d_target);
if (RetainArguments) {
[this->d_target retain];
[this->d_optionalArgument retain];
}
}
~t_ibaction_invocation() {
if (RetainArguments) {
[this->d_target release], target = 0;
[this->d_optionalArgument release], optionalArgument = 0;
}
}
id performAction() {
if (this->d_target && this->d_action) {
return [this->d_target performSelector:this->d_action withObject:this->d_optionalArgument];
}
else {
assert(d_target && d_action);
return 0;
}
}
private:
id d_target;
SEL d_action;
id d_optionalArgument;
};

Resources