最近小弟開始在研究 iPhone 的開發,無奈找到的資料都是舊的,也不知道怎麼放到 iPhone 上,應該很多人都跟我有同樣的困擾吧!經過幾天的研究總算寫出第一個 HelloWorld,而且可以不用付99元美金就可以放到實機上執行! 今天先和大家分享怎麼放到手機上,以及基本的 Xcode 使用:
我的環境如下:
IBMX60+iDeneb 10.5.5
iPhone 港版16G+FW2.2+越獄
iPhone SDK 2.2 final
MobileInstallation 已替換(這很重要,不然沒法放到 IP 上,可以參考 weiphone 的這個帖子:
http://www.weiphone.com/thread-206698-1-1.html)
注意: SDK 版本最好和 IP 版本一致!
好啦,同學們,課程開始啦!
1. 產生證書
由於 XCode 後面的版本都要求應用程式必須經過簽署,也因此你必須要
多花99美元加入 Developer Program (真是黑呀),反正我又不放到 App Store 賣錢,乾脆自己簽個證書來用不就好了
打開 Key Access 這個工具程式(在應用程式->工具程式內),並建立一個 "iPhone Developer" 名稱的證書(這個名稱不要改,否則你得在專案內改變)



類型選擇"編碼簽名"

輸入憑證資訊



最後選擇存放在"系統"內(不是 "登入",否則會找不到)

這樣就大功告成了,你已經省下$99了!
2. 打開 XCode 建立 View Based Application


2. 專案建立好了後,打開 Project Setting,並在 User-Defined Settings 輸入:
PROVISIONING_PROFILE_ALLOWED = NO
PROVISIONING_PROFILE_REQUIRED = NO

3. 打開 Info.plist,這個檔案主要存放應用程式的相關描述,按右鍵 Add Row,新增 SignerIdentity = "Apple iPhone OS Application Signing" (要一樣的,亂輸好像不行)

4. 打開 MainWindow.xib 檔案,並雙擊 Window 這個圖標,我們可以看到 iPhone 內唯一的一個 Window

5. 加入我們自己的 View,在 Class Identity(切換到最右邊的 Tab) 輸入 MyView 按下回車鍵

6. 同樣的我們拉進一個 UILabel 和 UIButton,並調整一下 Label 的屬性,這邊 Autosizing 的調整還蠻容易懂的,大家可以試著調整看看就知道了

7. 畫面弄好了,但是還沒有把我們的 View 和 ViewController 建立連接,點選 HelloWorld View Controller 後可以在菜單內的 Tools->Connections Inspector 看到和 View Controller 連接的 Outlet(也就是類裡面的變量),連接的方法是把 outlet 內的view 拖拉到我們實際建立的 View

8. 接著我們在建立好的 View 新增一個 outlet(lbl) 和 action(clickMe),這樣我們才可以改變 Label 這個物件,並分別把 outlet 連接到 HelloWorld 這個實際的 Label,action 連接到 "點我" 這個按鈕,並選擇 Touch Down 的事件,稍後我們會用到這個事件來改變 Label


9. 連接關係都建立好了,我們要產生實體的類,XCode 會幫我們把必要的 code 產生,產生的方法是在 File->Write Class Files

10.接下來我們加上一些代碼,分別打開MyView.h(注意要在 MyView 後面輸入 UIView,Xcode 不會加上)和MyView.m並輸入下面代碼



值得一提的是你可以在寫的過程中按 CTRL+"<" 看到提示
11. 選擇要使用的設備,在此我們選擇 Device - iPhone OS 2.2,XCode 會幫我們簽署,按下"允許"(不能按"總是允許")就可以輸出到你的iPhone了(第一次跑可能會出現 Security policy error,再跑一次
就不會出現了)

12. 要刪除模擬器或實體機上的 Ap, 只要按住圖標不放就可以了

13. 來個實體和虛擬機的合照吧,點"按我"可以改變 Hello World 的顏色

下課了~
[
本帖最后由 pochuang 于 2009-1-7 15:35 编辑 ]