Showing posts with label iPhone Application Development. Show all posts
Showing posts with label iPhone Application Development. Show all posts

Thursday, April 11, 2013

How to use SOAP WebService API - Objective-C iOS

http://www.w3schools.com/webservices/tempconvert.asmx

The above URL provides two SOAP web service

1. CelsiusToFahrenheit

Request Format:


POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/CelsiusToFahrenheit"

<?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>
    <CelsiusToFahrenheit xmlns="http://tempuri.org/">
      <Celsius>string</Celsius>
    </CelsiusToFahrenheit>
  </soap:Body>
</soap:Envelope>
   
Response Format


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>
    <CelsiusToFahrenheitResponse xmlns="http://tempuri.org/">
      <CelsiusToFahrenheitResult>string</CelsiusToFahrenheitResult>
    </CelsiusToFahrenheitResponse>
  </soap:Body>
</soap:Envelope>

2. FahrenheitToCelsius

Request Format:



POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/FahrenheitToCelsius"

<?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>
    <FahrenheitToCelsius xmlns="http://tempuri.org/">
      <Fahrenheit>string</Fahrenheit>
    </FahrenheitToCelsius>
  </soap:Body>
</soap:Envelope>
Response Format:



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>
    <FahrenheitToCelsiusResponse xmlns="http://tempuri.org/">
      <FahrenheitToCelsiusResult>string</FahrenheitToCelsiusResult>
    </FahrenheitToCelsiusResponse>
  </soap:Body>
</soap:Envelope>

iPhone application is going to consume these two service to convert Fahrenheit to Celsius and vice versa. 


#import <Foundation/Foundation.h>


@interface RequestResponseHandler : NSObject<NSURLConnectionDelegate, NSXMLParserDelegate>
{
    NSURLConnection *connection;
    NSMutableData *responseData;
    CallBack callback;
    
    NSMutableDictionary *model;
}

-(void) executeRequest:(NSURLRequest*) request completion:(CallBack) completionCallback;

+(RequestResponseHandler*) sharedInstance;

@end




#import "RequestResponseHandler.h"
#import "CelsiusToFahrenheitResponse.h"
#import "FahrenheitToCelsiusResponse.h"

static RequestResponseHandler *sharedInstance;

@implementation RequestResponseHandler

- (id)init
{
    self = [super init];
    if (self) {
        responseData = [[NSMutableData alloc] init];
    }
    return self;
}

+(RequestResponseHandler*) sharedInstance
{
    if  (sharedInstance == nil)
    {
        sharedInstance = [[self alloc] init];
    }
    
    return sharedInstance;

}

-(void) executeRequest:(NSURLRequest*) request completion:(CallBack)completionCallback
{
    callback = completionCallback;
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    
    responseData = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aconnection {
    
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:responseData];
    parser.delegate = self;
    [parser parse];
}


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
        
    if ([elementName isEqualToString:@"CelsiusToFahrenheitResult"])
    {
        model = [[NSMutableDictionary alloc] init];
        [model setObject:@"" forKey:@"CelsiusToFahrenheitResult"];
    }
    else if ([elementName isEqualToString:@"FahrenheitToCelsiusResult"])
    {
        model = [[NSMutableDictionary alloc] init];
        [model setObject:@"" forKey:@"FahrenheitToCelsiusResult"];
    }
 
}

//This method is to store the result between element (Result):
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
 
    NSArray *keys = [model allKeys];
    for (NSString *key in keys) {
        [model setValue:string forKey:key];
    }
        
}


-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
{
    if ([elementName isEqualToString:@"CelsiusToFahrenheitResult"] || [elementName isEqualToString:@"FahrenheitToCelsiusResult"]) {
        callback(model);
    }
}


@end

#import <Foundation/Foundation.h>


@interface SoapRequestFactory : NSObject

+(SoapRequestFactory*) defaultInstance;
-(NSURLRequest*) getRequestForType:(REQUEST_TYPE) requestType embeddingValue:(NSString*) value;

@end

#import "SoapRequestFactory.h"

static SoapRequestFactory *sharedInstance;

@implementation SoapRequestFactory


+(SoapRequestFactory*) defaultInstance
{
    if  (sharedInstance == nil)
    {
        sharedInstance = [[self alloc] init];
    }
    
    return sharedInstance;
}

-(NSURLRequest*) getRequestForType:(REQUEST_TYPE) requestType embeddingValue:(NSString*) value
{
    switch (requestType) {
        case CELSIUS_TO_FAHRENHEIT:
            return [self getCelsiusToFahrenheit:value];
            
        case FAHRENHEIT_TO_CELSIUS:
            return [self getFahrenheitToCelsius:value];

            
        default:
            break;
    }
}

