Wednesday 13 July 2016

Bug from Facebook in iOS app

When you add Facebook login/share/post..... in your iOS app. It's easy to integrate to your app if the app is simple. If the app is customized with custom navigation bar, you will get this error every time Facebook needs to popup VC then crashes.
 *** Assertion failure in -[UICGColor encodeWithCoder:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UIColor.m:1457
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only RGBA or White color spaces are supported in this situation.'
This is an error from facebook because they cannot work with customized navigation bar. To fix this. You need to find where navigationbar is colored with image, for example:
[[UINavigationBar appearancesetBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_top_pattern"]]]; 
Change to:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_top_pattern"] forBarMetrics:UIBarMetricsDefault]; 

The error is gone!

Thursday 7 July 2016

Implement Google Analytic V3 for iOS project without using Cocoapod

----Draft----
Do đặc thù các dự án của khách hàng nên đa số chúng ta làm các dự án maintain (nâng cấp, thêm feature v.v...). Nên việc source code sử dụng những "món" quá cũ kỹ cũng làm việc maintain của các developer khó khăn hơn.
Google Library là một trong những thư viện gây ra nhiều rắc rối nhất cho việc maintain của các developer. Khó khăn hơn, iOS cũng thường xuyên update, thêm "món" mới, bỏ "món" cũ. Điển hình là việc sử dụng Cocoapod trở nên phổ biến hơn.
Cocoapod là gì?
Google Analytics V3 đang khuyến khích người dùng nhúng vào project bằng Cocoapod. Trên trang web của Google Analytics thì cũng chỉ chỉ cách thêm thư viện bằng cách chạy bằng Cocoapod. Nhưng có cách khác mà chúng ta không cần dùng cocoapod vẫn thêm thư viện vào xài ngon lành.

A. Đối với app chưa có Google Analytics
Bạn cần download về thư viện V3 của GA (downloadv3).
Giải nén ra thì thấy có rất nhiều thứ trong đó. Chỉ copy folder GoogleAnalytics/Library và libGoogleAnalyticsServices.a vào Resource của app cần dùng GA. Add folder vào project. 
GA cũng cần thêm một số thư viện của iOS (App target -> Build Phrase -> Link Binary with Libraries). Nên check lại các thư viện cần thiết để tránh bị báo lỗi.
  • libGoogleAnalyticsServices.a
  • AdSupport.framework
  • CoreData.framework
  • SystemConfiguration.framework
  • libz.dylib
  • libsqlite3.dylib
  • libxml.dylib
    • Hướng dẫn cách add dylib trong XCode 7.0
Vào file .pch để import GA
#import "GAI.h"
#import "GAIFields.h"
Nếu ko import trong file.pch thì file nào dùng GA thì import library trong file đó.
Vào AppDelegate.m để init GA tracker
// 1 -- optional
[GAI sharedInstance].trackUncaughtExceptions = YES;
 
// 2 -- optional
[[GAI sharedInstance].logger setLogLevel:kGAILogLevelVerbose];
 
// 3 -- optional
[GAI sharedInstance].dispatchInterval = 20;
 
// 4 -- optional
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXXXXX-Y"];
@"UA-XXXXXXX-Y" là tracking ID của app sau khi đã đăng ký với GA.

Vào Controller cần track, paste đoạn code này vào file.h
#import "GAITrackedViewController.h"
 
@interface ClockViewController : GAITrackedViewController
 
@end
sau đó vào chỗ mà bạn cần track thêm đoạn code dùng để track
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.screenName = @"Clock";
}

Vậy là xong. Bạn có thể vào GA để xem chương trình đang chạy đã được track trên GA chưa.
Advance configuration tham khảo thêm tại đây: https://www.raywenderlich.com/53459/google-analytics-ios



B. Đối với app đã dùng Google Analytic V2