Pages

Awesome chrome feature for Developers

Copy network call as cURL in chrome

Some times we try to see how some sites make call to servers to fetch data & we want to simulate to it from other clients e.g. cURL, other http request clients.

So here is little help for developers to copy any network request executed through chrome.

To do this just open developer console of chrome (Menu->More Tools->Developer Tools)
& goto to Network Tab


I am loading freeios8.com , you can see all resources are fetched from server and you can see each network call in network tab as shown below.


After loading website you can select & right click on any network call , It will provide you copying current network call in various formats. e.g. I am selecting here Copy as cURL option, refer below screen shot.


When you copy this call It will be available in clipboard.
You can paste it on command prompt/terminal & get same response as browser from respective servers.
Below is sample copied content with 'Copy as cURL' option.
curl 'http://freeios8.com/' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Connection: keep-alive' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Referer: https://www.google.co.in/' -H 'Accept-Language: en-US,en;q=0.8,hi;q=0.6,da;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36' --compressed
Note : To execute curl command make sure you have curl utility already installed on your system.

Wifi disconnects when you lock OSX screen ? then you should try this.

Consider when you are in middle of downloading something very important & in between you want to lock the screen, But screen lock will disconnect wifi which will stop your important download in progress If you are lucky enough, you can resume that download after re-login but wait what if it is not resumable ? So you have to start over again.

Lets find how we can lock the screen without disconnecting the wifi connection.

We can follow steps
1 . Create 'Screen Saver Service' using Automator 
2 . Bind this service with keyboard shortcut.

Now lets create Screen Saver service using Automator

You can learn some basic about creating simple service in mac Here
Just go to Applications & run Automator.app , choose "Service" as type for document as shown in snap below.




It will create empty workflow for your service. Then select "no input " as option for Service receives...checkbox.



Keep next checkbox option as "any application", Search Screen saver... in search box as shown below. It will filter "Start Screen Saver" Action for you, then add that action in your workflow by dragging it to right side panel.




After adding this action your workflow should look similar to below snap. You can test this workflow by hitting Run button at right upper corner.




Finally save this service with any name as you want. I saved it with name "screen_saver_service".




Then open System Preferences & & goto Keyboard shortcut by following path as
System Preferences->Keyboard->Shortcut Tab->Services option ,
refer below snap





Here you will see  service "screen_saver_service" already listed in General section, you just need to set key combinations to invoke this service as per your liking. Now you are good to go for locking your screen by pressing keys you just set.


Delete unwanted emails from Gmail in One go.

Daily checking emails is good habit but most of us are very lazy to do it or we get lots of mails daily that we can not read/delete such emails on daily basis, due to this we end up owning thousands of unwanted emails in our Inbox, Same case was with me. So I finally found way to bulk delete emails from Gmail inbox & I am sharing same with you.

Follow below steps to get rid of those unwanted emails

  • Login to Gmail ( Not need to say)
  • You will see search bar at top with small arrow at end , as shown in below snap
gmail-search-bar
  • Click that arrow.
  • This will open search/filter form where you can input search criteria, e.g From, to, Subject of mail, Has attachment, Date within fields... etc
gmail-filter-form

  •  Now you can add email id (even you can use part of email address here) in Form field to search all emails matching that criteria. you can refer following example where I am searching all mails from @parsemailer.com and date within "1 year". After you all criteria inputs just hit search button.


gmail-filter-form




  •  When Gmail done searching for your filter, It will show all emails matching your input criteria. 

gmail-filter-selection

  • If you have results more than single page you have to select all the results before performing any action on them, To do this just go to menu,which is just below search bar. It will show options which you can apply on results. As we want to delete all filtered results, just go for "All", It will select all results on the first page. Gmail assist here by displaying message like All 50 conversations on this page are selected. Select all conversations that match this search So you can select all filtered results at same time and do action on all items in one go. You can follow below snap for doing this.

gmail-filter-results


  • When you click Select all conversations that match this search  link all result items will get selected Then you can perform your action (delete, archive...etc) on these items. At this stage you can perform delete those items and free your Inbox.




Hope this will help to clean your inbox a bit ...


Easiest way to Load HTML table in Google spreadsheet

We most of the time try to fetch data from some websites and store in google spread sheet , To do this we spend lots of time to scraping webpages & digging required data in them.

But one of the easiest way to load  HTML table from any webpage to use Google spreadsheet Import feature ( Google is very good at such things !)

To scrap such data I have spent lots of trying writing some scrips in multiple programming languages.But today I am going to tell you easiest way to do it.

