Friday, August 24, 2007

Need to make a correction

Okay, if you look backwards in the history of this blog, I've said before that when you use the '*' before the variable name, that tells the compiler to create it in memory. I was a little off by that. What that '*' means is simply reference the pointer in memory. Using this prevents the computer from lugging around a variable from method to method so it can be used. By referencing the pointer, you are essentially lugging around a simple memory address that the method will use to get the value of that variable.


This essentially is saying (Figuratively of course):
I have a car but it's in the garage. If you want to see my car, lets go out to the garage, but I don't personally carry my car with me at all times.


Just thought I would clarify this Objective-C/C++ thing.
Happy Coding :-)

Tuesday, August 14, 2007

Running an application as root in Objective-C

Okay, so if you look back into the past a bit on the Blog history, I was having problems with this type of thing. I had an application that needed to access files belonging to root. Anyway, after 2 1/2 hours reading documentation, I found this code snippet.

AuthorizationRef authorizationRef;

AuthorizationExecuteWithPrivileges(authorizationRef, "/Path/To/Application.app/Contents/MacOS/Application", kAuthorizationFlagDefaults, nil, nil);

Now, this will run any application as a root user so use it wisely. Just make sure, if you are trying to run a .app file, you need to continue into it's contents and run the actual application. That's represented above. Just replace both "Application" instances with your application's name.

After the call is made, the OS will launch the application specified with root user privileges. It will require that you authenticate as an admin before the application is launched.

Killing a process by name in Objective-C

Okay, so one thing that really frustrated me was that in objective-c, there wasn't a way to kill a process by name. Well, I found the way to do it. There is a command line application that you can use to kill a process by its name. To use this in Objective-C, you just need to use the following.

system("usr/bin/killall processName");

If you want to make this dynamic created using a NSString, just make sure you call the cString function on the NSString object.

Thursday, August 9, 2007

I need some help!

Okay. So, if you are a UNIX genius, you might be able to help me with this.

I have an application that uses the system() command to send a command to the terminal. I'm using sudo to allow the user to delete files that belong to root.

so an example will be something like this
system("sudo rm /file/to/delete.txt");

But, as soon as this runs, it asks in terminal for a password.

Here's the thing. I need it so that all authentication is done without the terminal. It all needs to be handled in the objective-c application.

Is there a way to pre-authenticate the user so that sudo will work without asking for a password in terminal? Is there another way to delete files that belong to root in objective-c? If you can answer this, please help me out. That would be great!

Saturday, August 4, 2007

What to do next!

So, I think I answered, for the most part, everyone's questions I've received. Now, what else is there that you are having problems with? (Objective-C problems, not life problems)

I'm thinking about placing a post up about Localization. Would anyone be interested in that? It's really hard to explain in the blog so it will be a download the source kind of thing. But, if you are working with a big company that does business around the world, then localization will be something you will have to deal with. 

I'm can't really think of anything else right now to create or talk about so if y'all have any questions about objective, make a comment to this post. I'll receive an email and respond to the comment ASAP.

Thursday, July 26, 2007

Saving data using NSDictionaries

Okay, I had a blog viewer inquire about this problem. They were wanting to save information about people in a single XML file but didn't know how to do it. 

If you want to save information about people, the easiest way to do that is by using nested dictionaries where a single root dictionary contains dictionaries with your data. 

When you store all your dictionaries in a root dictionary, you can just write the root dictionary to file and and you will get an XML file representation of you data. 

I have a demonstration app up on the server, you just can't find it from the first page. 

Here is the link to the dictionary example to demonstrate how you do this. 
maccoder.server.googlepages.com/DictionaryExample.zip
Make sure you build it in XCode first before you run it. The current built file doesn't reflect a change I made in code to keep it from locking up your computer. 

I hope this helps!
Happy coding! :-)

Monday, July 16, 2007

Authenticating Users in Objective-C

Okay, so here is another solution. Let say you are creating an application that makes changes to your computer. You only want administrators to have the right to use your application. How do you go about doing this you might ask... Well, it is actually really simple. Here is how you access the built in authentication framework in Mac OS X in Objective-C.

First, you need to import some libraries. In the header file, import the following files:
  • Security/Authorization.h
  • Security/AuthorizationTags.h
Make sure you use the angle brackets instead of quotations.

This will give you access to the built in authentication and some variables to use for basic authentication.

Second, you need to declare some variables, you need the following:
  • OSStatus
  • AuthorizationRef
  • AuthorizationItem
  • AuthorizationRights
  • AuthorizationFlags
This is how I declared them in the demo app on the demo server. 

OSStatus status;
AuthorizationRef authorizationRef;
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0);
AuthorizationRights rightSet = { 1, &right };
AuthorizationFlags flags = kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagDefaults;

Now, you have declared all the variables you will need. Notice that you don't have the '*' next to the variable name. This is because you are leaving the realm of Objective-C and going into C/C++ and Objective-C will try to create id variables for all the items with the '*'. This will cause the application to stop responding.

To check authorization, you need to do the two methods. 

First, call this method:

status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);

This calls the dialog to open if you aren't already authenticated. 
After this method call, call this

status = AuthorizationCopyRights(authorizationRef, &rightSet, kAuthorizationEmptyEnvironment, flags, NULL);

This will extract the actual authorization for the user entered and set it to the status variable.

Now, you do a check.

if (status == errAuthorizationSuccess){ 
//do something 
}

That's it! If the user is authenticated, then the method body will be executed, else, it is left alone. Now, in the demo app, I had problems getting the application to keep working after the user closed the dialog, or put in bad information. So, I just surrounded the code with a @try @catch block.

Happy coding! :-)