-
iPhone Development
Posted on January 5th, 2010 No comments -
XCode and Static Libraries
Posted on November 6th, 2009 No commentsI was having trouble adding a static library dependency to my Universal project in XCode. Here are a few articles on the subject.
Code Sharing Via Static Libraries And Cross-Project References
-
Can’t login to mac from Vista
Posted on September 9th, 2009 No commentsFor sometime I’ve had problems connecting from my Vista laptop to my MacBook Pro. I’ve only been able to connect the other way. Finally a solution.
This thread recommends the following:
On the start menu in the search field type “gpedit.msc”, hit enter. This will open the group policy editor.
Go to “Computer Configruation” –> “Windows Settings” –> “Security Settings” –> “Local Policies” –> “Security Options”
In the pane on the right side of the screen, select “Network Security: LAN Manager Authentication level.” By default this read “Send NTLMv2 response only.”
Change “Send NTLMv2 respone only” to “Send LM & NTLM — use NTLMv2 session security if negotiated”restart the client
If you use a famillial vista try this. You must modify the BDR
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
DWORD named : LmCompatibilityLevel must set to 1 -
Implementing right-click context menu in Cocoa as a function call
Posted on October 23rd, 2008 No commentsI was porting some Windows code to Cocoa that used a synchronous call to TrackMenuPopup (TPM_RETURNCMD | TPM_NONOTIFY) to display a context menu and immediately return the menu item selected. I didn't want the menu to post a notification because that would require a more complicated architecture that could port between Mac and Windows. I posted the question titled "Is there an equivalent technique in Cocoa for the synchronous TrackPopupMenu in Windows?" to stackoverflow.com.
I couldn't decouple the NSMenu notification to an NSView so I came up with a solution using a dummy NSView. This way I could implement a popup menu as a function call that returns the selected value. Apparently popUpContextMenu is synchronous:
I ultimately came up with the following solution:
// Dummy View class used to receive Menu Events
@interface DVFBaseView : NSView
{
NSMenuItem* nsMenuItem;
}
- (void) OnMenuSelection:(id)sender;
- (NSMenuItem*)MenuItem;
@end
@implementation DVFBaseView
- (NSMenuItem*)MenuItem
{
return nsMenuItem;
}
- (void)OnMenuSelection:(id)sender
{
nsMenuItem = sender;
}
@end
// Calling Code:
void HandleRButtonDown (NSPoint pt)
{
NSRect graphicsRect; // contains an origin, width, height
graphicsRect = NSMakeRect(200, 200, 50, 100);
//—————————–
// Create Menu and Dummy View
//—————————–
nsMenu = [[[NSMenu alloc] initWithTitle:@"Contextual Menu"] autorelease];
nsView = [[[DVFBaseView alloc] initWithFrame:graphicsRect] autorelease];
NSMenuItem* item = [nsMenu addItemWithTitle:@"Menu Item# 1" action:@selector(OnMenuSelection:) keyEquivalent:@""];
[item setTag:ID_FIRST];
item = [nsMenu addItemWithTitle:@"Menu Item #2" action:@selector(OnMenuSelection:) keyEquivalent:@""];
[item setTag:ID_SECOND];
//———————————————————————————————
// Providing a valid windowNumber is key in getting the Menu to display in the proper location
//———————————————————————————————
int windowNumber = [(NSWindow*)myWindow windowNumber];
NSRect frame = [(NSWindow*)myWindow frame];
NSPoint wp = {pt.x, frame.size.height – pt.y}; // Origin in lower left
NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined
location:wp
modifierFlags:NSApplicationDefined
timestamp: (NSTimeInterval) 0
windowNumber: windowNumber
context: [NSGraphicsContext currentContext]
subtype:0
data1: 0
0; data2: 0];
[NSMenu popUpContextMenu:nsMenu withEvent:event forView:nsView];
NSMenuItem* MenuItem = [nsView MenuItem];
switch ([MenuItem tag])
{
case ID_FIRST: HandleFirstCommand(); break;
case ID_SECOND: HandleSecondCommand(); break;
}
} -
Creating Cocoa applications programatically (i.e. NIB-less)
Posted on May 21st, 2008 4 comments"Why on earth would you want to create a Cocoa application without using Interface builder?"
That's the basic reaction you get if you try to seek out help online in writing a Cocoa application from scratch.
There may not be a lot of valid reasons but if there is at least one reasonable answer, then the question should be moot.
Our applications at DVFilm are cross-platform. The user interfaces are very simple. The majority of the code is written in C/C++ and in the business logic. So I have not given up on the idea that the simple form-based UI can be written in such a way that the majority can be shared between Windows and Mac.
There, that's my "reasonable" explanation on why I am crazy enough to consider creating a Cocoa application programmatically. If that doesn't satisfy, here is another reason.
I have inherited a Carbon application (a QT template app, actually) and simply want to add a single dialog using existing Cocoa code.
If neither answer satisfies, I DON'T CARE. I do appreciate all the arguments for stopping my Tilting at Windmills but THEY don't satisfy ME. If you can't bring yourself to suspend your disbelief and offer help, I'll figure it out on my own.
It's not like it's NOT SUPPORTED. And apparently, creating applications in Cocoa programmatically might very be well be the norm for iPhone development.
-
Notifications in Cocoa
Posted on April 9th, 2008 No commentsNotifications seem unnecessarily confusing the way they are documented.
I want to know when my my window resizes so I notice in the documentation for NSWindow that there is a NSWindowDidResizeNotification. Great seems that’s what I want to write a handler for.
What do I do? The documentation does not describe what the handler function signature should be but I locate some sample code that uses:
– (void)windowDidResize:(NSNotification*)theNotification
so I add that to my NSWindow derived class for my main window but the method is not called.
I read up on Notifications and they talk about registering for notifications you are interested in but that seems unnecessary for this particular event, especially since I’ve subclassed the window. What is it in my background that expects that by simply subclassing the window that, that would be sufficient to capture the resize event? Is it my Windows background?
Anyway, I added
[self setDelegate:self]; // Set as delegate in order to receive notifications
in my MainWIndow awakeFromNib handler and my resize handler started getting called.
In another application I had to set the delegate again when adding the resize handler to the NSHandler object for a Window wasn’t sufficient.
"Delegates and Data Sources" in the Cocoa documentation sheds some light on this.
-
Porting C++ code to Objective-C
Posted on March 20th, 2008 1 commentI’m in the middle of porting a Windows C++ application to the mac. I’ve posted before that I was pleasantly pleased to find that with some limitations, XCode supports mixing C++ and objective-C code.
Here are a few things to look out for when working with C++ code in a Cocoa application:
1. Missing C++ member functions may not be flagged at compile time and your program will abort with a message similar to:
ZeroLink: unknown symbol ‘__ZN8DVFArray6ExistsEP6DVFObj’
You can use the C++filt program which is part of the GNU Binary Utilities to help demangle the function in error. I could not build this package so I looked and found a copy of the C++filt program. Unfortunately I could only find a copy for the PC.
The example listed demangles to: DVFArray::Exists(DVFObj*)2. You cannot store C++ objects/pointers in Cocoa containers. One workaround is to store NSValue objects in the containers and store C++ pointeres in the NSValue objects:
[myNSArray addObject:[NSValue valueWithPointer:pCPPObj]];
.
.
pCPPObj = [[_array objectAtIndex:idx] pointerValue]3. You should not use static C++ objects or static data members. Constructors for static objects will be called before main() is called and you will not have an NSAutoReleasePool initialized yet. You will get a message similar to:
*** _NSAutoreleaseNoPool(): Object 0×307f60 of class NSConcreteValue autoreleased with no pool in place – just leaking
The discussion "NSAutoreleasePool and static data member constructors" is very good on this subject
-
EXC_BAD_ACCESS during startup
Posted on March 4th, 2008 No commentsOnce again, the XCODE debugger not only failed to help but got in the way by showing me a stack trace that was not directly relevant to my problem. The program crashed with EXC_BAD_ACCESS and the stack trace looked like this:
#0 0×90a594c7 in objc_msgSend
#1 0xbffff7b8 in ??
#2 0×932899d8 in loadNib
#3 0×932893d9 in +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:]
#4 0×9328903a in +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:]
#5 0×93288f7c in +[NSBundle(NSNibLoading) loadNibNamed:owner:]
#6 0×93288cc3 in NSApplicationMain
#7 0×00009f80 in main at main.mm:17This made me think that my NIB was corrupt since I didn’t see anything in the stack trace that pointed to my code. After wasting several hours investigating that rat hole, I decided to try to do what the debugger didn’t help with and that’s track down the offending line. With breakpoints and Debugger(); calls (breakpoints don’t always work for me) I found that an IBOutlet was not being initialized and I was sending a message to nil. If the run-time was not going to flag this as an error, why would this cause a problem down the line? Arrggg!
Lesson learned, if a stack trace looks similar to the above, then look at your initialization code of your Nib objects.
-
Memory bugs in Cocoa
Posted on March 4th, 2008 No commentsAt work, our software runs on Windows and Mac OS. I’m trying to reuse as much code as possible; not through some fancy cross-platform tools but by simply writing in portable C or C++ code and by using class wrappers that can each be implemented in the native platform. One thing I’ve been pretty pleased with XCode is it’s support of mixing Objective-C and C++ code fairly seamlessly.
Recently, in trying to port some C++ Windows code over I started having some memory crashes. Unfortunately the XCode debugger was of very little help with the stack trace not including any calls from my code. This was an excellent thread on some techniques to troubleshoot EXC_BAD_ACCESS problems. Fortunately, I didn’t have to try all the various techniques listed. After reading the entire thread I decided to pursue the notion that I might be over-releasing an object (though why should this be a problem? A warning perhaps but seems once a reference counter is down to 0 calling release again should be harmless). This DID lead me to the offending code so I was able to fix my problem without wasting too much time. Once again, the developer community saved me hours if not days of debugging. How did we get along as programmers before the Web?
-
Linking up Help in Cocoa
Posted on February 7th, 2008 No commentsI haven’t had to "hook-up" help in a Cocoa appplication yet since the application I inherited at work already had it working. But recently, when the application invoked help I’ve been getting the message:
"Help isn’t available for <application name>"
I found this article to help me understand how help is hooked up in Cocoa.
I checked the required metatag and the info.plist values and they seemed in order. What worked for me is to remove the Help Book Folder from the resources folder and re-insert it. Be sure to select the radio button titled "Create Folder References for any added folders"
The one thing that is different in my Xcode 2.4.1 version project from the article was that the Resources folder exists inside a Frameworks folder.


