Reset iptables / Clear / Remove all iptables rules

iptables has many different tables. In order to reset it or remove all rules of it, you need to run a series of commands like below:

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -t raw -F
iptables -t raw -X
iptables -P INPUT ACCEPT

Don’t forget to save your iptables configuration. However, you have another option to do it easily and permanently by running the following command:

> /etc/sysconfig/iptables

And then restart the iptables service. All the rules will go away.

[performSelector:withObject:afterDelay:] for calling C++ functions?

We can’t use C++ functions as selectors in .mm files as I talked before. We can use another way to implement the delay call:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
   // your C++ code here

It would call your C++ code after 5 seconds delay. Still block tricks.

Want to use Global Variable to present instance-related value?

Sometimes we may need to use a global variable to present a value acting like a class member variable, but we couldn’t define it into the class header file (Don’t ask why because cross-platform developers may really have this kind of requirements).

The solution is simple: use a static map as the global variable.

For example, in the mm file:


static map<YourClassName*, bool> isUsingXFeature;


if (shouldUseXFeature())
   isUsingXFeature[this] = true;


if (isUsingXFeature[this]) 

Of course you can define the global variable as unordered_map to go a fancier way.



C++ Version of HttpStatus Source Code

I have converted it from a Java version.

Using C++ method as NSNotificationCenter observer selector

There are many ways to do it. The usual way is to wrap the C++ code inside an Objective-C class.

When sometimes you need to handle NSNotification using C++, it would be simpler. For example, if you want to handle the MPMoviePlayer-finish-playing event, you probably use the following code:

[[NSNotificationCenter defaultCenter] addObserver:???

What should you fill in to the ???s?

You can’t directly convert a C++ method to a selector because they are totally different like a bird and a balloon. However, you can use a block instead of a selector:

[[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification 
usingBlock:^(NSNotification *note) {
     // write any of your C++ code here

Simple. Right.

UIWebView: YouTube video got shifted down a little bit after resizing

Have you met this? When you embed your YouTube video in your iOS App, it should look like this:


The blue area is the other part of your app. You are using YouTube iframe API to show your clip in a perfect size.

However, if you need to resize it smaller, you may get the following result:


The red part indicates the original size of your UIWebView. You’ll see there is an ugly black rectangle at the top of the video. Yes, the whole YouTube video player has got moved down.

I have researched for a whole day to figure out why it happens and how to prevent it, and finally I have got something and a work around.

It should be an iOS UIWebView issue, or in detail, it is an issue of the iOS UIKit controls who contains a CALayer.  If you are using some customized UIView as your OpenGLView, it could happen as well. However, I have found a work around for this issue – embed YouTube player shift issue:

Usually we use the following code to show a YouTube video in our UIWebViews in fullscreen:

<iframe id='playerId' type='text/html' width=100% height=100% src='' >

height=100% would cause the problem. Try using 90% or 95% instead, you’ll find the problem has solved!

However, it isn’t solved perfectly because: a) I don’t want my video player contains a border on the top/bottom; b) The problem may come out again when the player is resized small enough.

To make a perfect work around, we can keep using our height=100% code and add the following javascript:

window.onresize = function() {
  document.getElementById('playerId').style.height = window.innerHeight;

Believe or not, the issue has solved perfectly!


Create a std::function object from a member function

Sometimes, you need to pass a member function as a std::function object. You can’t directly use the member function address. Instead you need to add ‘this’ or other instance with the address of its member function.

There are two ways to do it:

1) Using std::bind

for member functions without parameters, you can do this: (e.g. bool Foo::doSomething() )

std::function<bool(void)> f = std::bind(&Foo::doSomething, this);

for member functions with parameters, you can do this using std::placeholders: (e.g. bool Foo::doSomething(int, int, float) )

std::function<bool(int, int, float)> f = std::bind(&Foo::doSomething, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);

std::placeholders contains _1 to _10. It’s good for normal use.

2) Using Lambda expression

std::function<bool(int, int, float)> f = [&] (int a, int b, float c) ->bool {
  return this->doSomething(a, b, c);

[&] or [=] depends on your context.