-(NSURLRequest*) getCelsiusToFahrenheit:(NSString*) value
{
    
    NSString *soapMessage = @"<?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>"
    "<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">"
    "<Celsius>%@</Celsius>"
    "</CelsiusToFahrenheit>"
    "</soap:Body>"
    "</soap:Envelope>";
    
    
    NSString *message = [NSString stringWithFormat:soapMessage, value];

    NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [message length]];
    
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [message dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    return theRequest;
}




-(NSURLRequest*) getFahrenheitToCelsius:(NSString *) value
{
    NSString *soapMessage = @"<?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>"
    "<FahrenheitToCelsius xmlns=\"http://tempuri.org/\">"
    "<Fahrenheit>%@</Fahrenheit>"
    "</FahrenheitToCelsius>"
    "</soap:Body>"
    "</soap:Envelope>";
    
    NSString *message = [NSString stringWithFormat:soapMessage, value];
    
    NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [message length]];
    
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/FahrenheitToCelsius" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [message dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    return theRequest;
}



@end

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>
{
    IBOutlet UISegmentedControl *segmentedControl;
    IBOutlet UILabel *result;
    NSString *value;
}

-(IBAction)segmentControlChanged:(id) sender;

@end

#import "ViewController.h"

#import "SoapRequestFactory.h"
#import "RequestResponseHandler.h"
#import "CelsiusToFahrenheitResponse.h"
#import "FahrenheitToCelsiusResponse.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    value = textField.text;
    [self segmentControlChanged:segmentedControl];
    
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated.
}

-(IBAction)segmentControlChanged:(UISegmentedControl*) sender
{
    NSURLRequest *request = [[SoapRequestFactory defaultInstance] getRequestForType:sender.selectedSegmentIndex embeddingValue:value];
    
    if (sender.selectedSegmentIndex == 0) {
        [[RequestResponseHandler sharedInstance] executeRequest:request  completion:^(NSDictionary *response){
            result.text = [response valueForKey:@"CelsiusToFahrenheitResult"];            
            
        }];
    }
    else
    {
        [[RequestResponseHandler sharedInstance] executeRequest:request  completion:^(NSDictionary *response){
            result.text = [response valueForKey:@"FahrenheitToCelsiusResult"];            
        }];
    }
}

@end

Wednesday, January 18, 2012

Rotating a Line on an arbitrary point

Rotating an line using mathematics.


In a graph sheet, (0, 0) is arbitrary poing and we will have another point (x, y) so we can draw a line from (0, 0) to (x, y), we can apply rotation formula to calculate new x and y for theta, then draw new line with the new point.

newXPosition = (x * cos(degree)) - (y * sin(degree))

newYPosition = (x * sin(degree)) + (y * cos(degree))


Suppose i have a line from (100, 100) to (150, 150), I want to rotate it from (100, 100).

Now we need to change the arbitrary point from (0, 0) to (100, 100). how to do?


First find out the length of the line then apply the theta, calculate the new poing and add the x and y position with the new x and y position.

(100, 100) and (150, 150)

length is (50, 50)

now i need to rotate the line theta angle.

newX = (50 * cos(degree)) + (50 * sin(degree))

newY = (50 * sin(degree)) + (50 * cos(degree))

newX = newX + 100;

newY = newY + 100;




 //
// STBView.h
// STBLineRotation
//
// Created by Stalin on 1/17/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface STBView : UIView
{
float newX, newY, newX1, newY1;
float radian;
}
@end




 //
// STBView.m
// STBLineRotation
//
// Created by Stalin on 1/17/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "STBView.h"
@implementation STBView
- (id)initWithCoder:(NSCoder *)aDecoder;
{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
newX = 100.0f;
newY = 100.0f;
newX1 = 150.0f;
newY1 = 150.0f;
radian = 0.5;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
float degree = (radian * M_PI) / 180;
NSLog(@"%f %f %f %f", newX, newY, newX1, newY1);
float x, y;
x = newX1;
y = newY1;
x -= newX;
y -= newY;
newX1 = (x * cos(degree)) - (y * sin(degree));
newY1 = (x * sin(degree)) + (y * cos(degree));
newX1 += newX;
newY1 += newY;
NSLog(@"%f %f %f %f", newX, newY, newX1, newY1);
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, newX, newY);
CGContextAddLineToPoint(c, newX1, newY1);
CGContextStrokePath(c);
}
@end




 // 
// ViewController.m
// STBLineRotation
//
// Created by Stalin on 1/17/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(rotateLine:) userInfo:nil repeats:YES];
// Do any additional setup after loading the view, typically from a nib.
}
-(void) rotateLine:(id) sender
{
[self.view setNeedsDisplay];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end









Download the object C code for line rotation









Saturday, December 3, 2011

Bar Chart and area chart rendering on iOS

This demonstrates how to render bar and area chart using the chart data.






Friday, July 22, 2011

Saving images into Photo Gallary


Image is object of UIImage.

UIImageWriteToSavedPhotosAlbum(Image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);



The call back signature should same as the below otherwise we will not get call back


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo


{


}