2015-08-07


For a fresh of taste I've installed Mac OS X El Captain(10.11) beta on my Macbook. Everything is fine until I want to upgrade my PHP installation via homebrew.

The result is:

> brew install php56
Warning: You are using OS X 10.11.
We do not provide support for this pre-release version.
You may encounter build failures or other breakage.
==> Installing php56 from homebrew/homebrew-php
==> Downloading https://php.net/get/php-5.6.11.tar.bz2/from/this/mirror
Already downloaded: /Library/Caches/Homebrew/php56-5.6.11
==> ./configure --prefix=/usr/local/Cellar/php56/5.6.11_2 --localstatedir=/usr/local/var --sysconfdir=/usr/local/etc
==> make
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sapi/cgi/php-cgi] Error 1
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libs/libphp5.bundle] Error 1

READ THIS: https://git.io/brew-troubleshooting
If reporting this issue please do so at (not Homebrew/homebrew):
  https://github.com/homebrew/homebrew-php/issues

Warning: You are using OS X 10.11.
We do not provide support for this pre-release version.
You may encounter build failures or other breakage.

According to the warning, that is not avoidable with build failure. ;-)

BUT, the problem can be solved with the power of google. :)

---

Open the File /usr/local/Library/Taps/homebrew/homebrew-php/Abstract/abstract-php.rb line 362, replace:
s.change_make_var! "EXTRA_LIBS", "\\1 -lstdc++"
to
s.change_make_var! "EXTRA_LIBS", "\\1 -lstdc++ /usr/local/opt/openssl/lib/libssl.dylib /usr/local/opt/openssl/lib/libcrypto.dylib"

then try to install the php via homebrew again.

via: https://github.com/Homebrew/homebrew-php/issues/1941

GSON makes the conversion JSON to POJO very easily


As a java developer one should know JSONObject very well. On the other hand, if one depends so heavily on JSONObject, the conversion from JSON to POJO becomes sometimes a burden if the structure of JSON string is very complex. One has to write so many codes to call JSONObject.getString("") and so on. Thanks GSON, there is a better way to convert JSON to java object.

 Define a java class to be an exact model of the JSON string like: 
{"book":{"translations":[{"translatedText":"Bonjour tout le monde"}]}} 
The java class:
class BookWrapper {
    public Book book;

    public static BookWrapper fromJson(String s) {
        return new Gson().fromJson(s, DataWrapper.class);
    }
    public String toString() {
        return new Gson().toJson(this);
    }
}
class Book {
    public List<Translation> translations;
}
class Translation { 
    public String translatedText;
}
As you can see, it's very convenient and elegant to use BookWrapper.fromJson(JsonString) to convert a JSON string to the java object.
Furthermore, GSON supports the versioning of a java object. It's helpful when the JSON structure has been changed with some versions.

Usage taken from the GSON user guide:

Versioning Support

Multiple versions of the same object can be maintained by using @Since annotation. This annotation can be used on Classes, Fields and, in a future release, Methods.  In order to leverage this feature, you must configure your Gson instance to ignore any field/object that is greater than some version number.  If no version is set on the Gson instance then it will serialize and deserialize all fields and classes regardless of the version.

public class VersionedClass {
  @Since(1.1) private final String newerField;
  
@Since(1.0) private final String newField;
  private final String field;

  public VersionedClass() {
    this.newerField = "newer";
    this.newField = "new";
    this.field = "old";
  }
}

VersionedClass versionedObject = new VersionedClass();
Gson gson = new GsonBuilder().setVersion(1.0).create();

String jsonOutput = gson.toJson(someObject);
System.out.println(jsonOutput);
System.out.println();gson = new Gson();
jsonOutput = gson.toJson(someObject);
System.out.println(jsonOutput);


======== OUTPUT ========
{"newField":"new","field":"old"}
{"newerField":"newer","newField":"new","field":"old"}


2014-05-15

FIX: Drush can't update Drupal to 7.28