Google Docs being able to import tables from websites. And indeed, it has a very useful function called ImportHtml that will scrape a table from a page. Also there are many other options are provided for importing data from live URLs. When you start writing Import... In first cell of your spreadsheet it will show you all options for importing various data as shown below


Importing HTML in Google spreadsheet snap


To extract a table, create a new spreadsheet and enter the following expression in the top left
=ImportHtml(URL, "table", num)
URL here is the URL of the page (between quotation marks), “table” is the element to look for (Google Docs can also import lists), and num is the number of the element (e.g. if there are two tables on same page you can load 2nd table using s as num value), in case there are more on the same page (which is rather common for tables). In our example I am loading table from page "http://www.w3schools.com/html/html_tables.asp".

So I write something like =ImportHtml("http://www.w3schools.com/html/html_tables.asp", "table", 1)  and hit enter to import data.




Once this is done, Google Docs retrieves the data and inserts it into the spreadsheet, including the headers. The last step is to download the spreadsheet as a CSV file.



This is very simple and quick, and a much better idea than writing a custom script. Of course, the real solution would be to offer all data as a CSV file in addition to the table to begin with. But until that happens, we will need tools like this to get the data into a format that is actually useful.

You can follow THIS to know more.

Integrating Static Library written in Objective C to Swift iOS Project

I was looking for integrating my one of the Static library written in Objective-C into new swift project  & you know what I got very little information on it. I struggled alot to make my things working with Objc+swift integration for my project.

Here I will guide you through such integration

1. Suppose you have some static Library XYZ written in Objective-C

Add that library as subproject into your target project as descried HERE

Then we have to create Bridge-header which will make swift code to access Objective C part of your library.

You can just follow
Xocde -> New -> File
there you need to add header file (from iOS/OS X section)
be carefull whilte naming this file. this file should have name like


<product_module_name>-Bridging-Header.h : Default Product module name is project name itself.

you can read more about "Product module name" at Apple Docs


Now add import statements in <product_module_name>-Bridging-Header.h for accessing Native SDK Classes


e.g.

If you have Project with name "sampleSwift" then your bridge file should look like


Background Pattern image to SKScene

Today we are going to see how we can add background pattern image to SKScene

Once, there was  requirement for one of project to set pattern image for SKScene background I tried with backgroundColor property of SKScene like


But it won't work.

So I wrote category to overcome this. Using this category of SKScene you can easily set background  pattern to SKScene without any pain.


Want to bypass bandwidth Caps ?

Unable to stream youtube without buffering due to bandwidth cap on youtube inside your Organisation ? want to boost the speed for youtube or any website which is under Bandwidth cap you should try dotVpn


You can download it from chrome webstore, Use it as chrome extension.

Before login


You need to create an account before you can start using the service. To do so, you are asked to enter your name, email address and password into the form in the Chrome browser.

After login


The functionality becomes available right afterwards. A click on the icon displays various information about the currently selected remote server including its location.

You can use the menu to switch to another server that you want to connect to instead. A total of nine different locations are supported right now including the USA, Germany, UK, France, Japan and Russia.
Also you can set country in this extension so you can stream from sites like hulu,netflix..etc

Give it try !
Note: I am not connected with dotVpn by any means 

Hosting file on Google drive

Google drive provides lots of storage space for storing your personal stuff at free of cost ...& Google provides not just few MBs it allows free storage up to 15GB (can you believe it ?)
You can meet google driver here - GoogleDrive
I am going to share pretty trick for hosting your HTML pages on google drive.

Below steps will guide you throgh

1 . Login to google drive with your google account, You will land up on screen similar to below screen
Then for hosting your content you can create folder (In my case its 'webDocs') as shown below


2 . Go inside newly created folder & upload your file in it (It can contain javascript as well  ) Now move back to your folder.


3 . Right click on the folder and select Share option 


4 . You will given following option for sharing, you go with more option.




5 . Then select "On - Public on the web" option and click save button. (you are almost done).


6 . Now you will have link from google drive for sharing. You just copy it as of now and store somewhere.


7  . Convert saved URL in to URL as given below 
www.googledrive.com/host/[doc id]
E.g. Id https://drive.google.com/folderview?id=<File_ID>&usp=sharing
will be converted to
https://www.googledrive.com/host/<File_ID>/
To get direct link to file just add file name at end of created URL as.
https://www.googledrive.com/host/<File_ID>/<Fille_Name> just hit it.

& you are done !



I have hosted file -bouncing-ball.html in webDocs folder in side google drive. You can have look at it and it contains some javascript also to show how you can play with this feature and create your own one page javascript application & host then on most reliable google servers using google drive.

