Friday, March 10, 2017

How to set the validation for Full name field using Objective C?

Full name field validation using Objective C code.

if ([self validate:self.fullnamefield.text] == 0)
{
        NSLog(@"Please Enter Valid User Name");

}

- (BOOL)validate:(NSString *)string
{
    NSError *error             = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z .]" options:0 error:&error];
    
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
    
    return numberOfMatches == string.length;

}

How to compress image without quality loss using Objective C?

Compress image, without quality loss using Objective C code:


NSData *imageData1= nil;

imageData1 =UIImageJPEGRepresentation([self compressForUpload:self.imageView.image scale:1.0], 0.8);

-(UIImage *)compressForUpload:(UIImage *)original scale:(CGFloat)scale
{
    CGSize originalSize = original.size;
    CGSize newSize = CGSizeMake(originalSize.width  *scale, originalSize.height  *scale);
    
    UIGraphicsBeginImageContext(newSize);
    [original drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage* compressedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return compressedImage;

}

Monday, March 6, 2017

How to make a phone call programmatically in iOS?

I struggled lot to find this solution to make a call from iPhone using Objective C code.
Please use the below code to solve that issue.

Objective C:

NSString *phNo = @"911234567890; //91-countrycode remaining are mobile number.

NSString *phoneNumber = [NSString stringWithFormat:@"tel:%@",phNo];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

Note: For iPad it won't work.

Swift:

let phoneNo = model.mobileNo
let newString:String = phoneNo.replacingOccurrences(of: " ", with: "")
let phoneUrl = URL(string: "telprompt:+91\(newString)")
if let callURL = phoneUrl {
     self.makeCall(callURL)

}

//makeCall function
func makeCall(_ phoneURL:URL) {
        if UIApplication.shared.canOpenURL(phoneURL) {
            UIApplication.shared.openURL(phoneURL)
        }
        else {
            let alertController = UIAlertController(title: "Warning!", message: "Not possible to make call", preferredStyle: .alert)
            let okAct = UIAlertAction(title: "OK", style: .default, handler: { (act) in
                print("OK")
            })
            alertController.addAction(okAct)
            self.present(alertController, animated: true, completion: nil)
        }
    }