Recently Drupal 7.28 is released. Since I've setup the update notification on some sites, then I've got so many emails from my drupal sites. As usual, I go to the web server, run the command "drush up" to upgrade the core and the modules. Everything looks fine as following:

 Name    Installed Version  Proposed version  Message  
 Drupal  7.27               7.28              Updates available


Update information last refreshed: Thursday, 05/15/2014 - 09:30
Code updates will be made to drupal core.
WARNING:  Updating core will discard any modifications made to Drupal core files, most noteworthy among these are .htaccess and robots.txt.  If you have made any modifications to these files, please back them up before updating so that you can re-create your modifications in the updated version of the file.
Note: Updating core can potentially break your site. It is NOT recommended to update production sites without prior testing.

Do you really want to continue? (y/n): y
Project drupal was updated successfully. Installed version is now 7.28.
Backups were saved into the directory /home/ubuntu/drush-backups/drupal/20140515073230/drupal.                                                                                                                                                                       [ok]
No database updates required                                                                                                                                                                                                                                         [success]
'all' cache was cleared.                                                                                                                                                                                                                                             [success]
Finished performing updates.                                                                                                                                                                                                                                         [ok]

So I supposed the core is updated to 7.28. BUT, the update notification emails come again and again. Then I ran "crush st" to check the status of the drupal installation, it showed:

$ drush st
 Drupal version                  :  7.27

Unbelievable, right? Drush cheats me on that ;)

So it seems a manual update is unavoidable, but I want to give Drush a second chance. So here are the command set to update drupal core to 7.28.

cd /tmp

drush dl

sudo rsync -rltD --delete --exclude 'sites' /tmp//drupal-7.28/ .

cd /path/to/web/root

drush updatedb

drush pm-update

Now I get a right upgraded Drual with 7.28.

$ drush st
 Drupal version                  :  7.28

2014-05-05

HOWTO: Make Google Charts SVG responsible and printable with full page width

Google Charts is widely used for a while. It's no doubt that it is easy to use and have great documentation. But, it has also many limitations. One problem I recently discovered is printing. Since the SVG that is generated by Google Charts is NOT responsible. If the user wants to print the page, the result only shows part of the charts since the Google Charts adds a CSS style like "overflow:hidden" to the SVG.

What I want is very simple, the user gets a printed result like WYSIWYG. After different approaches, I came to the idea of Image Replacement. 

The idea: since the generated SVG doesn't response to width changes, we have to use a responsive image instead of the SVG to get the perfect print result.

So here is the implementation. :)

HTML:
<div id='timeline'></div>
<div id='timeline_print'></div>

CSS:
@media print {
    #timeline_print {
        width: 100%;
    }
}

JavaScript(assumed that jQuery is loaded, this.chart refers to the Google Charts object):

var beforePrint = function() {
    $("#timeline_print").html('<img src="' + this.chart.getImageURI() + '">');
    $("#timeline").addClass('hidden');
};
var afterPrint = function() {
    $("#timeline_print").empty();
    $("#timeline").removeClass('hidden');
};
if (window.matchMedia) {
    var mediaQueryList = window.matchMedia('print');
    mediaQueryList.addListener(function(mql) {
        if (mql.matches) {
            beforePrint();
        } else {
            afterPrint();
        }
    });
}

window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;

2013-10-16

FIX: Unable to download data from https://rubygems.org/ - certificate verify failed

I've used rvm for a while to manage my ruby installations and it works great. Today I've tried updating my gems with gem update and it tells me "nothing is to be updated". Eh? At least I know cocoapods is updated from my installed 0.24 to 0.26. What happened?

Then I tried to update gems manually. Firstly:

> gem install cocoapods
ERROR:  Could not find a valid gem 'cocoapods' (>= 0), here is why:
          Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://s3.amazonaws.com/production.s3.rubygems.org/latest_specs.4.8.gz)

 Great! That's the reason. There is a long long list of the issue under https://github.com/rubygems/rubygems/issues/515

The solution suggested are:
1. use an insecure connection to rubygems.org with:

> gem update  --source http://rubygems.org

It works. But it doesn't solve the problem

2. RVM should update its certificates:


