Tuesday, April 12, 2011

Read and Write Text Files on IPhone OS

/Method writes a string to a text file
-(void) writeToTextFile{
      //get the documents directory:
      NSArray *paths = NSSearchPathForDirectoriesInDomains
           (NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];

      //make a file name to write the data to using the documents directory:
      NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                                   documentsDirectory];
      //create content - four lines of text
      NSString *content = @"One\nTwo\nThree\nFour\nFive";
      //save content to the documents directory
      [content writeToFile:fileName 
                   atomically:NO 
                       encoding:NSStringEncodingConversionAllowLossy 
                          error:nil];

}
 
 
//Method retrieves content from documents directory and
//displays it in an alert
-(void) displayContent{
      //get the documents directory:
      NSArray *paths = NSSearchPathForDirectoriesInDomains
                      (NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];

      //make a file name to write the data to using the documents directory:
      NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                                   documentsDirectory];
      NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
                                                      usedEncoding:nil
                                                             error:nil];
         NSLog(@"content: %@" , content)
      [content release];

} 
Source:  
http://howtomakeiphoneapps.com/2009/06/reading-and-writing-text-files-in-iphone-os-3-0/

Friday, April 8, 2011

Nice article about launching other apps from an iphone application

warning: passing argument 1 of 'localizedStringForKey:value:table:' from incompatible pointer type

 NSString *paramVia    = NSLocalizedString("share_on_twitter_url_via", nil);

there was missing something, so add @ to "share_on_twitter_url_via" : @"share_on_twitter_url_via"

 NSString *paramVia    = NSLocalizedString(@"share_on_twitter_url_via", nil);

iphone call programmatically


 NSString* telephone = @"tel:";
 telephone = [telephone stringByAppendingString:@"45345678"]; // set phone number
 NSURL *url = [ [ NSURL alloc ] initWithString: telephone ];
 [ [ UIApplication sharedApplication ] openURL: url ];

source: http://www.experts-exchange.com/Apple/Hardware/iPhone/Q_24182155.html

Wednesday, April 6, 2011

Simple Custom loading alert view

UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(140, 100)];

NSString* messageTitleToUser = @"Favorites list";
 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:messageTitleToUser message:@"loading..." delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];

  [alertView addSubview:spinner];
  [spinner startAnimating];
  [alertView show];

....
...
...

//if you programmatically want to close the alert view
 [alertView dismissWithClickedButtonIndex:0 animated:YES];
// stop spinner
[spinner stopAnimating];


References:
http://stackoverflow.com/questions/593234/how-to-use-activity-indicator-view-on-iphone


Friday, April 1, 2011

Objective-c Getting Started ".objc_class_name_NSObject", referenced from

Following the http://www.otierney.net/objective-c.html#gettingstarted tutorial, I got the following error:

".objc_class_name_NSObject", referenced from:
.objc_class_name_Fraction in Fraction.o
"_objc_msgSend", referenced from:

It was caused because there was missing a link to a framework: "Foundation.framework".

So, you can add existing frameworks to you project in XCode4 following these steps:

  1. In the project navigator, select your project
  2. Double click your target
  3. Select the 'Build Phases' tab
  4. Open 'Link Binaries With Libraries' expander
  5. Click the '+' button
  6. Select your framework (Foundation.framework)
Done.

references:
http://forums.macnn.com/79/developer-center/391122/newby-trying-learn-obj-c-xcode/
http://stackoverflow.com/questions/3352664/how-to-add-existing-frameworks-in-xcode-4

Real Objective C Hello World

Finally found a real HelloWorld in Objective c:

http://cupsofcocoa.wordpress.com/2010/09/03/objective-c-lesson-1-hello-world/

After that, we can know continue with this Complete Tutorial:

http://www.otierney.net/objective-c.html