Monday 19 May 2014

Playing embedded video in iOS

NSString* embeddtext =@"<iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/Neff9scaCCI\" frameborder=\"0\" allowfullscreen></iframe>";

NSLog(@"Full code = %@",embeddtext);

embeddtext = [embeddtext substringFromIndex:[embeddtext rangeOfString:@"src=\""].location+[embeddtext rangeOfString:@"src=\""].length];
embeddtext = [embeddtext substringToIndex:[embeddtext rangeOfString:@"\""].location ];

if([embeddtext rangeOfString:@"http"].location==NSNotFound)
    embeddtext=[NSString stringWithFormat:@"http:%@",embeddtext];

NSLog(@"video src = %@",embeddtext);

NSString *html = [NSString stringWithFormat:@"<iframe width=\"300\" height=\"250\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>",embeddtext];

[webView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];

Thursday 15 May 2014

Rendering Microsoft Files in Using Office Web App.

NSString *fileUrl;// .doc .docx, .ppt, .pptx, .xls, .xlsx files
fileUrl=[fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://view.officeapps.live.com/op/view.aspx?src=%@",fileUrl]]]];

Wednesday 14 May 2014

Url Special Characters encoding

NSString *str=@"!*'();:@&=+$,/?%#[]";//put the url here...

NSString *encodedStr=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                          NULL,
                                                                          (CFStringRef)str,
                                                                          NULL,
                                                                          CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                          kCFStringEncodingUTF8));
NSLog(@"after encoding = %@",encodedStr);

Detect blinks and smiles in Image

UIImage *image = [UIImage imageNamed:@"myImage"];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                          context:nil
                                          options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];

NSDictionary *options = @{ CIDetectorSmile: @YES, CIDetectorEyeBlink: @YES };

NSArray *features = [detector featuresInImage:image.CIImage options:options];

for (CIFaceFeature *feature in features) {
    NSLog(@"Bounds: %@", NSStringFromCGRect(feature.bounds));

    if (feature.hasSmile) {
    NSLog(@"Nice smile!");
    } else {
    NSLog(@"Why so serious?");
    }
    if (feature.leftEyeClosed || feature.rightEyeClosed) {
    NSLog(@"Open your eyes!");
    }
}

Tuesday 13 May 2014

Text to Speech in iOS7



@import AVFoundation;
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Hi sajan antony and kiran paul how are you guys !"];
utterance.rate = AVSpeechUtteranceMaximumSpeechRate / 10.0f;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]; 
// defaults to your system language
[synthesizer speakUtterance:utterance];

Monday 12 May 2014

HTML string from an NSAttributedString

NSAttributedString *attrString;
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};

NSData *htmlData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:options error:nil];
NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];

Wednesday 7 May 2014

Execute Javascript in iOS Applications [ iOS 7 ]

-(void) evaluateJavscript
{
    NSString * jsCode = @"1+2";
    JSContext *context = [[JSContext alloc] init];
    JSValue * value = [context evaluateScript:jsCode];
    NSLog(@"Output = %d", [value toInt32]);

}
-(void) accessJavscriptVariables
{
    JSContext *context = [[JSContext alloc] init];
    NSString * jsCode = @"var x; x=10;";
    [context evaluateScript:jsCode];

    JSValue * a =context[@"x"];
    NSLog(@"x = %d", [a toInt32]);
}
-(void) accessJavscriptFunctions
{
    //function with arguments
    JSContext *context = [[JSContext alloc] init];
    NSString * jsCode = @"function sum(a,b) { return a+b;} ";
    [context evaluateScript:jsCode];

    JSValue * func =context[@"sum"];
    NSArray * args = @[[NSNumber numberWithInt:10],[NSNumber numberWithInt:20]];
    JSValue * ret =[func callWithArguments:args];
    NSLog(@"10+20 = %d", [ret toInt32]);

    //function without arguments
    NSString * jsCode2 = @"function getRandom() { return parseInt(Math.floor((Math.random()*100)+1));} ";
    [context evaluateScript:jsCode2];

    JSValue * func2 =context[@"getRandom"];

    for(int i=0;i<5;i++)
    {
        JSValue * ret2 =[func2 callWithArguments:nil];
        NSLog(@"Random Value = %d", [ret2 toInt32]);
    }
}
-(void) codeBlocks
{
    JSContext *context = [[JSContext alloc] init];

    //we are telling that "sum" is function, takes two arguments
    context[@"sum"] = ^(int arg1,int arg2)
    {
        return arg1+arg2;
    };

    NSString * jsCode =@"sum(4,5);";
    JSValue * sumVal = [context evaluateScript:jsCode];

    NSLog(@"Sum(4,5) = %d", [sumVal toInt32]);


    //we are telling that "getRandom" is function, does not take any arguments
    context[@"getRandom"] = ^()
    {
        return rand()%100;
    };

    NSString * jsCode2 =@"getRandom();";
    for(int i=0;i<5;i++)
    {
        JSValue * sumVal2 = [context evaluateScript:jsCode2];
        NSLog(@"Random Number = %d", [sumVal2 toInt32]);
    }
}
-(void) jsExportProtocol
{
    JSContext * context = [[JSContext alloc] init];
    NSString * jsCode = @"function sqrtOf(obj){ return MyObject.getSqure(obj);}";

    //adding Objective-C Class to Javascript.
    context[@"MyObject"]=[MyObject class];

    [context evaluateScript:jsCode];

    MyObject * obj =[MyObject new];
    obj.x =10;

    JSValue * func =context[@"sqrtOf"];
    JSValue * val = [func callWithArguments:@[obj]];

    //we got MyObject from javascript.
    MyObject *sqrtObj = [val toObject];

    NSLog(@"Value =%d, %d", obj.x, [sqrtObj getX]);
}

Calling a javascript function located in a WebView

JSContext *context = [theWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];