keepRocking

Difference in strong and weak pointers in ObjectiveC

Before writting this, I assume you know basic knowledge of ObjectiveC and its well known topic of discussion Memory Management If you do not know you can follow tutorials on ObectiveC &  ObjectiveC Memory Management, Here I am going to explain how strong and weak pointers are different in ObjectiveC with some code example.

Consider Sample classes TechClass & MyClass explain, one class have 2 ivars which which points to object of MyClass with strong and weak reference viz. strogPntr & weakPntr as shown below


1
2
3
4
5
6
7
8
@interface MyClass()
@end

@interface TechClass(){
    
    MyClass * strogPntr;
    __weak MyClass * weakPntr;;
}

Now we create MyClass object and assign it to strogPntr. same object is pointed by weakPntr. Now if we assign nil to weakPntr it will not deallocate that object because still there is strong pointer referring to that pointer.

1
2
3
 strogPntr = [MyClass new];
 weakPntr = strogPntr;
 weakPntr = nil;

Pictorial representation of above code is given below
weak-pointer-nil

Lets see whats happens when we try other way around i.e, assigning nil strogPntr. what do you expect here? will object get deallocated or keeps on hanging with weakPntr  pointer ???
Correct answer is object will be deallocated because object do not have any strong pointer pointer to created object of MyClass also due to this now weakPntr will automatically point to nil.

1
2
3
 strogPntr = [MyClass new];
 weakPntr = strogPntr;
 strogPntr = nil;
Pictorial representation of above code is given below


Now onwards keep in mind  that if you want to persistent object inside any viewcontoller/custom object never ever nil all out strong pointer to object else the object will be revoked by system even you have tones of weak pointers to the object


keepRocking

Avoid Strong Reference Cycles when Capturing self

Blocks in objective C are really awesome...but...

If you need to capture self in a block, such as when defining a callback block, it’s important to consider the memory management implications.

Blocks maintain strong references to any captured objects, including self, which means that it’s easy to end up with a strong reference cycle if, for example, an object maintains a copy property for a block that captures self:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@interface TechClass : NSObject
@property (copy) void (^block)(void);
@end

@implementation TechClass
- (void)configureBlock {
    self.block = ^{
        [self doWork];    // capturing a strong reference to self
                               // creates a strong reference cycle
    };
}
@end

To avoid this retain cycle we have to do changes as below 


1
2
3
4
5
6
7
- (void)configureBlock {
    TechClass * __weak weakSelf = self;
    self.block = ^{
        [weakSelf doWork];   // capture the weak reference
                                  // to avoid the reference cycle
    }
}

This change makes sure that retain cycle is not created for self.
You can check all these scenario in sample code @ Sample Code

Writing String manipulation function for 'C' to understand (readonly/readwrite) string literals

We saw how char * and char [] are differently stored in memory in previous post
Now lets try to write simple string manipulation function. I will not write complex code as such this code will be more for writing safe functions which will do simple functionality without breaking the code.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<string.h>
char* trim(char *string){
    
    int len = strlen(string);
    
    for(int i=0;i<len;i++){
        
        if(string[i]==' '){
            string[i]='*';
        }
    }
    return string;
}

int main(){
    
    char * string ="hello there";
    printf("%s",trim(string));
    return 0;
    
}

Can you guess what will be output ??
Its is 'Bus error: 10' (Read here & here on this)
wondering how! If you have understood are-char-str-char-str-same.html? then answer to this question is simple. string variable is stored in read-only memory.
If we change '* string' to 'string[]' like shown below

1
2
3
4
5
6
7
int main(){

char string[] ="hello there";
printf("%s",trim(string));
return 0;

}
Above code will behave as expected and will give output
'hello*there'
So what if you want to write such string manipulation functions of you own but if user of this function provide string literal from readonly memory you can not change that. what can be good way to achieve what we want without modifying readonly memory.
Lets modify some code in trim() function like below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
char* trim(char *string){

  int len = strlen(string);
  char * iString  = (char*)calloc(len*sizeof(char)+1,0);

 iString[len]='\0';
 for(int i=0;i<len;i++){

   if(string[i]==' '){
    iString[i]='*';
   }else{
    iString[i]=string[i];
   }
 }
 return iString;
}

Now this method will be capable of handling char* / char[] input string with ease.
Note : User of trim() function should be responsible for free() ing memory (allocated to iString).

Are char * str & char str[] same ?

