Thursday, July 15, 2010
Linkedin Themes
just a little logic and theme module is made.....
we can use the same for all over the project
just using uipicker make one view that just shows this color schemes
and take its value
and pass it to backcolor of below tiles .... simple
remember prepare images as vector images and only white.... with transpeent bkg......
Enjoy!
New Report style era of iphone
How to access camera or gallery of device iphone/ipod
I've been working on a number of iPhone camera applications. The default photograph functionality seemed kind of awkward to me, and I wanted to have a much simpler process and interface. This is an example of an Xcode project which shows how to load the camera interface with a custom overlaid image, and then wait for a tap anywhere on the screen to take a photo.
To start, I created a new project (a View-based application) called CameraTapper.
I knew I would need to subclass UIImagePickerController, but there's really not much I needed to over-ride. I created a new file (with UIView as a base) called CustomCamera.m (and .h) and then modified them to support the "tap anywhere to capture" functionality:
CustomCamera.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CustomCamera : UIImagePickerController {
}
@end
CustomCamera.m
#import "CustomCamera.h"
@implementation CustomCamera
-(void) dealloc {
[super dealloc];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// override the touches ended method
// so tapping the screen will take a picture
[self takePicture];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
-(void) viewDidAppear: (BOOL)animated {
[super viewDidAppear:animated];
}
@end
I also added an overlay to the project (a 24 bit transparent png):
This overlay would give the user the visual clue of what to do in order to progress to the next step.
The rest of the code is simply there to demonstrate how this could be implemented in another project. I made a simple button within the CameraTapperViewController.m file to load the CustomCamera view, and I added functionality to show how the "taken" photograph could be used after it had been taken -- by adding it to the background of the main view itself.
CameraTapperViewController.h
#import
@interface CameraTapperViewController : UIViewController
UIImageView *myImageView;
UIImage *myImage;
}
@end
CameraTapperViewController.m
#import "CameraTapperViewController.h"
#import "CustomCamera.h"
@implementation CameraTapperViewController
- (void) launchCamera {
// Set up the camera
CustomCamera *cameraController = [[CustomCamera alloc] init];
cameraController.sourceType =
UIImagePickerControllerSourceTypeCamera;
cameraController.delegate = self;
cameraController.showsCameraControls = NO;
cameraController.navigationBarHidden = YES;
cameraController.toolbarHidden = YES;
// overlay on top of camera lens view
UIImageView *cameraOverlayView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"camera_overlay.png"]];
cameraOverlayView.alpha = 0.0f;
cameraController.cameraOverlayView = cameraOverlayView;
// animate the fade in after the shutter opens
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:2.2f];
cameraOverlayView.alpha = 1.0f;
[UIView commitAnimations];
[cameraOverlayView release];
[self presentModalViewController:cameraController animated:YES];
}
// User took an image
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)inImage
editingInfo:(NSDictionary *)editingInfo {
myImage = inImage;
myImageView.image = myImage;
// Get rid of the picker interface
[[picker parentViewController]
dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)viewDidLoad {
// display the background image view
myImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:myImageView];
// display the button to launch the camera
UIButton *btnLaunchCamera = [UIButton
buttonWithType
:UIButtonTypeRoundedRect];
btnLaunchCamera.frame = CGRectMake(60, 220, 200, 40);
[btnLaunchCamera setTitle:@"Launch Camera" forState:
UIControlStateNormal];
[btnLaunchCamera addTarget:self action:@selector(launchCamera) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnLaunchCamera];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[myImageView release];
[super dealloc];
}
@end
For further exploration, I'd like to start playing with taking a real-time (or near real-time) feed of images and manipulating them -- similar to some of the augmented reality applications that are currently being developed...
How to make settings in iphone
Step 1: Create a Sample Application
Step 2: Create a Settings Bundle
Create a new File Command + N and then select Settings from the side menu and Settings Bundle. The file must be named Settings.bundle to work correctly.
This will create two new files Root.strings and Root.plist. Open the Root.plist file and you should see the following screen listing some sample settings. This is actually a graphical representation of an XML file which defines the settings and how they are displayed. (You can see the xml code by right clicking the file and selecting Open as Plain Text)
Step 3: Define Settings Schema
The Root element has three child elements (1) Title; which defines the title of this settings view and is set to the name of the application. (2) StringTable; which is not covered in this tutorial. (3) PreferenceSpecifiers which contains all the settings we wish to elicit from the user. PreferenceSpecifiers has type Array and has a child element of type Dictionary for each setting: Item 1, item2 and so forth. We can add items by selecting PreferenceSpecifiers and clicking the Grey button at the end of the row.
Each setting has several properties of type string including the common required properties; Title and Type (Note all strings are case sensitive). The title defines the label displayed beside the control used to modify the setting and type defines the type of control used. There are seven possible controls which can be selected using the Type property:
PSGroupSpecifier
This is the simplest control and has only the two properties defined described above. It is used as a separator to break-up groups of similar settings. It cannot accept any input from the user.PSTitleValueSpecifier
This displays a "Read Only" setting it cannot accept input from the user but can be used to display settings changed elsewhere.Key:
DefaultValue:
Another String property which can store any text, this defines the Text that will be displayed in the specifier.PSTextFieldSpecifier
The next type of control is the TextField, it has some additional properties which are listed belowKey:
DefaultValue:
Another String property which can store any text this defines the initial Text that will be entered into the TextField when it is first loaded.IsSecure
This is a boolean property which defines if the charecters of the text field will be hidden, set to enabled for password fields.KeyboardType
A String property which can have one of the following values: Alphabet, NumbersAndPunctuati on, NumberPad, URL, EmailAddress. This property specifies which type of keyboard to display when text field is selectedAutocapitalizationType
A String property which can have one of the following values: None, Sentences, Words, AllCharactersAutoCorrectionType
A String property which can have one of the following values: Default, No, YesPSSliderSpecifier
This displays a slider on a bar which allows the user to select a number in a specified range. In addition to the standard Title and Type specifiers the Slider has the following properties.Key:
MinimumValue
A property storing a numerical value which corresponds to the slider being on the left end of the bar.MaximumValue
A property storing a numerical value which corresponds to the slider being on the right end of the bar.DefaultValue:
A numerical property which defines the original position of the slider.PSToggleSwitchSpecifier
Displays a toggle switch which can be either On or Off.
A boolean property which defines the value of the control of the toggle button is in the Off position.
DefaultValue:
A boolean property which defines the position of the Toggle button the first time it is loaded.PSMultiValueSpecifier
Multi-Value Specifier |
Key:
This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control.Values:
A property of type Array which stores sub-elements of type String, each element provides a a pos sible value for the control to take.Titles:
A property of type array which stores sub-elements of type String, each element provides a textual representation of one of the values specified in the previous property. For example if the first element of the values array is "01" and the first element of the titles array is "January" then the user is shown January but if January is selected the value stored for the control is 01.DefaultValue:
Another String property that defines the initial value of the control, should be one of the elements of the Values list.PSChildPaneSpecifier
Key:
This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control.File:
The name of another plist file without the extension.Step 4: Retrieving Values of Settings
You can retrieve the value of a setting by using the command given below; Replace the hilighted text with the name of the setting you want to get the value of.NSString* settingValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"<Setting Key>"]
Step 5: Loading a Child Pane
The final of the Seven Controls allows you to add a sub view. The procedure to do so is to add a new file Command + N and select Other from the menu on the right Property List from the list of fle types.
Create a child prefernces schema exactly as you created the original schema.
No in the original schema add a PSChildPaneSpecifier and add a File item to the specifier with the file name of the child view (less the extension).
Technically this should be enough but as you will see if you run your code now the child view will not load. To load your child view the child plist file has to be inside the settings bundle but I can't find a way of doing this from within XCode (after half an our of random tinkering). So just open a terminal and move the file into the bundle manually. To do this browse to the directory containing your xcode project, and find child.plist file (or whatever you named it). Use mv child.plist Settings.bundle/child.plist to move your file and then click Build and Go in XCode.
If there is some way of moving files into the bundle through XCode that you know of please leave a coment and I will update this document.
Make db layer apart from code
we should try to make datacotroller deferent than our codes
make a data controller
@interface DataController : NSObject {
NSMutableArray *list;
NSMutableArray *namelist;
NSMutableArray *mobiles;
NSMutableData *tdata;
NSMutableArray *ids;
NSMutableArray *sgns;
NSMutableArray *Emails;
NSMutableArray *Fexs;
NSMutableArray *Birthdays;
NSMutableArray *Annidays;
NSString *searchfilter;
}
- (unsigned)countOfList;
-(NSMutableArray *) GetList;
-(NSMutableArray *) GetIds;
-(NSMutableArray *) Getsgns;
-(NSMutableArray *) Getmobiles;
-(NSMutableArray *) GetEmails;
-(NSMutableArray *) GetFexs;
-(NSMutableArray *) GetBirthdays;
-(NSMutableArray *) GetAnniversarydays;
-(void) SetFilter:(NSString *)search;
-(NSString *)searchfilter;
- (void)createDemoData:(NSString *)search;
@end
and implement it as data layer
#import "DataController.h"
#import "DataSet.h"
#import "WebServiceHelper.h"
@interface DataController ()
@property (nonatomic, copy, readwrite) NSMutableArray *list;
@property (nonatomic, copy, readwrite) NSMutableArray *namelist;
@property (nonatomic, copy, readwrite) NSMutableArray *ids;
@property (nonatomic, copy, readwrite) NSMutableArray *sgns;
@property (nonatomic, copy, readwrite) NSMutableArray *Emails;
@property (nonatomic, copy, readwrite) NSMutableArray *Fexs;
@property (nonatomic, copy, readwrite) NSMutableArray *mobiles;
@property (nonatomic, copy, readwrite) NSMutableArray *Birthdays;
@property (nonatomic, copy, readwrite) NSMutableArray *Annidays;
@property (nonatomic, copy, readwrite) NSString *searchfilter;
- (void)createDemoData:(NSString *)search;
@end
@implementation DataController
@synthesize list;
@synthesize namelist;
@synthesize ids,Annidays;
@synthesize sgns,mobiles,Emails,Fexs,Birthdays;
@synthesize searchfilter;
- (id)init {
if (self = [super init]) {
searchfilter = [[NSString alloc] init];
}
return self;
}
- (void)setList:(NSMutableArray *)newList {
if (list != newList) {
[list release];
list = [newList mutableCopy];
}
}
- (unsigned)countOfList {
return [list count];
}
-(void)SetFilter:(NSString *)search
{
searchfilter=search;
[self createDemoData:searchfilter];
}
- (void)dealloc {
[searchfilter release];
[ids release];
[namelist release];
[list release];
[super dealloc];
}
- (void)createDemoData:(NSString *)search
{
WebServiceHelper *DataCon = [[WebServiceHelper alloc] init];
DataCon.XMLNameSpace = @"http://tempuri.org/";
DataCon.XMLURLAddress = @"http://urdomain.com/webservicefolder/service1.asmx";
DataCon.MethodName = @"MyMethodName";
DataCon.MethodParameters = [[NSMutableDictionary alloc] init];
[DataCon.MethodParameters setObject:[[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"] forKey:@"UserName"];
[DataCon.MethodParameters setObject:[[NSUserDefaults standardUserDefaults] stringForKey:@"password_preference"] forKey:@"Password"];
[DataCon.MethodParameters setObject:search forKey:@"GroupId"];
tdata = [DataCon initiateConnection];
DataSet *dsMyDataSet = [[DataSet alloc] initWithXMLData:tdata];
NSMutableArray *Name = [[NSMutableDictionary alloc] init];
NSMutableArray *ID = [[NSMutableDictionary alloc] init];
NSMutableArray *SGN = [[NSMutableDictionary alloc]init];
NSMutableArray *mob = [[NSMutableDictionary alloc]init];
NSMutableArray *mobarr = [[NSMutableArray alloc] init];
NSMutableArray *em = [[NSMutableDictionary alloc]init];
NSMutableArray *emarr = [[NSMutableArray alloc]init];
NSMutableArray *fe = [[NSMutableDictionary alloc]init];
NSMutableArray *fearr = [[NSMutableArray alloc]init];
NSMutableArray *bd = [[NSMutableDictionary alloc]init];
NSMutableArray *bdarr = [[NSMutableArray alloc]init];
NSMutableArray *an = [[NSMutableDictionary alloc]init];
NSMutableArray *anarr = [[NSMutableArray alloc]init];
ID = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"Id"];
Name = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"DisplayName"];
SGN = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"SubGroupName"];
mob = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"DisplayPhone"];
em = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"DisplayEmail"];
fe = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"DisplayFax"];
bd = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"Birthday"];
an = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"Anniversary"];
NSEnumerator *mobIterator= [mob objectEnumerator];
NSString *mobnm;
while(mobnm = [mobIterator nextObject])
{
if([mobnm isEqualToString:@"@#$%"])
{
[mobarr addObject:@""];
}
else
{
[mobarr addObject:mobnm];
}
}
NSEnumerator *emIterator= [em objectEnumerator];
NSString *emnm;
while(emnm = [emIterator nextObject])
{
if([emnm isEqualToString:@"@#$%"])
{
[emarr addObject:@""];
}
else
{
[emarr addObject:[NSString stringWithString:emnm]];
}
}
NSEnumerator *feIterator= [fe objectEnumerator];
NSString *fenm;
while(fenm = [feIterator nextObject])
{
if([fenm isEqualToString:@"@#$%"])
{
[fearr addObject:@""];
}
else
{
[fearr addObject:fenm];
}
}
NSEnumerator *dateIterator= [bd objectEnumerator];
NSString *datestring;
while(datestring = [dateIterator nextObject])
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss-'06:00'"];
NSDate *date = [dateFormatter dateFromString:datestring];
[dateFormatter setDateFormat:@"dd MMM "];
if(date==nil)
{
}
else {
[bdarr addObject:[dateFormatter stringFromDate:date]];
}
}
NSEnumerator *anIterator= [an objectEnumerator];
NSString *anstring;
while(anstring = [anIterator nextObject])
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss-'06:00'"];
NSDate *date = [dateFormatter dateFromString:anstring];
[dateFormatter setDateFormat:@"dd MMM "];
if(date==nil)
{
}
else {
[anarr addObject:[dateFormatter stringFromDate:date]];
}
}
self.namelist = Name;
self.list = playList;
self.ids = ID;
self.sgns = SGN;
self.mobiles = mobarr;
self.Emails = emarr;
self.Fexs = fearr;
self.Birthdays = bdarr;
self.Annidays = anarr;
[an release];
[anarr release];
[SGN release];
[ID release];
[Name release];
[emarr release];
[em release];
[mob release];
[mobarr release];
[fearr release];
[fe release];
[bd release];
[bdarr release];
}
-(NSMutableArray *) GetList
{
return self.namelist;
}
-(NSMutableArray *) GetIds
{
return self.ids;
}
-(NSMutableArray *) Getsgns
{
return self.sgns;
}
-(NSMutableArray *) Getmobiles
{
return self.mobiles;
}
-(NSMutableArray *) GetEmails
{
return self.Emails;
}
-(NSMutableArray *) GetFexs
{
return self.Fexs;
}
-(NSMutableArray *) GetBirthdays
{
return self.Birthdays;
}
-(NSMutableArray *) GetAnniversarydays
{
return self.Annidays;
}
@end
now you are following good structure in xcode ...
Enjoy!!
how to read and write to plist
- (void)readPlist
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Contacts.plist"];
listOfItems = [[NSMutableArray alloc] initWithContentsOfFile:path];
}
- (void)writeToPlist
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Contacts.plist"];
[listOfItems writeToFile:path atomically:YES];
}
search functions
- (void) searchbarTextDidBeginEditing:(UISearchBar *)theSearchBar {
searchbar.showsCancelButton = YES;
if(ovController == nil)
ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]];
CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.backgroundColor = [UIColor grayColor];
ovController.view.alpha = 0.5;
ovController.rvController = self;
[self.tableview insertSubview:ovController.view aboveSubview:self.parentViewController.view];
searching = YES;
letUserSelectRow = NO;
self.tableview.scrollEnabled = NO;
}
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
[copyListofItems removeAllObjects];
if([searchText length] > 0)
{
[ovController.view removeFromSuperview];
searching = YES;
letUserSelectRow = YES;
self.tableview.scrollEnabled = YES;
[self searchTableView];
}
else
{
[self.tableview insertSubview:ovController.view aboveSubview:self.parentViewController.view];
searching = NO;
letUserSelectRow = NO;
self.tableview.scrollEnabled = NO;
}
}
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
}
- (void) searchBarCancelButtonClicked:(UISearchBar *)theSearchBar
{
[self doneSearching_Clicked:theSearchBar];
}
- (void) searchTableView
{
NSString *searchText = self.searchbar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
NSEnumerator *userIterator= [usernames objectEnumerator];
NSString *username;
while(username = [userIterator nextObject])
{
[searchArray addObject:username];
}
//NSPredicate *searchpredicate = [NSPredicate predicateWithFormat:@"SELF contains %@",searchText];
//[searchArray filterUsingPredicate:searchpredicate];
for (NSString *sTemp in searchArray)
{
if([[sTemp componentsSeparatedByString:@" "] count ]>1)
{
NSArray *split = [[NSArray alloc] initWithArray:[sTemp componentsSeparatedByString:@" "]];
if ([split count]>0)
{
NSRange titleResultsRange = [[split objectAtIndex:0] rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSAnchoredSearch)];
NSRange titleResultsRange2 = [[split objectAtIndex:1] rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSAnchoredSearch)];
if ((titleResultsRange.length > 0) || (titleResultsRange2.length > 0))
{
[copyListofItems addObject:sTemp];
}
}
}
else
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSAnchoredSearch)];
if (titleResultsRange.length > 0)
{
[copyListofItems addObject:sTemp];
}
}
}
[searchArray release];
}
- (void) doneSearching_Clicked:(id)sender
{
self.navigationController.navigationBarHidden = NO;
searchbar.showsCancelButton = NO;
searchbar.text = @"";
[searchbar resignFirstResponder];
letUserSelectRow = YES;
searching = NO;
self.navigationItem.rightBarButtonItem = nil;
self.tableview.scrollEnabled = YES;
[ovController.view removeFromSuperview];
[ovController release];
ovController = nil;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Refresh" style:UIBarButtonSystemItemDone target:self action:@selector(loadData)] autorelease];
[self.tableview reloadData];
}
customize cell
// CustomCell.h
// iwebservice
//
#import
@interface CustomCell : UITableViewCell {
UILabel *primaryLabel;
UILabel *secondaryLabel;
UIImageView *myImageView;
}
@property(nonatomic,retain)UILabel *primaryLabel;
@property(nonatomic,retain)UILabel *secondaryLabel;
@property(nonatomic,retain)UIImageView *myImageView;
@end
now put the code in its m file
// CustomCell.m
// iwebservice
#import "CustomCell.h"
@implementation CustomCell
@synthesize primaryLabel,secondaryLabel,myImageView;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
primaryLabel = [[UILabel alloc]init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.font = [UIFont systemFontOfSize:14];
secondaryLabel = [[UILabel alloc]init];
secondaryLabel.textAlignment = UITextAlignmentLeft;
secondaryLabel.font = [UIFont systemFontOfSize:8];
myImageView = [[UIImageView alloc]init];
[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:secondaryLabel];
[self.contentView addSubview:myImageView];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,0, 50, 50);
myImageView.frame = frame;
frame= CGRectMake(boundsX+70 ,5, 200, 25);
primaryLabel.frame = frame;
frame= CGRectMake(boundsX+70 ,30, 100, 15);
secondaryLabel.frame = frame;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[super dealloc];
}
@end
now you can easily use it in your code
example,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(searching)
{
cell.primaryLabel.text = [copyListofItems objectAtIndex:indexPath.row];
}
else
{
if(indexPath.section ==0)
{
return nil;
}
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section-1];
NSArray *array = [dictionary objectForKey:@"Country"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.primaryLabel.text = cellValue;
}
cell.primaryLabel.backgroundColor = [UIColor clearColor];
cell.primaryLabel.textColor = [UIColor colorWithRed:139.0/256 green:196.0/256 blue:40.0/256 alpha:100];
cell.primaryLabel.highlightedTextColor = [UIColor blackColor];
cell.secondaryLabel.text =[finalsgn objectForKey:[final objectForKey:cell.primaryLabel.text] ];
cell.secondaryLabel.backgroundColor = [UIColor clearColor];
cell.secondaryLabel.textColor = [UIColor colorWithRed:144.0/256 green:154.0/256 blue:47.0/256 alpha:100];
cell.secondaryLabel.highlightedTextColor = [UIColor blackColor];
UIImageView *cellBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"add-cell.jpg"]];
[cell setBackgroundView:cellBG];
[cellBG release];
UIImageView *cellbkBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"add-flate.jpg"]];
cell.selectedBackgroundView =cellbkBG;
cell.selectedBackgroundView.clearsContextBeforeDrawing = NO;
[cellbkBG release];
[cell.backgroundView setNeedsDisplay];
return cell;
}
enjoy !
make sections in uitableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([listOfItems count]==0)
{
return 0;
}
if (searching)
{
return 1;
}
else
{
UILocalizedIndexedCollation *currentCollation = [UILocalizedIndexedCollation currentCollation];
return [currentCollation.sectionTitles count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (searching)
{
return [copyListofItems count];
}
else
{
if (section==0) {
return 0;
}
if(section<27)
{
NSDictionary *dictionary = [listOfItems objectAtIndex:section-1];
NSArray *array = [dictionary objectForKey:@"Country"];
return [array count];
}
else {
return 0;
}
}
}
now yu need to give titles them
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if(searching)
return nil;
return [[NSArray arrayWithObject:UITableViewIndexSearch] arrayByAddingObjectsFromArray:
[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
if(searching)
{
return -1;
}
else
{
UILocalizedIndexedCollation *currentCollation = [UILocalizedIndexedCollation currentCollation];
if (index>0)
{
return [currentCollation sectionForSectionIndexTitleAtIndex:index];
}
else
{
return 0;
}
}
}
enjoy!
perform selector on main thread
here it is how to do it
[self.tableview performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
enjoy!
how to load uitableview in background
- (void) loadData {
self.navigationItem.rightBarButtonItem = nil;
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[activityIndicator startAnimating];
UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[activityIndicator release];
self.navigationItem.rightBarButtonItem = activityItem;
[activityItem release];
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(Loadtabledata)
object:nil];
[queue addOperation:operation];
[operation release];
}
and now make main method
-(void) Loadtabledata
{
searching = NO;
letUserSelectRow = YES;
//listOfItems2 = [listOfItems mutableCopy];
[listOfItems2 removeAllObjects];
DataController *controller = [[DataController alloc] init];
self.dataController = controller;
[self.dataController SetFilter:searchfilter];
[controller release];
searchbar.autocorrectionType = UITextAutocorrectionTypeNo;
usernames = [self.dataController GetList];
NSMutableArray *IDs = [NSMutableArray array];
IDs = [self.dataController GetIds];
NSEnumerator *userIterator= [usernames objectEnumerator];
NSString *username;
NSMutableArray *array = [NSMutableArray array];
while(username = [userIterator nextObject])
{
[array addObject:username];
}
NSEnumerator *idIterator= [IDs objectEnumerator];
NSMutableArray *arrayid = [NSMutableArray array];
while(IDs = [idIterator nextObject])
{
[arrayid addObject:IDs];
}
final = [[NSMutableDictionary alloc] initWithObjects:arrayid forKeys:array];
[array sortUsingSelector:@selector(compare:)];
NSArray *charArray = [[NSArray alloc] initWithObjects: @"a", @"b", @"c", @"d",
@"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n",
@"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w",
@"x", @"y", @"z", nil];
for (int a = 0; a < 26; a++)
{
NSMutableArray *array2 = [NSMutableArray arrayWithArray:usernames];
NSString *tmpValue = nil;
tmpValue =[charArray objectAtIndex: a];
NSPredicate *gpredicate = [NSPredicate predicateWithFormat:@"SELF beginswith %@ || SELF beginswith %@ ",tmpValue,[tmpValue uppercaseString]];
[array2 filterUsingPredicate:gpredicate];
NSDictionary *tempdict = [NSDictionary dictionaryWithObject:array2 forKey:@"Country"];
[listOfItems2 addObject:tempdict];
}
NSMutableArray *array2 = [NSMutableArray arrayWithArray:array];
NSPredicate *gpredicate = [NSPredicate predicateWithFormat:@"!SELF MATCHES %@",@"([a-zA-Z]).*"];
[array2 filterUsingPredicate:gpredicate];
NSDictionary *tempdict = [NSDictionary dictionaryWithObject:array2 forKey:@"Country"];
[listOfItems2 addObject:tempdict];
NSMutableArray *subgroupnames = [[NSMutableArray alloc]init];
subgroupnames = [self.dataController Getsgns];
NSEnumerator *sgnIterator= [subgroupnames objectEnumerator];
NSMutableArray *arraysgn = [NSMutableArray array];
while(subgroupnames = [sgnIterator nextObject])
{
[arraysgn addObject:subgroupnames];
}
finalsgn = [[NSMutableDictionary alloc] initWithObjects:arraysgn forKeys:arrayid];
NSMutableArray *mobs = [[NSMutableArray alloc]init];
mobs = [self.dataController Getmobiles];
NSEnumerator *mobIterator= [mobs objectEnumerator];
NSMutableArray *arraymob = [NSMutableArray array];
while(mobs = [mobIterator nextObject])
{
[arraymob addObject:mobs];
}
mobilnos = [[NSMutableDictionary alloc] initWithObjects:arraymob forKeys:arrayid];
NSMutableArray *ems = [[NSMutableArray alloc]init];
ems = [self.dataController GetEmails];
NSEnumerator *emsIterator= [ems objectEnumerator];
NSMutableArray *arrayem = [NSMutableArray array];
while(ems = [emsIterator nextObject])
{
[arrayem addObject:ems];
}
emails = [[NSMutableDictionary alloc] initWithObjects:arrayem forKeys:arrayid];
NSMutableArray *fes = [[NSMutableArray alloc]init];
fes = [self.dataController GetFexs];
NSEnumerator *fesIterator= [fes objectEnumerator];
NSMutableArray *arrayfe = [NSMutableArray array];
while(fes = [fesIterator nextObject])
{
[arrayfe addObject:fes];
}
fexs = [[NSMutableDictionary alloc] initWithObjects:arrayfe forKeys:arrayid];
NSMutableArray *bds = [[NSMutableArray alloc]init];
bds = [self.dataController GetBirthdays];
NSEnumerator *bdsIterator= [bds objectEnumerator];
NSMutableArray *arraybd = [NSMutableArray array];
while(bds = [bdsIterator nextObject])
{
[arraybd addObject:bds];
}
birthdays = [[NSMutableDictionary alloc] initWithObjects:arraybd forKeys:arrayid];
NSMutableArray *ans = [[NSMutableArray alloc]init];
ans = [self.dataController GetAnniversarydays];
NSEnumerator *ansIterator= [ans objectEnumerator];
NSMutableArray *arrayan = [NSMutableArray array];
while(ans = [ansIterator nextObject])
{
[arrayan addObject:ans];
}
annidays = [[NSMutableDictionary alloc] initWithObjects:arrayan forKeys:arrayid];
if([listOfItems isEqualToArray:listOfItems2 ])
{
}
else {
listOfItems = [listOfItems2 copy];
finaltmp = [final copy];
[self.tableview performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
}
[self writeToPlist];
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Refresh" style:UIBarButtonSystemItemDone target:self action:@selector(loadData)] autorelease];
}
NET Web Service Classes
// WebServiceHelper.h
//
#import
@interface WebServiceHelper : NSObject {
NSString *XMLNameSpace;
NSString *XMLURLAddress;
NSString *MethodName;
NSString *SOAPActionURL;
NSMutableDictionary *MethodParameters;
NSMutableString *MethodParametersAsString;
}
@property(nonatomic, retain) NSString *XMLNameSpace;
@property(nonatomic, retain) NSString *XMLURLAddress;
@property(nonatomic, retain) NSString *MethodName;
@property(nonatomic, retain) NSMutableDictionary *MethodParameters;
@property(nonatomic, retain) NSString *SOAPActionURL;
@property(nonatomic, retain) NSMutableString *MethodParametersAsString;
- (NSMutableData*)initiateConnection;
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
Now in its .m file put the code below
// WebServiceHelper.m
//
#import "WebServiceHelper.h"
#import "XmlDataSetParser.h"
@implementation WebServiceHelper
@synthesize MethodName;
@synthesize MethodParameters;
@synthesize XMLNameSpace;
@synthesize XMLURLAddress;
@synthesize SOAPActionURL;
@synthesize MethodParametersAsString;
- (NSMutableData*)initiateConnection
{
NSMutableString *sRequest = [[NSMutableString alloc] init];
self.SOAPActionURL = [NSString stringWithFormat:@"%@%@",self.XMLNameSpace, self.MethodName];
[sRequest appendString:@""];
[sRequest appendString:@"
[sRequest appendString:@"
[sRequest appendString:[NSString stringWithFormat:@"<%@ xmlns=\"%@\">",MethodName, XMLNameSpace]];
if(MethodParametersAsString != nil) [sRequest appendString:MethodParametersAsString];
NSEnumerator *tableIterator = [MethodParameters keyEnumerator];
NSString *keyID;
while(keyID = [tableIterator nextObject])
{
[sRequest appendString:[NSString stringWithFormat:@"<%@>%@%@>", keyID, [MethodParameters objectForKey:keyID], keyID]];
}
[sRequest appendString:[NSString stringWithFormat:@"%@>", MethodName]];
[sRequest appendString:@"
[sRequest appendString:@"
NSURL *myWebserverURL = [NSURL URLWithString:XMLURLAddress];
//NSLog(@"%@",sRequest);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myWebserverURL];
[request addValue:[NSString stringWithFormat:@"%d",sRequest.length] forHTTPHeaderField:@"Content-length"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"beta.ecoscrm.com" forHTTPHeaderField:@"Host"];
[request addValue:SOAPActionURL forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSMutableData *myMutableData;
if(conn)
{
myMutableData=[[NSMutableData data] retain];
}
else {
NSLog(@"CONNECTION FAILED");
}
[NSURLConnection connectionWithRequest:request delegate:self];
NSError *WSerror;
NSURLResponse *WSresponse;
myMutableData = [[NSMutableData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror]];
NSString *theXml = [[NSString alloc]initWithBytes:[myMutableData mutableBytes] length:[myMutableData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXml);
[sRequest release];
return myMutableData;
[myMutableData release];
}
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host
{
return YES;
}
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host
{
}
@end
The class is used to call net web service which will return xml schema
you need to make two more classes now
make a class DataSet.h
// DataSet.h
#import
@interface DataSet : NSObject {
NSString *DataSetName;
NSMutableDictionary *Tables;
}
@property (nonatomic, retain) NSMutableDictionary *Tables;
@property (nonatomic, retain) NSString *DataSetName;
-(id)initWithXMLData:(NSMutableData *)data;
-(NSString *)getFirstForTableAndColumn:(NSString *)tableName col:(NSString *)colName;
-(NSMutableDictionary *)getRowsForTableAndColumn:(NSString *)tableName col:(NSString *)colName;
-(NSMutableDictionary *)getRowsForTableAndColumnWhereEqualsString:(NSString *)tableName col:(NSString *)colName where:(NSString *)wherevalue;
-(NSMutableDictionary *)getRowsForTableAndColumnWhereColumnEqualsString:(NSString *)tableName col:(NSString *)colName whereColumn:(NSString *)wherecolumn whereValue:(NSString *)wherevalue;
-(NSMutableDictionary *)getRowsForTableAndColumnWhereEqualsStrings:(NSString *)tableName col:(NSString *)colName where:(NSArray *)wherevalues;
-(void) dealloc;
@end
now put the code below in its .m file
// DataSet.m
//
#import "DataSet.h"
#import "XMLDataSetParser.h"
@implementation DataSet
@synthesize Tables;
@synthesize DataSetName;
-(id)initWithXMLData:(NSMutableData *)data
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
XmlDataSetParser *xmParser = [[XmlDataSetParser alloc] init];
[parser setDelegate:xmParser];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
NSString *theXml = [[NSString alloc]initWithBytes:[data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding];
// NSLog([[NSString alloc] initWithFormat:@"response:%@", theXml, nil]);
self.Tables = [[xmParser thisDataset] Tables];
[parser release];
[xmParser release];
return self;
}
-(NSMutableDictionary *)getRowsForTableAndColumn:(NSString *)tableName col:(NSString *)colName
{
NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];
NSEnumerator *tableIterator = [self.Tables keyEnumerator];
NSString *tableKey;
while(tableKey = [tableIterator nextObject])
{
if([tableKey isEqualToString:tableName])
{
NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray: [[self.Tables objectForKey:tableKey] allKeys]];
[sortedKeys sortUsingSelector:@selector(compare:)];
NSEnumerator *rowIterator = [sortedKeys objectEnumerator];
NSString *rowKey;
while(rowKey = [rowIterator nextObject])
{
if([rowKey rangeOfString:colName].location != NSNotFound)
{
NSArray *split = [rowKey componentsSeparatedByString:@"--"];
if([[split objectAtIndex:0] isEqualToString:colName])
{
[returnDictionary setObject:[[self.Tables objectForKey:tableKey] objectForKey:rowKey] forKey:[split objectAtIndex:1]];
}
}
}
}
}
if ([returnDictionary count] > 0)
{
return returnDictionary;
}
else
{
return nil;
}
[returnDictionary release];
}
-(NSString *)getFirstForTableAndColumn:(NSString *)tableName col:(NSString *)colName
{
NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];
NSEnumerator *tableIterator = [self.Tables keyEnumerator];
NSString *tableKey;
while(tableKey = [tableIterator nextObject])
{
if([tableKey isEqualToString:tableName])
{
NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray: [[self.Tables objectForKey:tableKey] allKeys]];
[sortedKeys sortUsingSelector:@selector(compare:)];
NSEnumerator *rowIterator = [sortedKeys objectEnumerator];
NSString *rowKey;
while(rowKey = [rowIterator nextObject])
{
if([rowKey rangeOfString:colName].location != NSNotFound)
{
NSArray *split = [rowKey componentsSeparatedByString:@"--"];
if([[split objectAtIndex:0] isEqualToString:colName])
{
[returnDictionary setObject:[[self.Tables objectForKey:tableKey] objectForKey:rowKey] forKey:[split objectAtIndex:1]];
}
}
}
}
}
if ([returnDictionary count] > 0)
{
return [returnDictionary objectForKey:@"1"];
}
else
{
return nil;
}
[returnDictionary release];
}
-(NSMutableDictionary *)getRowsForTableAndColumnWhereEqualsStrings:(NSString *)tableName col:(NSString *)colName where:(NSArray *)wherevalues
{
return nil;
}
-(NSMutableDictionary *)getRowsForTableAndColumnWhereEqualsString:(NSString *)tableName col:(NSString *)colName where:(NSString *)wherevalue
{
NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];
NSEnumerator *tableIterator = [self.Tables keyEnumerator];
NSString *tableKey;
while(tableKey = [tableIterator nextObject])
{
if([tableKey isEqualToString:tableName])
{
NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray: [[self.Tables objectForKey:tableKey] allKeys]];
[sortedKeys sortUsingSelector:@selector(compare:)];
NSEnumerator *rowIterator = [sortedKeys objectEnumerator];
NSString *rowKey;
while(rowKey = [rowIterator nextObject])
{
if([rowKey rangeOfString:colName].location != NSNotFound)
{
NSArray *split = [rowKey componentsSeparatedByString:@"--"];
NSString *rowData = [[self.Tables objectForKey:tableKey] objectForKey:rowKey];
if ([rowData isEqualToString:wherevalue])
{
[returnDictionary setObject:rowData forKey:[split objectAtIndex:1]];
}
}
}
}
}
if ([returnDictionary count] > 0)
{
return returnDictionary;
}
else
{
return nil;
}
[returnDictionary release];
}
-(NSMutableDictionary *)getRowsForTableAndColumnWhereColumnEqualsString:(NSString *)tableName col:(NSString *)colName whereColumn:(NSString *)wherecolumn whereValue:(NSString *)wherevalue
{
NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];
NSEnumerator *tableIterator = [self.Tables keyEnumerator];
NSString *tableKey;
while(tableKey = [tableIterator nextObject])
{
if([tableKey isEqualToString:tableName])
{
NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray: [[self.Tables objectForKey:tableKey] allKeys]];
[sortedKeys sortUsingSelector:@selector(compare:)];
NSEnumerator *rowIteratorWhereColumn = [sortedKeys objectEnumerator];
NSString *rowWhereKey;
while(rowWhereKey = [rowIteratorWhereColumn nextObject])
{
if([rowWhereKey rangeOfString:wherecolumn].location != NSNotFound)
{
NSArray *split = [rowWhereKey componentsSeparatedByString:@"--"];
NSString *coldata = [[self.Tables objectForKey:tableKey] objectForKey:rowWhereKey];
if([coldata isEqualToString:wherevalue])
{
if( [[self.Tables objectForKey:tableKey] objectForKey:[NSString stringWithFormat:@"%@--%@", colName, [split objectAtIndex:1]]]==nil)
{
}
else
{
[returnDictionary setObject:[[self.Tables objectForKey:tableKey] objectForKey:[NSString stringWithFormat:@"%@--%@", colName, [split objectAtIndex:1]]] forKey:[split objectAtIndex:1]];
}
}
}
}
}
}
if ([returnDictionary count] > 0)
{
return returnDictionary;
}
else
{
return nil;
}
[returnDictionary release];
}
-(void)dealloc
{
self.Tables = nil;
[super dealloc];
}
@end
this will create dataset
u need one more class to parse ur xml
make class
// XMLDataSetParser.h
//
#import
#import "DataSet.h"
@interface XmlDataSetParser : NSObject {
DataSet *thisDataset;
Boolean beginDataset;
Boolean beginTable;
NSString *currentTable;
NSString *currentColumn;
}
@property (nonatomic, retain) DataSet *thisDataset;
@property (nonatomic, retain) NSString *currentTable;
@property (nonatomic, retain) NSString *currentColumn;
- (void)parserDidStartDocument:(NSXMLParser *)parser;
- (void)parserDidEndDocument:(NSXMLParser *)parser;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
@end
put the code below in its m file
// XMLDataSetParser.m
//
#import "XmlDataSetParser.h"
#import "DataSet.h"
@implementation XmlDataSetParser
@synthesize thisDataset;
@synthesize currentTable;
@synthesize currentColumn;
#pragma XML Parser Delegate Methods
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
thisDataset = [[DataSet alloc] init];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if (beginDataset == YES)
{
thisDataset.DataSetName = elementName;
thisDataset.Tables =[[NSMutableDictionary alloc] init];
beginDataset = NO;
}
else
{
if ([elementName isEqualToString:@"diffgr:diffgram"]) {
beginDataset = YES;
}
if (thisDataset.DataSetName != nil && beginTable == YES)
{
if([currentColumn isEqualToString:@""] || currentColumn == nil)
{
currentColumn = elementName;
}
}
if (thisDataset.DataSetName != nil && beginTable == NO)
{
if([thisDataset.Tables objectForKey:elementName] == nil)
{
[thisDataset.Tables setObject: [[NSMutableDictionary alloc] init] forKey:elementName];
currentTable = elementName;
}
beginTable = YES;
}
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if(beginTable == YES && [elementName isEqualToString:currentTable])
{
beginTable = NO;
}
if(beginTable == YES && currentColumn != @"")
{
currentColumn = @"";
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to convert XML into dataset: %@", [parseError description]];
NSLog(@"error parsing XML: %@", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSMutableDictionary *table = [[NSMutableDictionary alloc] initWithDictionary:[thisDataset.Tables objectForKey:currentTable]];
NSEnumerator *myIterator = [table keyEnumerator];
NSString *aKey;
NSInteger intCnt = 0;
while(aKey = [myIterator nextObject])
{
NSArray *split = [[NSArray alloc] initWithArray:[aKey componentsSeparatedByString:@"--"]];
if([[split objectAtIndex:0] isEqualToString:currentColumn])
{
intCnt++;
}
[split release];
}
NSString *colName = [[NSString alloc] initWithFormat:@"%@--%d", self.currentColumn, (intCnt+1), nil];
[table setObject:string forKey:colName];
[thisDataset.Tables setObject:table forKey:currentTable];
[colName release];
[table release];
}
@end
now u have all the classes defined for web service
now here below i show you how to use it
#import "DataSet.h"
#import "WebServiceHelper.h"
import the above in class in which u wanna call web service
now use below code
WebServiceHelper *DataCon = [[WebServiceHelper alloc] init];
DataCon.XMLNameSpace = @"http://tempuri.org/";
DataCon.XMLURLAddress = @"http://yourdomain.com/webservicesfolder/service1.asmx";
DataCon.MethodName = @"MyMethodName";
DataCon.MethodParameters = [[NSMutableDictionary alloc] init];
[DataCon.MethodParameters setObject:[[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"] forKey:@"UserName"];
[DataCon.MethodParameters setObject:[[NSUserDefaults standardUserDefaults] stringForKey:@"password_preference"] forKey:@"Password"];
[DataCon.MethodParameters setObject:search forKey:@"GroupId"];
tdata = [DataCon initiateConnection];
DataSet *dsMyDataSet = [[DataSet alloc] initWithXMLData:tdata];
NSMutableArray *Name = [[NSMutableDictionary alloc] init];
NSMutableArray *ID = [[NSMutableDictionary alloc] init];
NSMutableArray *SGN = [[NSMutableDictionary alloc]init];
ID = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"Id"];
Name = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"DisplayName"];
SGN = [dsMyDataSet getRowsForTableAndColumn:@"_x0030_" col:@"SubGroupName"];
Enjoyy !
Soap Envelope in Xcode
ommunicating with the outside world is one of the ways to make your iPhone applications interesting and useful. This is especially true today where they are so many web services that provide so much useful functionality. However, consuming web services in iPhone is not for the faint-of-heart. Unlike other development tools (such as Microsoft Visual Studio), Xcode does not have built-in tools that make consuming web services easy. Everything must be done by hand and you need to know how to form the relevant XML messages to send to the web services and then parse the returning XML result.
This article will give you a good understanding of how to communicate with XML web services from within your iPhone application, and the examples will provide a solid foundation for consuming other web services in your own projects.
Consuming Web Services
Figure 1. IPToCountry Web Service: This web service exposes two web methods. |
Before beginning an Xcode project to consume a web service, examining a real web service to see the different ways you can consume is worthwhile. My favorite example is to use an ASMX XML web service created using .NET. For discussion purposes, here's a look at a web service called IPToCountry, which lets you supply an IP address and returns the country to which the IP address belongs.
The IPToCountry web service is located at http://www.ecubicle.net/iptocountry.asmx. If you use Safari to load this URL, you'll see that it exposes two web methods as shown in Figure 1.
As an example, the FindCountryAsXML returns the result (the country) as an XML string. Clicking the FindCountryAsXML link reveals the page shown in Figure 2.
Figure 2. FindCountryAsXml Service: Testing the web service through a browser interface shows the XML packets exchanged between the client and the server. |
The important parts are the sections following the Test section of the page. They detail the various ways in which you can consume the web service. In the .NET world, accessing the web service is a pretty straightforward affair—Visual Studio provides a built-in tool that automatically creates a web proxy service object for the web service when you download the WSDL document. For iPhone development, you need to get your hands dirty, so it's far more important to understand the underlying mechanics of consuming web services.
Using SOAP 1.1
One way to consume this web service is to use SOAP (Simple Object Access Protocol). When using SOAP, you use the HTTP POST method to send a request to the web service, as shown in the following example:
POST /iptocountry.asmx HTTP/1.1
Host: www.ecubicle.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.ecubicle.net/webservices/FindCountryAsXml"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<FindCountryAsXml xmlns="http://www.ecubicle.net/webservices/">
<V4IPAddress>string</V4IPAddress>
</FindCountryAsXml>
</soap:Body>
</soap:Envelope>
The parts of the code in bold are the placeholders where you need to substitute the actual values. A couple of important things to note from the above:
- The URL for the web service is http://www.ecubicle.net/iptocountry.asmx.
- The URL for the SOAPAction attribute is http://www.ecubicle.net/webservices/FindCountryAsXml.
- The Content-Type for the request is text/xml; charset=utf-8.
- The HTTP method is POST.
- The SOAP request is shown below:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<FindCountryAsXml xmlns="http://www.ecubicle.net/webservices/">
<V4IPAddress>string</V4IPAddress>
</FindCountryAsXml>
</soap:Body>
</soap:Envelope> - The Content-Length of the SOAP request is the total number of characters in the complete SOAP request.
The web service will return a response formatted as follows:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<FindCountryAsXmlResponse
xmlns="http://www.ecubicle.net/webservices/">
<FindCountryAsXmlResult>xml result</FindCountryAsXmlResult>
</FindCountryAsXmlResponse>
</soap:Body>
</soap:Envelope>
For a real request, the result (the country) will be enclosed within the block of XML code (the bold placeholder in the preceding example). You would need to extract it from the XML result.