> rvm osx-ssl-certs status all
Certificates for /etc/openssl/cert.pem: Old.


> rvm osx-ssl-certs update all
Updating certificates for /etc/openssl/cert.pem: Updating certificates in '/etc/openssl/cert.pem'.
mkdir: /etc/openssl: Permission denied
Admin password required for 'mkdir -p /etc/openssl':
Updated.

Then everything should be fine now.

A detailed information can be found at http://railsapps.github.io/openssl-certificate-verify-failed.html

For non-RVM and non-MAC users, here is the answer: http://stackoverflow.com/questions/19150017/ssl-error-when-installing-rubygems-unable-to-pull-data-from-https-rubygems-o

2013-10-14

FIX: A blank view appears after QLPreviewController dismissed in iOS 7

Recently when I am working with iOS 7, I encounter the following problem: If the modalPresentationStyle of an view controller is set as UIModalPresentationFormSheet or UIModalPresentationPageSheet and after one uses the QLPreviewController once in the view controller and dismisses the Quick Look, a blank / gray view will appear on the screen instead of the presented view controller. This blank view cannot be dismissed or interacted.

That seems a serious bug in iOS 7. :(

After some days trying errors, I've found the following workaround:

in the presenting ViewController:

if ([self DeviceSystemMajorVersion] >= 7) {
modalVC.transitioningDelegate = self;
modalVC.modalPresentationStyle = UIModalPresentationCustom;
} else {
modalVC.modalPresentationStyle = UIModalPresentationFormSheet;
}


The function to detect iOS 7:

- (NSUInteger) DeviceSystemMajorVersion {
    static NSUInteger _deviceSystemMajorVersion = -1;
    static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue];
});
return _deviceSystemMajorVersion;
}


The presenting view controller should conform to the protocol UIViewControllerAnimatedTransitioning and UIViewControllerTransitioningDelegate, in order to use the custom transaction style that indroduced in iOS 7. Continue to add these methods:

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source {
return self;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
UIView *inView = [transitionContext containerView];
    UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
    UIView *fromView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
UIView *preferenceOverlay = [[UIView alloc] initWithFrame:inView.frame];
preferenceOverlay.backgroundColor = [UIColor blackColor];
preferenceOverlay.alpha = 0;
[inView addSubview:preferenceOverlay];
toView.frame = CGRectMake(0, 0, 540, 540);
toView.center = CGPointMake(CGRectGetMidX(inView.frame), CGRectGetMidY(inView.frame));
[inView addSubview:toView];
    [UIView animateKeyframesWithDuration:(1.0) delay:0 options:0 animations:^{
preferenceOverlay.alpha = 0.4;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:finished];
    }];
}


Then in the presented view controller:

-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
if ([self DeviceSystemMajorVersion] >= 7) {
if (QLOpened) {
self.view.frame = CGRectMake(0, 0, 540, 540);
CGFloat xCenter = CGRectGetMidX(self.view.superview.frame) + 125;
CGFloat yCenter = CGRectGetMidY(self.view.superview.frame) - 125;
self.view.center = CGPointMake(xCenter, yCenter);
NSLog(@"Center X:%f Y:%f",xCenter,yCenter);
}
}
}


Then let the view controller to conform the protocol QLPreviewControllerDelegate and add the following method:
- (void)previewControllerWillDismiss:(QLPreviewController *)controller {
NSLog(@"QLView dismissed");
[self.view setNeedsLayout];
}


DONE!

If you have the centering problem of the modal view controller, try fine-tuning the center point of the method viewWillLayoutSubviews

2013-09-27

HOWTO: Compile Sigil under Mac OS X from source codes

