Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
Hello, I am very new to mac cocoa applications. I currently work on iOS programming. So i was interested in cocoa applications also. I followed few tutorials and got a core-data based cocoa application built. Its very simple. Add records to table view and removing them. Now i have started to compare .net application to cocoa application..is it right?i built C# applications in the past in my school. I used to get .exe files in those applications. I can use the .exe file on any of my PCs. I used archive to create an application to test now. So i saved it on my desktop. Now i open the application and work with it..add records, remove records. When i close the application, it asks me to save it in binary,sqlite or xml. How can i close the application to save it like a .exe file. Basically not to ask user to save..more questions coming. Thanks
 
Last edited:

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
The Binary, XML and sqlite are the data part of core-data. They have nothing to do with (.exe) equivalency.

When you build an (Cocoa) application bundle in MacOS you are creating a nested folder structure with the binary (no file extension) and any resources needed to run that application.

This would be equivalent to creating and installing an application in Windows that includes a .exe and resources like images that are used by that .exe in say C:\Program Files\
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@jared_kipe.Thanks. So is there a way i can build a cocoa application exactly like an .exe file with resources like you said. Like an application where i enter username, password. It connects to a web service. Validates those credentials and returns me true/false. I want to take that application and put it on my friends mac and he can put his credentials and check. But no saving option when we close the application. Very simple one. I must say again i am very new to cocoa.
 

Ap0ks

macrumors 6502
Aug 12, 2008
316
93
Cambridge, UK
@jared_kipe.Thanks. So is there a way i can build a cocoa application exactly like an .exe file with resources like you said. Like an application where i enter username, password. It connects to a web service. Validates those credentials and returns me true/false. I want to take that application and put it on my friends mac and he can put his credentials and check. But no saving option when we close the application. Very simple one. I must say again i am very new to cocoa.
Turn off core data and document-based application and you'll be able to make a simple program without asking you to save when exiting.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
@jared_kipe.Thanks. So is there a way i can build a cocoa application exactly like an .exe file with resources like you said. Like an application where i enter username, password. It connects to a web service. Validates those credentials and returns me true/false. I want to take that application and put it on my friends mac and he can put his credentials and check. But no saving option when we close the application. Very simple one. I must say again i am very new to cocoa.

You don't understand, every time you build a Cocoa application you are building the 'like .exe with resources like I said'.

What is happening is that you made an application like TextEdit. You go into it, mess around, and low and behold, the application wants to save your progress.

If you want an application without any of that, start over with a pure view based applications, throw some IBOutlet text fields up there with a button to process the results. When you build it you can take that, zip it up and put it on your friends computer no problem.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
If you want an application without any of that, start over with a pure view based applications, throw some IBOutlet text fields up there with a button to process the results. When you build it you can take that, zip it up and put it on your friends computer no problem
I followed like you said.Doing that will build me a iPad/iPhone application. I dont want an iOS application. A mac/cocoa app that one logs in and gets connected to a web service. A stand alone application.
Forgive me for this non-mac terminology.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@jared_kipe.I was also trying to do more on a core data cocoa app (TestApp) that i built. I made an archive->application and saved the file to my desktop. I had saved some data in that application. Saved it in binary info called "PlayerInfo". I launched the app on my desktop. I went to File->Open Recent and choose the binary info file "PlayerInfo". It open another instance of my app TestApp and showed the file info there. So coming back to one my earlier questions, when i double click and open the application can it retrieve the file info automatically?
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
I followed like you said.Doing that will build me a iPad/iPhone application. I dont want an iOS application. A mac/cocoa app that one logs in and gets connected to a web service. A stand alone application.
Forgive me for this non-mac terminology.

Sorry, I guess I just mean OSX->Cocoa Application, then don't check off 'Use Core Data'. This will give you a basic view based application.

----------

@jared_kipe.I was also trying to do more on a core data cocoa app (TestApp) that i built. I made an archive->application and saved the file to my desktop. I had saved some data in that application. Saved it in binary info called "PlayerInfo". I launched the app on my desktop. I went to File->Open Recent and choose the binary info file "PlayerInfo". It open another instance of my app TestApp and showed the file info there. So coming back to one my earlier questions, when i double click and open the application can it retrieve the file info automatically?

There are a lot of ways to answer this. You could use a NSUserDefaults to save all of your data. You could use NSUserDefaults to store the last file saved. You could use NSBundle to save/load your data automatically inside your application bundle...
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
Sorry, I guess I just mean OSX->Cocoa Application, then don't check off 'Use Core Data'. This will give you a basic view based application.
Don't check off? Should i or should i not check the checkmark.? So in one of my iOS app, i check login for an employee. Then save the employee entity in core data. Next time if my app is launched i bring the saved values and use those values to check login. I tried to that here in cocoa, but it was not possible. I used managedobjectcontext to save like i did in core data in iOS application

