Showing posts with label IPhone. Show all posts
Showing posts with label IPhone. Show all posts

Wednesday, May 4, 2011

Tuesday, April 26, 2011

IPhone: Custom Table Cell

In order to make a custom table cell you have to handle the cell contentView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
   
    // Set up the cell...
    pinFreeNumberItem *cellValue = [tableData objectAtIndex:indexPath.row];
   
    UIView *myContentView = cell.contentView;
    UILabel* topLeftLabel = [[UILabel alloc] initWithFrame: CGRectMake(2, -6, 350, 30)];
    topLeftLabel.text = @"hi1";
   
    [myContentView addSubview:topLeftLabel];
   
    UILabel* bottomRightLabel = [[UILabel alloc] initWithFrame: CGRectMake(170, 24, 350, 15)];
    bottomRightLabel.text =@"hi2";
   
    [myContentView addSubview:bottomRightLabel];
   
    UILabel* bottomLeftLabel = [[UILabel alloc] initWithFrame: CGRectMake(2, 24, 150, 15)];
    bottomLeftLabel.text = @"hi3";
   
    [myContentView addSubview:bottomLeftLabel];
   
    return cell;
}

Resources:
http://stackoverflow.com/questions/5337340/custom-uitablecell
http://iphone.zcentric.com/2008/08/05/custom-uitableviewcell/

IPhone Table View Styles

Tuesday, April 19, 2011

Getting unique Id from ABPersonInstance

iterate a Dict in objective c


NSEnumerator *enumerator = [myDict keyEnumerator];
id key;
// extra parens to suppress warning about using = instead of ==
while((key = [enumerator nextObject]))
    NSLog(@"key=%@ value=%@", key, [myDict objectForKey:key]);
 
source:
http://stackoverflow.com/questions/1284429/is-there-a-way-to-iterate-over-an-dictionary 

request to send from iphone data and files to server using http post


-(NSMutableURLRequest *)requestWithURL:(NSURL *)url withBody:(NSMutableDictionary *)body  withUrlAndAttachedFile:(NSString *)fileAndUrl withFileName:(NSString *)fileName withFileFormat:(NSString *)fileFormat

{
    NSMutableURLRequest *request;
    NSString *requestBody;
       
    request = [[NSMutableURLRequest alloc] initWithURL:url
                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                       timeoutInterval:60];
   
    request= [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file_0\"; filename=\"%@.%@\"\r\n", fileName, fileFormat] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    NSData *postData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fileAndUrl]];
    [postbody appendData:[NSData dataWithData:postData]];
    [postbody appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
   
   
    // iterate body dict
    NSEnumerator *enumerator = [body keyEnumerator];
    id key;
    // extra parens to suppress warning about using = instead of ==
    while((key = [enumerator nextObject])){
        //NSLog(@"key=%@ value=%@", key, [body objectForKey:key]);
        [postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[body objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
   
   
    // close form
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];
   
   
    return request;
}
references:
http://stackoverflow.com/questions/4458112/mac-os-nsurlconnection-in-iteration

Monday, April 18, 2011

iphone error: expected '=', ',', ';', 'asm' or '__attribute__' before error

Noted that this error happened only for the NSString*, for e.g.,
  • extern double const kTimeout; // fine
  • extern NSString* const kImageType; // compile error
After the above analysis & little break, eventually the problem is resolved by adding the the following import to the .h - "Foundation/Foundation.h"

Source:http://stackoverflow.com/questions/990906/iphone-error-expected-asm-or-attribute-before-foo

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

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, March 25, 2011

printing addressBook multivalue identifier

ABMultiValueIdentifier identifier;

....
....
....


NSLog(@"identifier");
NSLog(@"%i" , identifier);

source: http://www.roseindia.net/tutorial/iphone/examples/nslog/nslogintegerexample.html

Assertion failed: (((ABCMultiValue *)multiValue)->flags.isMutable), function ABMultiValueAddValueAndLabel

I Had this code

ABMultiValueRef multiPhone = ABRecordCopyValue(person, kABPersonPhoneProperty);
// here y was getting the error: Assertion failed: (((ABCMultiValue *)multiValue)->flags.isMutable), //function ABMultiValueAddValueAndLabel
ABMultiValueAddValueAndLabel(multiPhone, @"1111111111", kABOtherLabel ,NULL);

It was because, in order to add some values to multiPhones var, this had to be mutable, it means to be ABMutableMultiValueRef type, so:

ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutableCopy(multiPhones);
ABMultiValueAddValueAndLabel(multiPhone, @"1111111111", kABOtherLabel ,NULL);

that's all.









Friday, March 18, 2011

create person ABRecordRef: adding a contact to address book



CFErrorRef error = NULL;

ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();


ABRecordRef newPerson = ABPersonCreate();

ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"John", &error);

ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Doe", &error);

Here is the whole guide, it's very complete:


Source: http://www.modelmetrics.com/tomgersic/iphone-programming-adding-a-contact-to-the-iphone-address-book/

Tuesday, March 15, 2011

getting value property from ABRecordRef - Extract Address Book Address Values Tutorial

I was searching for the way to get value properies from an ABRecordRef Object and I found out it was using ABRecordCopyValue:


NSString *name = (NSString *)ABRecordCopyValue(person
, kABPersonFirstNameProperty);
 
but the great thing I found was an Amazing Tutorial : 
Tutorial: Extract Address Book Address Values on iPhone OS 

Friday, February 25, 2011

Code Sign error: Provisioning profile can't be found

In XCode, in the "Groups & Files" pane, expand "Targets" and double-click on your app's target.

This will bring up the Info pane for your target. In the "Build" section, check the "code signing" section for any old profiles. I found that it was building to an old profile there.

Source: http://www.iphonedevsdk.com/forum/iphone-sdk-development/21137-updated-3-0-now-provisioning-profile-cant-found.html

Thursday, February 17, 2011

"_OBJC_CLASS_$_MFMailComposeViewController", referenced from: __objc_classrefs__DATA@0 in libQCLib.a(ShowEmailEditorVCO.o) ld: symbol(s) not found


If you get his error trying to build and run.... 

"_OBJC_CLASS_$_MFMailComposeViewController", referenced from:
      __objc_classrefs__DATA@0 in libQCLib.a(ShowEmailEditorVCO.o)
ld: symbol(s) not found

Just Try adding the MessageUI framework to your project. :P

Source: http://groups.google.com/group/quickconnectiPhone/browse_thread/thread/1b676c17fd09de03