Sigil is a very powerful ePub file publisher, which can be used to create, modify and edit ebooks. Since the source codes of Sigil is put on GitHub(https://github.com/user-none/Sigil/), I have to compile Sigil directly from source codes. Now here is the HOWTO。 :)

Firstly, you need brew(http://brew.sh) to let it install different packages for compiling.

Once brew is installed, you should install the following packages using terminal:

> brew install cmake
> brew install qt5

> export Qt5_DIR=/usr/local/opt/qt5

Check out the source codes from GitHub or download the ZIP . Go to the directory in Terminal and execute the following commands:

> mkdir build
> cd build
> cmake -DFORCE_BUNDLED_COPIES=1 -DCMAKE_BUILD_TYPE=Release ..
> make

At the end in the folder "bin" is the compiled Sigil.app, which can be dragged into Application folder.

DONE!

2013-08-28

FIX: Running PEAR, PECL & Co. under Mac OS X Mavericks DP6

When I tried to use pear under Mac OS X Mavericks DP6, I got a very unhelpful error message:

$ pecl
Could not open input file: /usr/lib/php/peclcmd.php

It seems that pear & pecl are not preinstalled! No problem, at least Mavericks provides an install package.

sudo php -d detect_unicode=0 /usr/lib/php/install-pear-nozlib.phar

Then the nightmare began:

$ sudo pecl install uploadprogress
downloading uploadprogress-1.0.3.1.tgz ...
Starting to download uploadprogress-1.0.3.1.tgz (9,040 bytes)
.....done: 9,040 bytes
4 source files, building
running: phpize
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
building in /private/tmp/pear/temp/pear-build-rootTuNmJz/uploadprogress-1.0.3.1
running: /private/tmp/pear/temp/uploadprogress/configure
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... i386-apple-darwin13.0.0
checking host system type... i386-apple-darwin13.0.0
checking target system type... i386-apple-darwin13.0.0
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib/php/extensions/no-debug-non-zts-20100525
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking whether to enable uploadprogress functions... yes, shared
checking for ld used by cc... /Applications/Xcode5-DP6.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
checking if the linker (/Applications/Xcode5-DP6.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) is GNU ld... no
checking for /Applications/Xcode5-DP6.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 196608
checking command to parse /usr/bin/nm output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking for dsymutil... dsymutil
checking for nmedit... nmedit
checking for -single_module linker flag... yes
checking for -exported_symbols_list linker flag... yes
checking if cc supports -fno-rtti -fno-exceptions... yes
checking for cc option to produce PIC... -fno-common
checking if cc PIC flag -fno-common works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/Applications/Xcode5-DP6.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... darwin13.0.0 dyld
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
running: make
/bin/sh /private/tmp/pear/temp/pear-build-rootTuNmJz/uploadprogress-1.0.3.1/libtool --mode=compile cc  -I. -I/private/tmp/pear/temp/uploadprogress -DPHP_ATOM_INC -I/private/tmp/pear/temp/pear-build-rootTuNmJz/uploadprogress-1.0.3.1/include -I/private/tmp/pear/temp/pear-build-rootTuNmJz/uploadprogress-1.0.3.1/main -I/private/tmp/pear/temp/uploadprogress -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /private/tmp/pear/temp/uploadprogress/uploadprogress.c -o uploadprogress.lo
mkdir .libs
 cc -I. -I/private/tmp/pear/temp/uploadprogress -DPHP_ATOM_INC -I/private/tmp/pear/temp/pear-build-rootTuNmJz/uploadprogress-1.0.3.1/include -I/private/tmp/pear/temp/pear-build-rootTuNmJz/uploadprogress-1.0.3.1/main -I/private/tmp/pear/temp/uploadprogress -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /private/tmp/pear/temp/uploadprogress/uploadprogress.c  -fno-common -DPIC -o .libs/uploadprogress.o
In file included from /private/tmp/pear/temp/uploadprogress/uploadprogress.c:23:
/private/tmp/pear/temp/uploadprogress/php_uploadprogress.h:34:10: fatal error: 'php.h' file not found
#include <php.h>
         ^
1 error generated.
make: *** [uploadprogress.lo] Error 1

ERROR: `make' failed

No way! The /usr/include doesn't exist for phpize....Hours of trying error and searching... Thanks a post from Homebrew community, I got that problem fixed through:

sudo ln -s /Applications/Xcode5-DP6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/ /usr/include

2013-08-16

FIX: Errors with installing MySQL on Ubuntu

In order to install a LNMP environment on a VPS, I've tried the following command to install MySQL server, but what I got is:


130816 18:53:17  InnoDB: Waiting for the background threads to start
130816 18:53:18 InnoDB: 5.5.32 started; log sequence number 1595675
130816 18:53:18  InnoDB: Starting shutdown...
130816 18:53:19  InnoDB: Shutdown completed; log sequence number 1595675
start: Job failed to start
invoke-rc.d: initscript mysql, action "start" failed.
dpkg: error processing mysql-server-5.5 (--configure):
 subprocess installed post-installation script returned error exit status 1
No apport report written because MaxReports is reached already
                                                              Processing triggers for ureadahead ...
Errors were encountered while processing:
 mysql-server-5.5
E: Sub-process /usr/bin/dpkg returned an error code (1)

It's really an error message that says nothing. The ubuntu is freshly installed. After hours of trying with solutions from Internet, I've found the following solution working for me:


sudo apt-get clean

sudo apt-get autoclean

sudo apt-get remove --purge mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server mysql-server-5.5 mysql-server-core-5.5

sudo apt-get install mysql-server




before reinstalling, you may need also the following commands:


rm -rf /var/lib/mysql
rm -rf /etc/mysql*



So it is.

2013-08-15

FIX: Xcode failed to get the task for process XXX

Recently by debugging an app on devices, Xcode keeps throwing the error message "Failed to get the task for process XXX" like this:

enter image description here

After long searching on the internet, one solution has saved my day:

It turns out that using a different provisioning profile (one with a wildcard rather than one without) solved this issue.

So I changed the provisioning profile by debugging from app specified to ios team provisioning profile. BRAVO,  now it works like a charm.

via StackOverflow

2013-08-13

Workaround for "AssertMacros: queueEntry, file: /SourceCache/IOKitUser_Sim/IOKitUser-920.1.11/hid.subproj/IOHIDEventQueue.c, line: 512"

I am busy recently with testing some apps under iOS 7 Beta 5. Now while running the app, the debug console keeps outputting some useless information like:

AssertMacros: queueEntry, file: /SourceCache/IOKitUser_Sim/IOKitUser-920.1.11/hid.subproj/IOHIDEventQueue.c, line: 512

 It is really annoying and makes the debug info useless, since it appears every time when I touch the screen. Thanks 0xced, now here is a workaround solution for that:


Here is a workaround to this annoying problem.

  1. Download fishhook
  2. Copy fishhook.h and fishhook.c in your project
  3. Compile this file (QuietAssertMacros.c) in your project:



#include <dlfcn.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "fishhook.h"

int slient_fprintf(FILE * restrict stream, const char * restrict format, ...)
{
    if (strncmp(format, "AssertMacros:", 13) == 0)
    {
        return 0;
    }
    else
    {
        va_list args;
        va_start(args, format);
        int result = vfprintf(stream, format, args);
        va_end(args);
        return result;
    }
}

__attribute__((constructor)) void QuietAssertMacros(void)
{
    rebind_symbols((struct rebinding[1]){{"fprintf", slient_fprintf}}, 1);
}
Unfortunately, you will have to repeat these steps for every project you are working on.

via devforum apple

2013-07-30

MySQL Error 1130 "Host 'xx.xx.xx.xx' is not allowed to connect to this MySQL server."

When I tried to connect a mysql server from outside today morning, I got a mysql error like:

ERROR 1130: Host 'xx.xx.xx.xx' is not allowed to connect to this MySQL server.


That means the mysql server is configured to allow only local connections. The configuration can be modified easily in mysql tables.

Under terminal, type the following commands

> mysql -u root -pmypassword

mysql> use mysql;

mysql> update user set host = '%' where user = 'root';

mysql> flush privileges;



Now you could connect the mysql server from the outside client.

2013-06-26

Strange Behavior: UIButton Text will be not shown.

Because of a multilingual project, all the texts should be translated and replaced with the correct language version. Everything was fine until I found that a button won't show the text assigned.

The code:


 [button titleLabel].text = @"I won't be changed";


The result is: the button shows always the text that is set up in the storyboard. If I delete the text in Storyboard, it will even disappear when I tap!

The solution is also simple:


[button setTitle:@"I will change" forState:UIControlStateNormal]


via StackOverflow

2013-06-17

One Class for One Problem

The keyboard on iOS devices is sometimes a nightmare for a programmer.  Take the UITextField as an example, when a keyboard appears, it will cover most screen area and let the text fields hidden in the back of the soft keyboard. A solution provided by Apple is using the UIScrollView, but there are a lot of mathematical calculation for a programmer. :(

After a while research on the internet, I've found that Michael Tyson has built a drop-in solution for such kind of problems. He packs everything that one needs into a class and puts the sources on GitHub.

If you follow the instruction on the Github, the task with UITextField and Keyboard is easy as a cake!

Usage

For use with UITableViewController classes, drop TPKeyboardAvoidingTableView.m and TPKeyboardAvoidingTableView.h into your project, and make your UITableView a TPKeyboardAvoidingTableView in the xib. If you're not using a xib with your controller, I know of no easy way to make its UITableView a custom class: The path of least resistance is to create a xib for it.
For non-UITableViewControllers, drop the TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h source files into your project, pop a UIScrollView into your view controller's xib, set the scroll view's class toTPKeyboardAvoidingScrollView, and put all your controls within that scroll view. You can also create it programmatically, without using a xib - just use the TPKeyboardAvoidingScrollView as your top-level view.


Well done, Michael!

via ATasty Pixel

HOWTO: UIScrollView scroll to bottom programmatically

The autolayout function of a view controller is very useful. The UI elements can find their position after a rotation automatically. Except for UIScollerview...Orz

The reason is: The Layout in Storyboard is normally designed for portrait, not for landscape. So if an user rotates his iOS devices, the UIScrollView cannot resize its content height automatically. So a programmatical resize is needed. BUT, there is another way: just scroll the UIScrollView to the bottom(or where you want), the content will be presented in landscape correctly like in portait.

Here is the solution: Adding the following code to the view controller where the scroll view is.



-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// Scroll to the bottom
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:YES];
}

via StackOverflow

2012-08-21

iOS: nested push animation can result in corrupted navigation bar


Today I got a very strange error by running an iOS App:

1. nested push animation can result in corrupted navigation bar
2. Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

It happens when the view controller tries to pop to top view controller without waiting for the loading end of child view controller.

Solution:

1. try to set a boolean variable to mark the view controller should pop to the top view controller
2. in viewDidAppear:(BOOL)animated method, check if a call of [self.navigationController popToRootViewControllerAnimated:YES] is needed

The reason:

popToRootViewControllerAnimated() must be called after the view appears!

2012-08-06

Fixing double entries in "Open with" menu under Mac OS X

Sometimes it is very annoying to see duplicated entries in the "Open with..." menu under MAC OS X, the reason is just the database for file types doesn't refresh for some reason. Now it can be fixed using:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

Tested under Mac OS X 10.8 (Mountain Lion).

2011-12-22

Fix the cydia crash problem after jailbreak iOS 5 with redsn0w 0.9.9b9

It is really weird. After using redsn0w 0.9.9b9 to jailbreak my iPhone 3GS with iOS 5.0.1, the iphone can be booted with the option "just boot" in redsn0w. The Cydia icon showes up on the screen. Open it and say I am a "User". The loading process can be seen, and... quit. I can ssh into the iOS, that is to say, the iphone is already jailbroken. But what happened with Cydia?!

I've tried many different methods.

1. restore the stock firmware and do the jailbreak again. -> No success.
2. reinstall Cydia with SSH on the iPhone. -> No success.
3. just boot the iphone tetherly again and again -> No success.


So I tried to read the log file of Cydia. The log file can be found at /private/var/tmp/cydia.log. I noticed that an error is repeated again and again.


2011-12-22 14:43:20.135 MobileCydia[252:e07] Setting Language: de_DE
Assertion failed: (dictionary->lockFD != -1), function IDXUserDictionaryOpen, file /SourceCache/Mecabra/Mecabra-248/mecabra/IDXUserDictionary.c, line 88.

I've googled for a while and nothing is found...but it must be something wrong with the laguage settings. I've discoved I use English as user interface language and German as date/time format. Mumm, maybe that is the problem. I've set the both as English. Now, Cydia runs smoothly without any problems.

In a short, that is a bug of Cydia, it depends on the language settings of iOS! Try to set up the language unified, then it doesn't complain again.

2011-11-19

How to install FontForge on Mac OS X Lion

Since I got some old font files before 2007 and I wanna use them, after some inspection, Font Magager told me that the name tables of the fonts are mssing. In order to add them manually, I googled a while to find out FontForge can do the job.

Now comes the problem. FontForge is open source and doesn't provide any compiled binary for Mac OS X ab 10.6. That means, one must complie it before it can be used. So after a little searching again, I found Garrick's guide [How to install fontforge on mac os lion]. Sadly, the process he used doesn't work for me.

He suggests that it can be done easily with the commands:

brew install fontforge
sudo brew link fontforge

The first command returns some fatal errors:

Error: Failed executing: make install
These existing issues may help you:
https://github.com/mxcl/homebrew/issues/7658
https://github.com/mxcl/homebrew/issues/8144
https://github.com/mxcl/homebrew/issues/8491

Then the answers in those issues suggest:

brew install cairo --use-clang

I tried

brew install fontforge

after successful execution of that command. Then it came with:

Error: Failed executing: make
If `brew doctor' does not help diagnose the issue, please report the bug:
https://github.com/mxcl/homebrew/wiki/checklist-before-filing-a-new-issue

According to the message, I ran the command:

brew doctor

The result was:

Unbrewed dylibs were found in /usr/local/lib.

If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
/usr/local/lib/libfaac.0.dylib
/usr/local/lib/libfreetype.6.dylib
/usr/local/lib/libical.0.0.0.dylib
/usr/local/lib/libicalss.0.0.0.dylib
/usr/local/lib/libicalvcal.0.0.0.dylib
/usr/local/lib/libltdl.3.1.0.dylib
/usr/local/lib/libmp3lame.0.dylib
/usr/local/lib/libmp4v2.0.dylib

Unbrewed static libraries were found in /usr/local/lib.

If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected static libraries:
/usr/local/lib/libdevkit.a
/usr/local/lib/libical.a
/usr/local/lib/libicalss.a
/usr/local/lib/libicalvcal.a
/usr/local/lib/libkld.a
/usr/local/lib/libltdl.a
/usr/local/lib/libredo_prebinding.a

Unbrewed .la files were found in /usr/local/lib.

If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected .la files:
/usr/local/lib/libical.la
/usr/local/lib/libicalss.la
/usr/local/lib/libicalvcal.la
/usr/local/lib/libltdl.la


So I deleted all the above mentioned files manually and executed the command again.

brew install fontforge


The result:


Error: Failed executing: make
If `brew doctor' does not help diagnose the issue, please report the bug:
https://github.com/mxcl/homebrew/wiki/checklist-before-filing-a-new-issue


Very luckily I saw a warning before the compiling errors, that reads:

Warning: Building with LLVM, but this formula is reported to not work with LLVM:

Compiling cvexportdlg.c fails with error: initializer element is not constant

We are continuing anyway so if the build succeeds, please open a ticket with
the following information: 2336-10.7. So
that we can update the formula accordingly. Thanks!

If it doesn't work you can: brew install --use-gcc


So I tried the command again:

brew install fontforge --use-gcc

Now Mac OS X Lion lets me compile FontForge endly.

In conclusion, the right commands to compile FontForge on Mac OS X Lion should be:

brew install cairo --use-clang

brew install fontforge --use-gcc


In order to see FontForge in Application folder, you need to run this command:

ln -s /usr/local/Cellar/fontforge/20110222/FontForge.app /Applications