Code:
ITMLoginResult *loginResult = (ITMLoginResult *)[self.theParser.loginResultArray objectAtIndex:0];
		NSLog(@"the empid gotten is %@",loginResult.empID);
		NSLog(@"the secure hash is %@",loginResult.secureHash);
		
		NSError *fetchError = nil;
		NSFetchRequest *fR = [[NSFetchRequest alloc]init];
		fR.entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.moc];
		
		fR.predicate = [NSPredicate predicateWithFormat:@"employeeID == %@",loginResult.empID];
		NSArray *tmpArray = [self.moc executeFetchRequest:fR error:&fetchError];
		
		NSLog(@"the tmpArray count is %d",tmpArray.count);
		if (tmpArray.count == 1)
		{
			Employee *empl = [tmpArray objectAtIndex:0];
			NSLog(@"the employee exists %@",empl.employeeID);
			NSLog(@"the secure hash for him is %@",empl.empHash);
		}
		
		int empID = [loginResult.empID intValue];
 
		Employee *emp  = (Employee *)[NSEntityDescription insertNewObjectForEntityForName:@"Employee"																		inManagedObjectContext:self.moc];
		emp.employeeID = [NSNumber numberWithInt:empID];
		emp.empHash = loginResult.secureHash;
		
		[self.empArray addObject:emp];
		
		NSError *error;
		
		if (tmpArray.count ==0)
		{
			if(![self.moc save:&error])
			{
				NSLog(@"Failed to save the object");
			}
			else
			{
				NSLog(@"Save Successful");
			}
			
		}

So i parse an xml and save the values to an array "loginResultArray". I retrieve the same empID if it exists, but tmpArray is always 0. Then i save it using self.moc. I see the message "Save Successful".So whats happening to my data?
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
Don't check off? Should i or should i not check the checkmark.? So in one of my iOS app, i check login for an employee. Then save the employee entity in core data. Next time if my app is launched i bring the saved values and use those values to check login. I tried to that here in cocoa, but it was not possible. I used managedobjectcontext to save like i did in core data in iOS application

Code:
ITMLoginResult *loginResult = (ITMLoginResult *)[self.theParser.loginResultArray objectAtIndex:0];
		NSLog(@"the empid gotten is %@",loginResult.empID);
		NSLog(@"the secure hash is %@",loginResult.secureHash);
		
		NSError *fetchError = nil;
		NSFetchRequest *fR = [[NSFetchRequest alloc]init];
		fR.entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.moc];
		
		fR.predicate = [NSPredicate predicateWithFormat:@"employeeID == %@",loginResult.empID];
		NSArray *tmpArray = [self.moc executeFetchRequest:fR error:&fetchError];
		
		NSLog(@"the tmpArray count is %d",tmpArray.count);
		if (tmpArray.count == 1)
		{
			Employee *empl = [tmpArray objectAtIndex:0];
			NSLog(@"the employee exists %@",empl.employeeID);
			NSLog(@"the secure hash for him is %@",empl.empHash);
		}
		
		int empID = [loginResult.empID intValue];
 
		Employee *emp  = (Employee *)[NSEntityDescription insertNewObjectForEntityForName:@"Employee"																		inManagedObjectContext:self.moc];
		emp.employeeID = [NSNumber numberWithInt:empID];
		emp.empHash = loginResult.secureHash;
		
		[self.empArray addObject:emp];
		
		NSError *error;
		
		if (tmpArray.count ==0)
		{
			if(![self.moc save:&error])
			{
				NSLog(@"Failed to save the object");
			}
			else
			{
				NSLog(@"Save Successful");
			}
			
		}

So i parse an xml and save the values to an array "loginResultArray". I retrieve the same empID if it exists, but tmpArray is always 0. Then i save it using self.moc. I see the message "Save Successful".So whats happening to my data?

A lot about this doesn't look particularly right (and wouldn't compile in its current state). At this point, I honestly don't know if/why you want to use Core Data. How much data you are storing, and where/how it is being store in CD.

Submit full compilable code with comments for further help.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@jared_kipe. This compiles. I have button click event. On clicking it i connect to a web service. Pass my values. Check login. Return 2 values. empid and secure hash.

Code:
- (IBAction)btnLoginClick:(NSButton *)sender
{
	if (!self.appDelegate)
	{
		self.appDelegate = (ITMAppDelegate *)[[NSApplication sharedApplication] delegate];
	}
	self.moc = self.appDelegate.managedObjectContext;
	
	if (!self.moc)
	{
		NSLog(@"Empty moc");
	}
	else
	{
		NSLog(@"the MOC is not empty");
	}
	NSString *uname = [self.txtUserName stringValue];
	NSString *pwd = [self.txtPassword stringValue];
		
	NSString *path = @"http://abcdxyz.com/IOrderItems.asmx/";
	NSString *strPath = [NSString stringWithFormat:@"%@%@?strUserName=%@&strPassword=%@",path,@"LoginEmployee",uname,pwd];
	NSLog(@"the path is %@",strPath);
	NSURLRequest *url = [NSURLRequest requestWithURL:[NSURL URLWithString:strPath]];
	NSData *dataReturned = [NSURLConnection sendSynchronousRequest:url returningResponse:nil error:nil];
	NSXMLParser *xmlparser = [[NSXMLParser alloc]initWithData:dataReturned]; //newNSData

	
	NSString *requestPath = [[url URL] absoluteString];
	NSString *newString =[NSString stringWithUTF8String:[dataReturned bytes]];
	NSLog(@"the new string is %@",newString);
	
	self.theParser = [[ITMParser alloc] initParser];

	
	[xmlparser setDelegate:	self.theParser];
	
	BOOL worked = [xmlparser parse];
	if(worked)
	{
//..all the code that i pasted earlier
		
	}
	else
	{
		NSLog(@"IT did not work");
	}
}