Lets have look at one of the bothering subject of 'C' to newbie programmers. Yes , You guessed it right it is POINTERS. nightmare of Computer science students.
(Here is interesting(for me at least ) discussion on Pointer's evilness)


First lets see basic definition of pointer -

pointer is a variable which contains the address in memory of another variable

Let's visualise memory location and pointer as above. You can see MemAd007 is valid memory location having some data value. Now there is another memory location labeled as pointer and having value as MemAd007 .
What is difference between pointer & MemAd007 memory location ?
Answer is pointer contain memory address and other data( its can be int, float...any data)
Lets consider sample code in C


1
2
3
4
5
6
7
8
#include<stdio.h>

int main(){

   char * str ="hello";
   char  s[10]="hi" ;
   return 0;
}

Now time for making hands dirty with assembly code ....
You can get assembly code from c using
#>gcc -O2 -S -c <program_name>
But instead I found website where we can get more informative assembly code from C.
Below is assembly code for above program. (You can use this tool to do it online - http://assembly.ynh.io/)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
              .Ltext0:
               .section .rodata
              .LC0:
0000 68656C6C   .string "hello"
     6F00
               .text
               .globl main
              main:
              .LFB0:
               .cfi_startproc
0000 55         pushq %rbp
               .cfi_def_cfa_offset 16
               .cfi_offset 6, -16
0001 4889E5     movq %rsp, %rbp
               .cfi_def_cfa_register 6
0004 4883EC30   subq $48, %rsp
0008 64488B04   movq %fs:40, %rax
     25280000 
     00
0011 488945F8   movq %rax, -8(%rbp)
0015 31C0       xorl %eax, %eax
0017 48C745D8   movq $.LC0, -40(%rbp)
     00000000 
001f 48C745E0   movq $26984, -32(%rbp)
     68690000 
0027 66C745E8   movw $0, -24(%rbp)
     0000
002d B8000000   movl $0, %eax
     00
0032 488B55F8   movq -8(%rbp), %rdx
0036 64483314   xorq %fs:40, %rdx
     25280000 
     00
003f 7405       je .L3
0041 E8000000   call __stack_chk_fail
     00
              .L3:
0046 C9         leave
               .cfi_def_cfa 7, 8
0047 C3         ret
               .cfi_endproc
              .LFE0:
              .Letext0:

You can see here there is section called .rodata which stores value "hello" as it is. You can read more about this data section @ http://www.bravegnu.org/gnu-eprog/c-startup.html.
So its clear that s, and str are stored differently in memory itself. As "hello" stored in .rodata section, which is read only section hence we can not modify contents of 'str' but we can modify contents of 's' easily.
Hope this small experiment clears you doubt.

Want to write C/C++ code on MAC ??


There may be time when you want to write some C/C++ programs on Mac machine.But you wonder how can you write such programs on MAC. Most of you might have known that Mac machines comes with preinstalled C,C++,python ...etc.
you can use terminal to write C/C++ programs., but wait do you like really writing programs in terminal (vim/ nano - text editors) which are very uncomfortable for newbies.

So ....

which is best IDE provided in Mac to write C/C++ code with great suggestions while writing your pretty code ?

Guys the answer is Xcode. is you heard it write it is not just for developing iOS/Mac application. you can just explore into Xcode to find out new project option in Xcode which allows us to create Command line tool in C/C++...


Here are snaps for doing stuff

1. Open Xcode (You can download it from Xcode download ▼ )

2 . Go to File - > new -> Project



3 . Click on project -> Select Application in OS X section -> Select Command Line Tool



4 . This will create new project add product name and other information. Here you can select your       choice of programming language (It includes C/C++)


5 . When you put all information and hit Next button you will land to project window just created by you.
NOW the time to run your hello world :)




How to find directory in linux based environment using 'find' command

Some time we are using terminal and we need to search some files at same time. So Lets see how we can find any files/folder from terminal

There is one good command to find files on *nix based systems. (Apple machines are not exception to this :) )

We can divide find command syntax as below

Syntax :
find /where/to/look/up criteria action

Suppose you want to find item named 'myworkspace' in your user directoty, you can write find command as
find ~ -name "myworkspace"
In above line '~' represents current user home directory
This command will search and print all items (files/folders) matching 'myworkspace'

Goind further you can also add much more restrition for find , you can ask find command to list only files or folders using 'type' switch.
find ~ -type f -name "sample"  (This will find files only)
find ~ -type d -name "sample"  (This will find folders only)
f stands for File
d stands for Directory 

For more details you can head towards - http://content.hccfl.edu/pollock/Unix/FindCmd.htm !

Simple pattern matching in C

Following code snippet explains simple pattern matching program with modular functions