Friday, 30 October 2015

Circular Progress View

iOS framework don't provide pre-build 'Circular Progress View/Bar' like UIProgressBar.
Implementing a 'Circular Progress View' is simple in iOS. Here piece of code for implement 'Circular Progress View'.

1) Create a Sub-Class of UIView Class

@interface CircularProgressView : UIView
{
    CGFloat startAngle;
    CGFloat endAngle;
}
@property (nonatomic, assign) CGFloat percentage;
-(id) initWithFrame:(CGRect)frame;
@end

#import "CircularProgressView.h"

@implementation CircularProgressView

-(id) initWithFrame:(CGRect)frame{
 
  self = [super initWithFrame:frame];
    if (self) {
       
        startAngle = M_PI * 1.5;
        endAngle = startAngle + (M_PI*2);
    }
    return self; 
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor colorWithRed:(CGFloat)235/255 green:(CGFloat)237/255 blue:(CGFloat)236/255 alpha:1] setStroke];
    UIBezierPath *benzierPathBack = [UIBezierPath bezierPath];
    [benzierPathBack addArcWithCenter:CGPointMake(rect.size.width / 2, rect.size.height / 2) radius:(rect.size.width / 2 - 5) startAngle:startAngle endAngle:(endAngle - startAngle) * (100 / 100.0) + startAngle clockwise:YES];
   
    benzierPathBack.lineWidth = 10;
    [benzierPathBack stroke];
   
    CGContextSaveGState(context);
   
    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
   
    // Create our arc, with the correct angles
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width / 2, rect.size.height / 2) radius:(rect.size.width / 2-5) startAngle:startAngle endAngle:(endAngle - startAngle) * (_percentage / 100.0) + startAngle clockwise:YES];
   
    // Set the display for the path, and stroke it
    bezierPath.lineWidth = 10;
    [[UIColor colorWithRed:(CGFloat)1/255 green:(CGFloat)111/255 blue:(CGFloat)37/255 alpha:1] setStroke];
    [bezierPath stroke];  
}


Import CircularProgressView Class, create an object and change percentage value, you can see a 'Circular Progress View'.

Friday, 23 October 2015

Multiform request in Objective c

MultiForm request with NSUrlConnection.  
 
     //Url
    NSString *urlString = @"";

 //Create Url request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
   
    NSString *boundary = @"0xLhTaLbOkNdArZ";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
   
    //Reading the file
    NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.zip"];
    NSData *myData = [NSData dataWithContentsOfFile:filePath];
   
    NSMutableData *body = [NSMutableData data];
   
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%filePath.zip\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/zip\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:myData];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
   
    [request setHTTPBody:body];
   
    NSError *returnError = nil;
    NSHTTPURLResponse *returnResponse = nil;
   
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnResponse error:&returnError];
    if (returnError) {
        NSLog(@"return Error = %@",returnError);
    }
    NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"str = %@",str);
   
    if (returnResponse.statusCode == 200) {
        NSLog(@"Success");
        }
    }

//==========================================
 MultiForm request with AFHTTPNetwork 
//Url
    NSString *urlString = @"";

//Create Operation Manager instance
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

//Prepare Parameter for request
    NSDictionary *postData = @{};

    [manager POST:urlString parameters:dictPostData constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
       //Add file to request
 [formData appendPartWithFileData:imageData name:@"file" fileName:@"filename.jpeg"] mimeType:@"image/jpeg"];

 }success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response: %@", responseObject);
        if(operation.response.statusCode == 200){
            NSLog(@"Success");
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

Friday, 16 October 2015

Random number in Objective C

Random int between 1 and N
NSUInteger r = arc4random_uniform(N); 


Random int between 1 and N
NSUInteger r = arc4random_uniform(N) + 1; 

Random double between 0 and 1
srand48(time(0)); double r = drand48(); 

Pick a random element from an array
if ([array count] > 0) {  
    id obj = array[arc4random_uniform([array count])]; } 

Generate a Random Lowercase NSString
NSString *letter = [NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'a']; 
Generate a Random Uppercase NSString
NSString *letter = [NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'A'];
Pick a Random character from string
NSString *vowels = @"aeiouy"; 
NSString *letter = [vowels substringWithRange:NSMakeRange(arc4random_uniform([vowels length]), 1)];