Yes i want to use CD. Further down in the process of this application, i want to retrieve customer data from web service and store it in CD . Like a complete employee table with 5/6 fields. Customer table with 8/9 fields. So they could be 10000 records. So i will very much use CD in storing and fetching. This is a simple start up. Hope you get my point. Thanks.
 
Last edited:

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
@jared_kipe. This compiles. I have button click event. On clicking it i connect to a web service. Pass my values. Check login. Return 2 values. empid and secure hash.

Code:
- (IBAction)btnLoginClick:(NSButton *)sender
{
	if (!self.appDelegate)
	{
		self.appDelegate = (ITMAppDelegate *)[[NSApplication sharedApplication] delegate];
	}
	self.moc = self.appDelegate.managedObjectContext;
	
	if (!self.moc)
	{
		NSLog(@"Empty moc");
	}
	else
	{
		NSLog(@"the MOC is not empty");
	}
	NSString *uname = [self.txtUserName stringValue];
	NSString *pwd = [self.txtPassword stringValue];
		
	NSString *path = @"http://abcdxyz.com/IOrderItems.asmx/";
	NSString *strPath = [NSString stringWithFormat:@"%@%@?strUserName=%@&strPassword=%@",path,@"LoginEmployee",uname,pwd];
	NSLog(@"the path is %@",strPath);
	NSURLRequest *url = [NSURLRequest requestWithURL:[NSURL URLWithString:strPath]];
	NSData *dataReturned = [NSURLConnection sendSynchronousRequest:url returningResponse:nil error:nil];
	NSXMLParser *xmlparser = [[NSXMLParser alloc]initWithData:dataReturned]; //newNSData

	
	NSString *requestPath = [[url URL] absoluteString];
	NSString *newString =[NSString stringWithUTF8String:[dataReturned bytes]];
	NSLog(@"the new string is %@",newString);
	
	self.theParser = [[ITMParser alloc] initParser];

	
	[xmlparser setDelegate:	self.theParser];
	
	BOOL worked = [xmlparser parse];
	if(worked)
	{
//..all the code that i pasted earlier
		
	}
	else
	{
		NSLog(@"IT did not work");
	}
}

Yes i want to use CD. Further down in the process of this application, i want to retrieve customer data from web service and store it in CD . Like a complete employee table with 5/6 fields. Customer table with 8/9 fields. So they could be 10000 records. So i will very much use CD in storing and fetching. This is a simple start up. Hope you get my point. Thanks.

No it won't, because this line is not a complete statement.
Code:
Employee *emp  = (Employee *)[NSEntityDescription insertNewObjectForEntityForName:@"Employee"

If you want to continue using CD, then I already gave you a good solution. Use NSUserDefaults to store the path to where they saved their data. Then on launch of the application use it to load the data if that path is still valid.

This is how applications like iTunes and iPhoto know where to save/load their libraries. But if there isn't a library there it will prompt you to save a new one or load an old one.

Edit: Also sending the users password in plain text and as a url parameter is horribly insecure.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
Code:
Employee *emp  = (Employee *)[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.moc];
I dont know how the inManagedObjectContext got cut off here in my post..but in my code its like i wrote above.

If you want to continue using CD, then I already gave you a good solution. Use NSUserDefaults to store the path to where they saved their data. Then on launch of the application use it to load the data if that path is still valid.

Can you give me an example. I used NSUserDefaults to store empid,asmx paths and stuff like that in settings bundle and a plist. But the actual core data was in sqlite DB. So its a little confusing.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
Code:
Employee *emp  = (Employee *)[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.moc];
I dont know how the inManagedObjectContext got cut off here in my post..but in my code its like i wrote above.



Can you give me an example. I used NSUserDefaults to store empid,asmx paths and stuff like that in settings bundle and a plist. But the actual core data was in sqlite DB. So its a little confusing.

Giving you a full working example would be basically doing the whole thing for you.

Start small, hard code in the path to the file you already saved and get your application to load it automatically.

Then get your app to save the path to a key in your NSUserDefaults for the currently open data file when your application quits (or periodically).

Put both of them together and you can just automatically load your last used database.
 

ElectricSheep

macrumors 6502
Feb 18, 2004
498
3
Wilmington, DE
You should take a look at the StickiesWithCoreData sample project in XCode's Organizer. It contains sample code that demonstrates how to save your managed object context to a pre-determined locations.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@ElectricSheep. I couldnt find the example in my Developer tools. I looked up google also, but could not find the example.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.