顯示具有 objctive c 標籤的文章。 顯示所有文章
顯示具有 objctive c 標籤的文章。 顯示所有文章

2014年8月10日 星期日

iOS 開發筆記 - 快速使用 Google Analytics SDK for iOS : Screens Usage



工作上看著老闆很重視統計資料,比我這個本業搞 Web Service 還認真,因此,想嘗試用在 Mobile app 會有如何成果!過去在 Blog 也曾用過,大概就可以得知哪篇文章比較多人看、哪個國家比較多人等等

這次也用 CocoaPods 安裝 Google Analytics SDK for iOS (pod 'GoogleAnalytics-iOS-SDK'),過程:

Step 1: 先在 Google Analytics 網站上註冊一個 App ,以此得到 tracking id

Google Analytics 首頁 -> 管理員 -> 資源(Click) -> 新建資源 -> 行動應用程式 -> 取得追蹤編號 -> 例如 @"UA-#######-#" 等

Step 2: 使用 CocoaPods 管理 Google Analytics SDK for iOS

$ vim Podfile
pod 'GoogleAnalytics-iOS-SDK'
$ pod install
...
Using GoogleAnalytics-iOS-SDK (3.0.9)
...


Step 3: 在 AppDelegate.m 初始化 Google Analytics 資訊

#import "GAI.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 30;
//[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelNone];
[[GAI sharedInstance] trackerWithTrackingId:@"UA-########-#"];

id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
tracker.allowIDFACollection = YES;

// ...

return YES;
}


Step 4: 在任何想回報的地方埋下 Codes

#import "GAIDictionaryBuilder.h"
#import "GAIFields.h"

- (void)reportStatus:(NSString *)pattern {
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value: pattern];
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];
}


如此一來,則可以透過 Google analytics 網站上觀察到多少使用者用了 App,並且透過上述 reportStatus 搭配的 pattern 字眼,可以用在使用者用了個功能就回報一次等。此外,在 Google Analytics SDK for iOS 上,其實有整合一個 GAITrackedViewController 供人繼承使用,仿造 Web 上頭的經驗,直接幫你記錄哪個 ViewController 用了多久 :) 有興趣的可以翻翻官方文件。
以下就是送出 [self reportStatus:@"test"]; 的統計資訊,需要留意的離開某個功能時,也該回報另一個狀態才能完整的終止。因此,透過 Screen 的用法,可以快速不破壞原先的程式架構。



註:由於 CocoaPods 裡頭維護的 GoogleAnalytics-iOS-SDK 沒有 link libAdIdAccess.a 這支,這將導致無法正確使用 IDFA 訊息,所以我自己額外再包了一個:changyy / GoogleAnalyticsSdkiOSUsingIDFA 來用用。

2013年12月10日 星期二

iOS 開發筆記 - 透過 semaphore 讓 function 特性從 nonblocking 回到 blocking 模式

相信從事 Window Programming 的大多都知道 Main thread(UI thread) 的特性,例如 Main Thread 用來處理事件,須儘量避免做太久的工作,此外,所謂的 nonblocking 的原理就只是在建立一支 thread 去執行任務,任務完成後在依照設計返回到 Main thread 去更動 UI 狀況。
因此,採用一些手機視窗程式等第三方 SDK 時,更是容易碰到 nonblocking functions。這會使得規劃的 function flow 有點亂掉。

在 iOS app development 環境中,以 block 為例,可以透過 semaphore 來達成 blocking 效果,如:

dispatch_semaphore_t waitJobDone = dispatch_semaphore_create(0);
[self callBlock:^{
// do something
dispatch_semaphore_signal(waitJobDone);
}];
dispatch_semaphore_wait(waitJobDone, DISPATCH_TIME_FOREVER);


順便筆記一下其他東西,隨意使用 nonblocking 用法:

dispatch_async( dispatch_queue_create(“job_running”, NULL), ^{


});


在非 UI thread 中,又想動 UI 方式:

dispatch_async( dispatch_queue_create("job_running", NULL), ^{
// do something ...
     
dispatch_async(dispatch_get_main_queue(), ^{
// update ui
});
});


最後一提,如果用到的 SDK 跟網路相關的(NSURLConnection),那大多不能用 semaphore 來使之改成 blocking 模式(或是改法很繁雜要深入or破壞SDK架構),這跟 NSURLConnection Delegate Callback 設計有關(NSRunLoop)。

iOS 開發筆記 - ARC forbids explicit message send of 'dealloc'

最近我也投入 ARC 開發模式 XD 再也不去管到底要不要對哪個 object 執行 release 的議題,這適應其實也不難,就真的不要寫 [object release] 即可 XD

但有些時候還是要在物件銷毀時去做一些處理,例如解決或等待相關的事件完成,就常常會寫這段:

- (void)dealloc
{
    // do something ...
    [super dealloc];

}


結果,Xcode 就噴這段訊息:

ARC forbids explicit message send of 'dealloc'

Transitioning to ARC Release Notes 文件查證:
You may implement a dealloc method if you need to manage resources other than releasing instance variables. You do not have to (indeed you cannot) release instance variables, but you may need to invoke [systemClassInstance setDelegate:nil] on system classes and other code that isn’t compiled using ARC. 
Custom dealloc methods in ARC do not require a call to [super dealloc] (it actually results in a compiler error). The chaining to super is automated and enforced by the compiler.

僅需改成這樣了:

- (void)dealloc
{
    // do something ...
}

2013年12月3日 星期二

iOS 開發筆記 - 解決 Storyboard UITableViewCell contentView 第一次設定 subViews frame 失效問題

為了讓自己趕快習慣 Storyboard 開發模式,瘋狂地用 Storyboard 做事 XD 除了降低原本的開發速度外,接著碰到一些好像是 bug 的奇妙問題?

這個問題是在 Storyboard 內已經拉好一個 UITableViewController ,接著在裡頭增加 UITableViewCell 後,再上頭加多個 ImageView 把玩,然而,希望 device rotation 後能後平均地顯示在 UITableViewCell 裡頭,例如有五個 ImageView 可以平均地分布。

此時在 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 已經完成重新計算各個 ImageView 的 frame 座標。

由於 Storyboard 是以 Portrait 模式設計,因此裝置一開始從 Portrait 轉換到 Landscape 時,一切都正常顯示。但如果裝置一開始就是在 Landscape 模式時,反而那些 ImageView 卻還是依照 Storyboard 裡的 Portrait 模式設計,無論我把他們的 frame 座標更新也無用。(但經過 device rotation 後,會顯示正常)

不曉得這是不是 bug ?總之,最後就想到一招就是第一次強制 ImageView 重新歸位 XD 希望這招之後可以拿掉。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        // ...

        if( cell.tag == 0 )
        {
                cell.tag = 1;
                for (UIView *v in [cell.contentView subviews])
                {
                        [v removeFromSuperview];
                        [cell.contentView addSubview:v];
                }
        }
        return cell;
}