Cocoapods第三方套件管理 - iOS開發必吃的可可豆

  • 0

安裝Cocoapods

在終端機下,輸入:

$ sudo gem install cocoapods


建立podfile

在終端機下,cd切換到專案的資料夾,輸入:

$ vi Podfile

目的要建立一個檔案讓cocoapods讀取你要管理的第三方套件


建入文字輸入介面,然後輸入以下資訊

platform:ios, '7.0'
pod 'AFNetworking', '~> 2.4.1'
pod 'ECSlidingViewController', '~> 2.0.3’


pod後面接著的是你要的套件名稱,後面是版本號,若不加,預設是下載最新的版本。
輸入完成後,按Esc到指令模式,輸入:wq,存檔離開。


下載/更新套件

$ pod update
這時cocoapods會根據你設定的iOS版本號和套件做相依性檢查。

使用套件

完成後,套件就會下載到你的專案資料夾,此時的資料夾會多幾個資料夾和檔案。
裝了cocoapods之後都必須要用.xcworkspace開啟專案。


往後如果要新增或更新套件,就再去修改cocoapods,加入你要下載的套件名稱即可。

使用套件的方法也很簡單,直接在程式裡import “xxx.h”就可以囉!大功告成





參考資料

http://cocoapods.org/

GCD - dispatch_queue的基本用法

  • 0
Grand Central Dispatch(GCD),是Apple用來處理多執行緒的一個機制,執行緒交由OS管理,效能也更好。

Dispatch Queues有以下3種類型:

1.Serial Queue:串列執行block,會依照放入的順序執行。
2.Concurrent Queue:可以同時執行多個block,但是執行完成的順序是隨機的。
3.Main dispatch queue:在應用程式的main thread中執行block。


下面是幾個簡單的使用方式:

1.Serial Queue

建立Serial Queue的方法很簡單,首先先宣告一個dispatch_queue_create建立一個dispatch_queue_t,然後執行dispatch_async的block。

範例裡分別執行2個Serial Queue,尤於是Serial Queue一次只執行一次的特性的,所以會先執行Run1,結束後再執行Run2。

dispatch_queue_t createQueue = dispatch_queue_create("SerialQueue", nil);
    dispatch_async(createQueue, ^(){
        [self run1];
    });
    dispatch_async(createQueue, ^(){
        [self run2];
    });

- (void)run1{
    for (int i=0; i<5 br="" i="">     
[NSThread sleepForTimeInterval:1];
        NSLog(@"Run1:%d",i);
    }
}
- (void)run2{
    for (int i=0; i<5 br="" i="">     
[NSThread sleepForTimeInterval:0.3f];
        NSLog(@"*Run2:%d",i);
    }
}
執行結果

Run1:0
Run1:1
Run1:2
Run1:3
Run1:4
*Run2:0
*Run2:1
*Run2:2
*Run2:3
*Run2:4
有時,當我們需要在背景用執行緒讀取資料,完成後再更新UI,這時可以用以下的寫法:

    dispatch_queue_t createQueue = dispatch_queue_create("SerialQueue", nil);
    dispatch_async(createQueue, ^(){
        [self run1];
        dispatch_async(dispatch_get_main_queue(), ^(){
            [self run2];
        });
    });

執行結果跟上面的例子一樣,但不同的是,當run1執行完成後,run2是交由dispatch_get_main_queue(),也就是Main Thread來完成,所以當有需要更新UI的地方,可以採取這樣的寫法。


2.Concurrent Queue

建立Cocurrent Queue的方法如下,首先建立dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),DISPATCH_QUEUE_PRIORITY_DEFAULT的意思是交由OS來管理優先順序,開發者可以根據需求調整執行的優先權,系統提供4種:
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^(){
        [self run1];
    });
    dispatch_async(queue, ^(){
        [self run2];
    });
執行結果
*Run2:0
*Run2:1
*Run2:2
Run1:0
*Run2:3
*Run2:4
Run1:1
Run1:2
Run1:3
Run1:4
可以看到,執行的結果完全是隨機的,也就是說,系統是同時在背景執行2個block。


3.Main dispatch queue

尤於是在主執行緒上執行,所以執行的時候,其它正在執行緒上跑的block都會排隊執行。

dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(mainQueue, ^(){
        [self run1];
        dispatch_async(dispatch_get_main_queue(), ^(){
            [self run2];
        });
    });

執行結果
Run1:0
Run1:1
Run1:2
Run1:3
Run1:4
*Run2:0
*Run2:1
*Run2:2
*Run2:3
*Run2:4 

後續還有Group of Queue,待我研究後補上。


參考資料
GCD介绍(一): 基本概念和Dispatch Queue
Austin's study note - 多工筆記 (GCD)

Xcode 6 建立 Universal Static Library 的方法

  • 0
有時候一個專案裡,常會加入自已常用的Framework,這些有可能是Open Source,或是別人分享的程式。

這時候會希望把他包成一個檔案較好管理,也方便在其他專案裡,同步更新。

這次就來說說如何在Xcode 6裡製作靜態函式庫(Static Library)。


1.首先,新建一個專案,選Cocoa Touch Static Library。



2.然後寫好你要獨立在Library的程式,設定要支援的iOS SDK版本,分別發佈Simulator和Device版。


3.在Build Phases->Copy Files,加入你要讓人別人呼叫的.h檔。



4.接下來在Product資料夾打開你Build好的資料夾


5.新建一個資料夾,分別把.a檔取名為youlibrary_simulator.a和youlibrary_device.a

6.然後用終端機打開這個資料夾,輸入以下命令,製作一個Universal的Static Library
lipo -create "youlibrary_simulator.a" "youlibrary_device.a" -output "youlibrary_universal.a" 7.將youlibrary_universal.a Add進你要用的應用程式專案。

8.在你的應用程式專案裡,打開Build Settings->Other Linker Flags,輸入「-Objc」。


9.大功告成,之後就可以在.m檔裡import "youlibrary.h"使用你製作的Library囉!

尤於靜態函式庫是一個.a檔,看不到裡面的標頭檔有那些,所以如果團隊中其它人要使用的話,還是要寫個說明文件,方便其它開發者閱讀。


[iOS] iOS App在Testflight上,接收Remote Push Notification的方法

  • 0
通常在處理Remote Push Notification的功能時,一定會先用自已的實機測試,也就是用XCode直接Build進iOS裝置做測試,這時候需要一個develope的pem來做為發送的憑證,然後等到實際上架後,再做1個Distribution的給正式上線的App使用。

但如果也要給別的使用者測試,諸如老闆、主管之類的,總不可能一個一個排隊來用XCode Build,會起肖,所以用Testflight(以下簡稱:TF)的服務可以幫我解決這個問題。

然後,沒有Remote Push Notification的App還算容易搞,發佈可以直上TF。但有的話,該怎麼辦?其實還是可以的。

因為要上TF時,一定要用發佈的Provisioning Profiles,所以就直接在Apple後台(iOS Dev Center)建1個專屬這個App用在TF的Distrubution,這個新建的Distrubution Profile發佈的時候要選擇Ad Hoc,不是App Store,否則最後不會出現Select Devices的頁面讓你勾選那個裝置可以裝,切記。

Distribution的類別


Select Devices-選擇那些裝置可以安裝App


好了,建好專屬的Distrubution Profile,下載丟到XCode去,再Archive一次吧,這樣就可以丟到TF上測試Remote Push Notification了。

Android GCM(Google Cloud Messaging) Client&Server端建置筆記

  • 0
最低需求2.3
模擬器測試最低需求4.2.2
1.Google API Library下載
開發環境請先下載Google APIs的library,把 /extras/google/google_play_services/libproject/google-play-services_lib/複製到你的專案資料夾底下,再Import進專案,File > Import, select Android > Existing Android Code into Workspace。可以參考Android官方

2.在AndroidManifest.xml,設定google api
然後在AndroidManifest.xml裡的Application加入
    <meta-data android:name=“com.google.android.gms.version" android:value="@integer/google_play_services_version" />

3.建立Google APIs專案,取得伺服器應用程式的金鑰
請先到Google APIs建立專案給GCM用,並建立一組「伺服器應用程式的金鑰」,產生的金鑰用來讓PHP發送Message用的。
https://console.developers.google.com/project
4.設定AndroidManifest.xml新增App背景服務與權限
在AndroidManifest.xml裡,設定以下權限
    <uses-permission android:name="android.permission.INTERNET" />     <uses-permission android:name="android.permission.GET_ACCOUNTS" />     <uses-permission android:name="android.permission.WAKE_LOCK" />     <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />     <permission android:name=“com.app.yourapp.permission.C2D_MESSAGE"         android:protectionLevel="signature" />     <uses-permission android:name=“com.app.yourapp.permission.C2D_MESSAGE" />

裡面
同樣設定一些服務
    <receiver android:name=".GcmBroadcastReceiver” android:permission="com.google.android.c2dm.permission.SEND" >             <intent-filter >                 <action android:name="com.google.android.c2dm.intent.RECEIVE" />                 <category android:name=“com.app.yourapp" />             </intent-filter >     </receiver= >     <service android:name=".GcmIntentService" />

5.主程式MainActivity
宣告一個字串,這串數字是Google APIs專案中的專案編號。
String SENDER_ID “123456789”;

在OnCreate加入,主要判斷是不是有playerServices,及App有沒有取得RegistrationId。
        context = getApplicationContext();         //  GCM registration.         if (checkPlayServices()) {             gcm = GoogleCloudMessaging.getInstance(this);             regid = getRegistrationId(context);             if (regid.isEmpty()) {                 registerInBackground();             }             Log.d(TAG,"Regid"+regid);         } else {             Log.d(TAG, "No valid Google Play Services APK found.");         }

新增GcmIntentService繼承IntentService, 處理App通知收到後要做什麼事。
實際程式碼請參考Android Developer

新增GcmBroadcastReceiver繼承WakefulBroadcastReceiver, 這支程式要幹嘛不曉得,應該是常駐在背景,等待接收通知用的(請有緣人解釋給我聽)。

6.完成以上步驟,實際測試。
也可以用Simulator測試,只要把Simulator的Target設定成Google Api即可。

7.PHP Server端原始碼
//------------------------------
// The recipient registration IDs
// that will receive the push
// (Should be stored in your DB)
//
// Read about it here:
// http://developer.android.com/google/gcm/
//------------------------------

$data = array('title' => $title, 'message' => $message );
$ids = array($registration_id);
//------------------------------
// Call our custom GCM function
//------------------------------

sendGoogleCloudMessage(  $data, $ids );

//------------------------------
// Define custom GCM function
//------------------------------

function sendGoogleCloudMessage( $data, $ids )
{
//------------------------------
// Replace with real GCM API
// key from Google APIs Console
//
// https://code.google.com/apis/console/
//------------------------------

$apiKey = '金鑰'; // 貼上步驟3的伺服器應用程式的金鑰」

//------------------------------
// Define URL to GCM endpoint
//------------------------------

$url = 'https://android.googleapis.com/gcm/send';

//------------------------------
// Set GCM post variables
// (Device IDs and push payload)
//------------------------------

$post = array(
'registration_ids'  => $ids,
'data'              => $data,
);

//------------------------------
// Set CURL request headers
// (Authentication and type)
//------------------------------

$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);

//------------------------------
// Initialize curl handle
//------------------------------

$ch = curl_init();

//------------------------------
// Set URL to GCM endpoint
//------------------------------

curl_setopt( $ch, CURLOPT_URL, $url );

//------------------------------
// Set request method to POST
//------------------------------

curl_setopt( $ch, CURLOPT_POST, true );

//------------------------------
// Set our custom headers
//------------------------------

curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

//------------------------------
// Get the response back as
// string instead of printing it
//------------------------------

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

//------------------------------
// Set post data as JSON
//------------------------------

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );

//------------------------------
// Actually send the push!
//------------------------------

$result = curl_exec( $ch );

//------------------------------
// Error? Display it!
//------------------------------

if ( curl_errno( $ch ) )
{
echo 'GCM error: ' . curl_error( $ch );
}

//------------------------------
// Close curl handle
//------------------------------

curl_close( $ch );

//------------------------------
// Debug GCM response
//------------------------------
echo $result;
}







Unity使用JSONObject解析json資料

  • 0
最近又要用到Json跟網路的功能,來做連線程式,於是找了一下Unity如何處理Json的方法。

我使用JSONObject來parser的函式庫,可以自行到Asset Store找下來安裝即可,免費的喔!

簡單講一下怎麼操作,分為Encoding與Decoding

Encoding
string ourPostData = "{\"Name\":\"Awei\"}"; 
假設你有一串Json的字串,如何轉成JSONObject呢?
JSONObject encodingObject = new JSONObject (JSONObject.Type.OBJECT);
encodingObject.AddField ("ID",1);
encodingObject.AddField ("Name", "Awei");
encodingObject.AddField ("Age", "18");
print ("Hello : " + q["Name"]);
執行結果
Hello : Awei

說明

第1行宣告1個JSONObject的物件,宣告同時,指定是一個空物件。
第2~4行把值塞入空物件,可以塞數值、字串或是陣列。
第5行將值讀出。




Decoding
string ourPostData = "{\"Name\":\"Awei\"}"; 
假設你有一串Json的字串,如何轉成JSONObject呢?
JSONObject output = new JSONObject(ourPostData);
print (output["Name"]);
執行結果
Awei

說明

第1行就是宣告1個JSONObject的物件,宣告同時,把json的字串帶入。
第2行就是印出output的物件,宣告的物件是1個陣列,可以帶Key的名字(ex:Name, Age),或是指定索引值(ex:0,1,2),取得該陣列的值。


用法大概是以上這樣,我是拿來做網路傳資料使用,所以在送出時,我需要把資料做Encoding然後處理成byte[]給網路去傳送,傳送的時候,直接在jsonobject後面加上.ToString(),即可轉成字串,丟給網路處理了;而接收的時候,就是使用Decoding將回傳的資料做解譯。

NGUI ScrollView加Button後不能拖拉的問題

在NGUI的ScrollView中,如果你的ScrollView有設定可以拖拉的話,當你又加入了Button到ScrollView,這時候你會發現,Button可以按,但ScrollView卻不能Drag了。

原因是BoxCollider,當我把Button的BoxCollider關閉就正常了,滿奇怪的原因。

而我目前解決的方法是在Button裡在加入UIDrag Scroll View這個Script,然後把ScrollView設定好,此時,Button可按又可拖拉囉!