2008年10月26日星期日

iPhone SDK中使用fopen && open

需要引入
fopen与open都可以使用,在Application_Home/Documents/这个目录下是可以使用"写"的操作。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//creates paths so that you can pull the app's path from it

NSString *documentsDirectory = [paths objectAtIndex:0];
//gets the applications directory on the users iPhone

NSString *scoreFileName = @"/SavedScores";
//sets the name of the file name to be SavedScores

const char *scoreFile = [[documentsDirectory stringByAppendingString:scoreFileName] UTF8String]; //appends scoreFileName to the end of documentsDirectory & converts it to a const char so that fopen can deal with it.

/*
FILE * pFile;
if((pFile = fopen ( scoreFile , "w" )) != NULL) { //opens file & positions to beginning
fclose(pFile);
}
*/

int fd;

fd = open(scoreFile, O_APPEND | O_WRONLY, 0755);

close(fd);

2008年10月22日星期三

iPhone non official SDK aka ToolChain finally setup

[Original: http://aakash-bapna.blogspot.com/2007/10/iphone-non-official-sdk-aka-toolchain.html]

Woo.. after doing the process almost ten times, downloading the full xodetools.dmg(~924mb) i finally have the iPhone toolchain working.. i have setup the non official iPhone SDK ,, untill the official by Apple comes out in Feb'08 .
Start developing native iPhone apps today, no more web 2.0...
There are many guides out there, i am just telling my expierience, hope it works for you..
It uses the GC++, GCC compiler in linux..
I used Fedore 7 on VMware(virtual machine, you can grab both of them for free) to build the toolchain. I recomd. linux for good performance..
Content from -http://code.google.com/p/iphone-dev/wiki/Building ,with my tips in BOLD.

HOWTO build the toolchain
Run terminal...

  • Ensure that you have a copy of bison and flex installed on your system. All major systems should have these tools installed or available in package management systems. Most linux flavours have the packages, you can skip this..
  • $ bison --version
    GNU
    Bison version 1.28
    $ flex
    --version
    flex version
    2.5.4
  • Check out a copy of LLVM SVN, and build a release build (as opposed to a debug one). Currently, due to Issue 70, we are limited to revision 42498. it will take some time while files are being downloaded to your PC from internet, probably no errors here
  • $ svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm-svn -r 42498
    $ pushd llvm
    -svn
    $ .
    /configure --enable-optimized
    $ make ENABLE_OPTIMIZED=1
    $ sudo make install
    $ LLVMOBJDIR=`pwd`
    $ popd
  • Check out a copy of the iphone-dev SVN repository.( it will take large amount of time, the longest step of all)
  • $ svn checkout http://iphone-dev.googlecode.com/svn/trunk/ iphone-dev
    $ pushd iphone
    -dev
  • Make a directory to hold the toolchain.
  • $ sudo mkdir /usr/local/arm-apple-darwin
  • Build odcctools. ( you will get some error if you are using CYGWIN on windows here, refer to there site for details )
  • $ mkdir -p build/odcctools
    $ pushd build
    /odcctools
    $
    ../../odcctools/configure --target=arm-apple-darwin --disable-ld64
    $ make
    $ sudo make install
    $ popd
  • Get a copy of the iPhone root filesystem. This is usually obtained by decrypting and extracting the iPhone restore software using these tools, but there are many other methods to obtain this, including simply using scp to download all the files from the iPhone over Wi-Fi. (Scroll below for a download link of ready to use iPhone 1.0.2 image) Unpack the root filesystem somewhere, and set the environment variable $HEAVENLY to its path. (set this again if you restart system or terminal)
  • $ HEAVENLY=/usr/local/share/iphone-filesystem
  • Install the iPhone headers to the appropriate place. If you aren't on Mac OS X, replace the /Developer/SDKs/MacOSX10.4u.sdk path with the full path to the unpacked Mac OS X 10.4 Universal SDK(ie. MacOSX10.4u.sdk). If you don't have a copy of this, scroll below..
  • $ pushd include
    $
    ./configure --with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk
    $ sudo bash install
    -headers.sh
    $ popd
  • Install csu, which includes crt1.o, dylib1.o, and bundle1.o. Don't rebuild them from source, as this requires a working cross-GCC, which you don't have yet (and the build-from-source process for csu is broken right now anyway). Binaries are provided for this reason.
  • $ mkdir -p build/csu
    $ pushd build
    /csu
    $
    ../../csu/configure --host=arm-apple-darwin
    $ sudo make install
    $ popd
  • Only if you are compiling on leopard 10.5. This is an ugly workaround and compiling with the resulting toolchain will give you a warning.
  • $ mv llvm-gcc-4.0-iphone/configure llvm-gcc-4.0-iphone/configure.old
    $ sed
    's/^FLAGS_FOR_TARGET=$/FLAGS_FOR_TARGET=${FLAGS_FOR_TARGET-}/g' \
    llvm
    -gcc-4.0-iphone/configure.old > llvm-gcc-4.0-iphone/configure
    $
    export FLAGS_FOR_TARGET="-mmacosx-version-min=10.1"
    $ sudo ln
    -s /usr/local/arm-apple-darwin/lib/crt1.o \
    /usr/local/arm-apple-darwin/lib/crt1.10.5.o
  • Configure and make LLVM-GCC. Make sure that $LLVMOBJDIR and $HEAVENLY are set per the instructions above.(i got the errors like error 1, error 2 more than 5 times, i solved this problem by changing lots of files,symlinks in /usr/lib folder of iphone image hence i have uploaded my modified working iphone image. see below for errors).
  • $ mkdir -p build/llvm-gcc-4.0-iphone
    $ pushd build
    /llvm-gcc-4.0-iphone
    $
    ../../llvm-gcc-4.0-iphone/configure --enable-llvm=`llvm-config --obj-root` \
    --enable-languages=c,c++,objc,obj-c++ --target=arm-apple-darwin --enable-sjlj-exceptions \
    --with-heavenly=$HEAVENLY --with-as=/usr/local/bin/arm-apple-darwin-as \
    --with-ld=/usr/local/bin/arm-apple-darwin-ld
    $ make LLVM_VERSION_INFO
    =2.0-svn-iphone-dev-0.3-svn
    $ sudo make install
    $ popd
    $ popd
You're done. Have fun!

Gotchas

  • Make sure that there are no spaces in any of the directory names that you use. GCC does not build if there are spaces in the directory names, and the shell scripts will not work. This is most often a problem on Windows.
  • Make sure that /usr/local/bin is in your PATH. This is not the case out of the box on Mac OS X, although chances are if you've installed things from source before you've added it already.
  • If LLVM failed to compile because of missing bison and/or flex (evidence that this was the case is a failed compile on utils/TableGen/FileLexer.l), you must make clean and delete utils/TableGen/FileParser.h before trying again with bison and flex installed. LLVM's make clean script does not always properly clean up after a failed build.
  • Make sure that $HEAVENLY points to a copy of the full iPhone root filesystem, not just the unpacked update DMG. If you get errors about being unable to find -lc, then your $HEAVENLY is set incorrectly, you mistyped the --with-heavenly option to configure, or your copy of the root partition is incomplete. Your copy of the iPhone root filesystem must include a usr/lib/libc.dylib file.

HOWTO obtain the Mac OS X headers on Linux or Windows

=Install the cpio utility using your distribution's standard package management system. All the major Linux distributions, as well as Cygwin, should have this utility.
=No need to download the whole xcode tools dmg (~924mb). I extracted the package,uploaded it for you :), download the needed file from here- http://rapidshare.com/files/66764258/MacOSX10.4u.sdk.rar
=Extract RAR, then the pax archive by creating a new dir and using the following command in terminal
gunzip -c MacOSX10.4u.sdk.pax.gz | cpio -i

=Find MacOSX10.4u.sdk folder in extracted contents and link it toolchain install procedure.

--LOGIN as root to avoid weird errors, password prompts again and again..

--The envirnoment variables should be set properly to check there use this command in terminal- echo $VARIABLENAME, eg. - echo $HEAVENLY if it returns blank line set it again. the envirno. variable has a liftime till the terminal runs, if you close it, you have to set it again


-- errors i got ,
when i do "make LLVM_VERSION_INFO=2.0-svn-iphone-dev-0.3-svn " its runs for a long time and then results in this error * /usr/local/bin/arm-apple-darwin-ld: /home/Aakash/iphone/iphone-image/usr/lib/libc.dylib truncated or malformed object (mach header extends past the end of the file) collect2: ld returned 1 exit status make2 [libgcc_s.dylib] Error 1 make2 Leaving directory `/root/iphone-dev/build/llvm-gcc-4.0-iphone/gcc' make1 [stmp-multilib] Error 2 make1: Leaving directory `/root/iphone-dev/build/llvm-gcc-4.0-iphone/gcc' make: [all-gcc] Error 2*
i did this to get off the error "open all .dylib files (<=1 k) one by one in folder /iphone-image/usr/lib/, rename it - and for example libc.dylib shows path to libSystem and libSystem - path to libSystem.B.dylib. Just copy libSystem.B.dylib twice and rename 2 new files to libc.dylib and libSystem.dylib. do this for all <=1kb .dylib files".
The iPhone image i uploaded here contains these changes, no need to do again.


Password for all my rapidshare uploads if needed in this post is aksblog.co.nr

Usefull links-
Post toolchain install--
  • sample test app that worked for me http://absolutech.org/files/testApp.rar , extract, goto directory in terminal type make
  • http://www.cocoadev.com/index.pl?UIKit
  • http://phonedev.tumblr.com/
  • http://www.lucasnewman.com/iphone/

Need any help, post as comment...

HOWTO build the toolchain


  • Check out a copy of the iphone-dev SVN repository.
  • $ svn checkout http://iphone-dev.googlecode.com/svn/trunk/ iphone-dev
    $ pushd iphone
    -dev
  • Make a directory to hold the toolchain.
  • $ sudo mkdir /usr/local/arm-apple-darwin
  • Build odcctools.
  • $ mkdir -p build/odcctools
    $ pushd build
    /odcctools
    $
    ../../odcctools/configure --target=arm-apple-darwin --disable-ld64
    $ make
    $ sudo make install
    $ popd
  • Get a copy of the iPhone root filesystem. This is usually obtained by decrypting and extracting the iPhone restore software using these tools, but there are many other methods to obtain this, including simply using scp to download all the files from the iPhone over Wi-Fi. Unpack the root filesystem somewhere, and set the environment variable $HEAVENLY to its path.
  • $ HEAVENLY=/usr/local/share/iphone-filesystem
  • Install the iPhone headers to the appropriate place. If you aren't on Mac OS X, replace the /Developer/SDKs/MacOSX10.4u.sdk path with the full path to the unpacked Mac OS X 10.4 Universal SDK. If you don't have a copy of this, see below.
  • $ pushd include
    $
    ./configure --with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk
    $ sudo bash install
    -headers.sh
    $ popd
  • Install csu, which includes crt1.o, dylib1.o, and bundle1.o. Don't rebuild them from source, as this requires a working cross-GCC, which you don't have yet (and the build-from-source process for csu is broken right now anyway). Binaries are provided for this reason.
  • $ mkdir -p build/csu
    $ pushd build
    /csu
    $
    ../../csu/configure --host=arm-apple-darwin
    $ sudo make install
    $ popd
  • Only if you are compiling on leopard 10.5. This is an ugly workaround and compiling with the resulting toolchain will give you a warning.
  • $ mv llvm-gcc-4.0-iphone/configure llvm-gcc-4.0-iphone/configure.old
    $ sed
    's/^FLAGS_FOR_TARGET=$/FLAGS_FOR_TARGET=${FLAGS_FOR_TARGET-}/g' \
    llvm
    -gcc-4.0-iphone/configure.old > llvm-gcc-4.0-iphone/configure
    $
    export FLAGS_FOR_TARGET="-mmacosx-version-min=10.1"
    $ sudo ln
    -s /usr/local/arm-apple-darwin/lib/crt1.o \
    /usr/local/arm-apple-darwin/lib/crt1.10.5.o
  • Configure and make LLVM-GCC. Make sure that $LLVMOBJDIR and $HEAVENLY are set per the instructions above.
  • $ mkdir -p build/llvm-gcc-4.0-iphone
    $ pushd build
    /llvm-gcc-4.0-iphone
    $
    ../../llvm-gcc-4.0-iphone/configure --enable-llvm=`llvm-config --obj-root` \
    --enable-languages=c,c++,objc,obj-c++ --target=arm-apple-darwin --enable-sjlj-exceptions \
    --with-heavenly=$HEAVENLY --with-as=/usr/local/bin/arm-apple-darwin-as \
    --with-ld=/usr/local/bin/arm-apple-darwin-ld --enable-wchar_t=no
    $ make LLVM_VERSION_INFO
    =2.0-svn-iphone-dev-0.3-svn
    $ sudo make install
    $ popd
    $ popd
  • You're done. Have fun!

XCode中整合toolchain开发环境和苹果官方的iPhone SDK

[转 http://www.cocoachina.com/index.php/archives/cocoachina_140.html]

网上搭建iPhone开放开发环境的文章已经不少了。本文参考了不少资料,详细说明如何成功在Leopard中搭建一个完整的开发环境并整合XCode进行iPhone源生程序的开发。

在XCode中整合toolchain开发环境和苹果官方的iPhone SDK并不冲突,你可以在同一个XCode中既建立toolchain的开发工程,也可以建立官方的iPhone SDK工程。

本文在Leopard 10.5.2,XCode 3.1 Beta 3(随iPhone SDK Beta3提供),iPhone OS 1.1.4测试通过。

准备工作

请确认操作系统为Leopard 10.5.2。

请确认XCode已经安装。最好安装最新的官方XCode 3.1 Beta。

手中最好是有iPhone或者iPod Touch, 已经jailbreak。

获取toolchain源码

打开一个terminal窗口,随便建立一个目录

  1. mkdir iphoneenv
  2. cd iphoneenv

后面的操作都在这个目录中进行,只需复制下文文字框中的内容,贴到terminal窗口执行即可。

获取源码

  1. svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm-svn -r 42498
  2. pushd llvm-svn
  3. ./configure –enable-optimized
  4. make ENABLE_OPTIMIZED=1
  5. sudo make install
  6. LLVMOBJDIR=`pwd`
  7. popd

获取iphone-dev SVN代码库

  1. svn checkout http://iphone-dev.googlecode.com/svn/trunk/ iphone-dev
  2. pushd iphone-dev

建立一个保存toolchain的目录。

  1. sudo mkdir /usr/local/arm-apple-darwin

编译odcctools

  1. mkdir -p build/odcctools
  2. pushd build/odcctools
  3. ../../odcctools/configure –target=arm-apple-darwin –disable-ld64
  4. export INCPRIVEXT="-isysroot /Developer/SDKs/MacOSX10.4u.sdk"
  5. make
  6. sudo make install
  7. popd

获取iPhone中的框架和头文件

获取这个iPhone的镜像有很多办法,这里介绍几种:

最省事的办法(如果手头没有iPhone或是没有wi-fi):

在google搜索iphone_image,找到了就直接下载,解压缩。

麻烦点的办法(手头有jailbreak并安装了installer的iPhone或者iPod Touch):

如果你家里或者单位有wi-fi,而且iPhone已经连上w-ifi,直接在iPhone的Installer里找到BSD Subsystem和OpenSSH并安装,然后用scp把iPhone里面从根目录开始的所有子目录的所有的文件拷贝出来。

或者在installer里安装AFPd,然后用Finder打开afp://你的iphone的ip地址,用户名用root, 密码用alpine,把里面的所有文件直接拽出来。

详细方法不介绍了,相信你一定能搞定。

复制镜像

假设你把这个镜像里的所有文件都放在/iphone_image目录,那么执行下面操作(不要离开terminal的当前目录):

  1. sudo mkdir /usr/local/share/iphone-filesystem
  2. sudo cp -Rp /iphone_image/* /usr/local/share/iphone-filesystem/

设置$HEAVENLY的环境变量

  1. HEAVENLY=/usr/local/share/iphone-filesystem

  1. pushd include
  2. ./configure –with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk
  3. sudo bash install-headers.sh
  4. popd

安装csu

  1. mkdir -p build/csu
  2. pushd build/csu
  3. ../../csu/configure –host=arm-apple-darwin
  4. sudo make install
  5. popd

configure和make LLVM-GCC

  1. mv llvm-gcc-4.0-iphone/configure llvm-gcc-4.0-iphone/configure.old
  2. sed 's/^FLAGS_FOR_TARGET=$/FLAGS_FOR_TARGET=${FLAGS_FOR_TARGET-}/g' llvm-gcc-4.0-iphone/configure.old > llvm-gcc-4.0-iphone/configure
  3. sudo ln -s /usr/local/arm-apple-darwin/lib/crt1.o \/usr/local/arm-apple-darwin/lib/crt1.10.5.o
  4. mkdir -p build/llvm-gcc-4.0-iphone
  5. pushd build/llvm-gcc-4.0-iphone
  6. export FLAGS_FOR_TARGET="-mmacosx-version-min=10.1"
  7. sh ../../llvm-gcc-4.0-iphone/configure –enable-llvm=`llvm-config –obj-root` \
  8. –enable-languages=c,c++,objc,obj-c++ –target=arm-apple-darwin –enable-sjlj-exceptions \
  9. –with-heavenly=$HEAVENLY –with-as=/usr/local/bin/arm-apple-darwin-as \
  10. –with-ld=/usr/local/bin/arm-apple-darwin-ld
  11. make LLVM_VERSION_INFO=2.0-svn-iphone-dev-0.3-svn
  12. sudo make install
  13. popd
  14. popd

这样,专门用来编译iPhone的toolchain gcc就编译完了,文件位置在:

  1. /usr/local/bin/arm-apple-darwin-gcc

整合XCode

接下来,整合XCode:

下载XCode模板

解压缩,把

  1. iPhone UIKit Skeleton Application

这个文件夹复制到

  1. /Developer/Library/XCode/Project Templates/Application/

好了,打开XCode,新建一个项目,你会发现列表里已经出现了

iPhone UIKit Skeleton Application

这样一个模板(不在iPhone OS分类里而是在Mac OS X分类)

直接创建项目,取个名字, 直接编译,成功。

建议你使用本文的方法编译toolchain,而不是直接下载别人编译好的安装包,这样问题会少很多。

如果你下载了其他地方的例子,可能需要修改Makefile才可以编译成功。尤其是google code的iPhone代码。

比如:

你需要把

  1. CC=/usr/local/arm-apple-darwin/bin/gcc

改成:

  1. CC=/usr/local/bin/arm-apple-darwin-gcc -v

  1. CC=/usr/local/arm-apple-darwin/bin/g++

改成:

  1. CXX=/usr/local/bin/arm-apple-darwin-g++

把LDFLAGS里的

  1. -Wl,-syslibroot,/usr/local/arm-apple-darwin/heavenly

直接删掉。

好了,这样,你的开发环境就搭建完了。

2008年10月13日星期一

How To Create An iPhone Preferences File

  1. In Finder navigate to your Xcode project.
  2. Create a new folder called Settings.
  3. Inside the folder create a new file called Root.plist. See below for an empty Root.plist example.
  4. Rename the Settings folder to Settings.bundle. Finder will ask if you really want to do this. You do. Root.plist will “disappear” into the Settings.bundle file.
  5. In Xcode Command-click on Resources in your project. Select Add > Existing Files…
  6. Select Settings.bundle and click Add twice.
  7. You should now see the Settings.bundle file in Xcode and if you expand it Root.plist will appear again.

If you know of a less convoluted way to create this structure in Xcode, please let me know in the comments.

Empty Root.plist file:








Edit Root.plist

If you double click on Root.plist in Xcode the file will open in a standard text editor. A better alternative is to command-click Root.plist and select Open With Finder. That will open the Property List Editor.

  1. Expand the Root node and click the New Child button.
  2. Name the child Title, select the String class and enter the name of your application as the value.
  3. With the new Title node selected, click the New Sibling button.
  4. Name the new node PreferenceSpecifiers, and select the Array class. (You can’t enter a value for an Array.)
  5. With the PreferenceSpecifiers node selected, click the New Child button.
  6. Select the class Dictionary for the new node. (You can’t change the name of the node nor the value for a Dictionary entry.)
  7. Expand the new node and click on the New Child button.
  8. Name the new child Title, leave it as a String class, and give it a value that will have meaning to a user of your application.
  9. Click on the New Sibling button. Give the node these values: name = Type, class = String, value = PSTextFieldSpecifier. This will allow a user to enter a preference value in a text field.
  10. Click on the New Sibling button. Give the node these values: name = Key, class = String, value = something that has meaning in your code. This is the key your code will use to lookup this preference value.
  11. Click on the New Sibling button. Give the node these values: name = DefaultValue, class = String, value = a meaningful default. This value will be used unless the user has set the preference to something else.
  12. Save the Root.plist file.

If you expand all the nodes in the Property File Editor it should look something like this:

Root.plist

If you view the file in an Xcode text editor it will look like this:





PreferenceSpecifiers


DefaultValue
2
Key
delayBeforeDialing
Title
Delay before dialing
Type
PSTextFieldSpecifier


Title
DTMF Dialer

Build and run your app. Exit your app and tap the Settings app. You should now see a new entry that matches the name of your app. Tap this entry and your own custom preferences will show up. The user can edit and save these preferences without you having to write any code. Pretty neat.

Custom Settings

Multi Value Specifier Preference

If the user should select between multiple pre-defined values you can use a PSMultiValueSpecifier. Enter the visible titles as an Array under the name Titles, and the values your code sees as an Array under the name Values.

Something like this:

MultiValueSpecifier

Other Types of Preference Values

  • PSToggleSwitchSpecifier - for boolean values
  • PSSliderSpecifier - for a range of values
written by Nick Dalton \\ tags: , , , , , , ,

2008年10月12日星期日

[Firmware 2.1] How to skip provisioning profile for iPhone SDK

[from: http://iphonesdkdev.blogspot.com/]

How to skip provisioning profile for iPhone SDK (build 9M2517) for firmware 2.1

First close your Xcode and backup the PhoneOS.platform Info.plist by running this in Mac Terminal


sudo cp -p /Developer/Platforms/iPhoneOS.platform/Info.plist /Developer/Platforms/iPhoneOS.platform/Info.plist.bak


Then edit the PhoneOS.platform Info.plist file

sudo vi /Developer/Platforms/iPhoneOS.platform/Info.plist


and change

PROVISIONING_PROFILE_ALLOWED
YES
PROVISIONING_PROFILE_REQUIRED
YES


to this


PROVISIONING_PROFILE_ALLOWED
NO
PROVISIONING_PROFILE_REQUIRED
NO


Launch Xcode and then create new project using my Xcode Template for pwned device "View-Based Application" to build and go to either Project Settings 2.0 or Project Settings 2.1 or iPhone Simulator and you don't need the trick to reset it anymore.

Remember, you still need a fake codesign identity and patched MobileInstallation binary in your iPhone.

If you are registered iPhone Developer with provisioning profile and codesign cert from Apple, please remember to revert the PhoneOS.platform Info.plist back to the original one before building your project to App Store.

Xcode Template for pwned iPhone device updated to version 3.5.2

[R http://iphonesdkdev.blogspot.com/]
This Xcode Template is now updated to version 3.5.2

To install, run these in Mac Terminal (assume you have installed the Xcode in /Developer folder)


curl -O http://www.iphone.org.hk/attach/42698-ToolChainTemplate_v352.zip
unzip -o 42698-ToolChainTemplate_v352.zip -d "/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates"

MD5 (42698-ToolChainTemplate_v352.zip) = 5de15c4cc9f7bab7feeafff5f121da36



There are 4 new project templates to build the command line utility for iPhone
C++ Tool
CoreFoundation Tool
Foundation Tool
Objective C++ Tool


These 4 command line utility templates used wifi connection to install compiled binary in your iPhone, so you need not connect your iPhone via usb port and MobileInstallation patch is not needed.

Requirements & Info:
(1) Default use Open toolchain headers
(2) iPhone and Mac connected to wifi (you should change the IPHONE_IP in Project Settings)
(3) Open SSH and Link Indenty Editor installed in iPhone
(4) Mac's ssh key installed in iPhone
(5) Debug / Release build to iPhone folder /private/var/root (no Build & Go) and you should change IPHONE_INSTALL_DIR in the Project Settings
(6) Cannot build to Simulator

Xcode Template Version 3.5.2 adds a Static Library Template for iPhone
To build the Universary Library for i386 and arm
First build it on "Device" "Release"
Second build it on "Simulator" "Release"
Then the Universary Library binary will be in the project directory
To examine the content of the Universary Library binary, run ./otoolprint.sh in the project folder


Open Tool Chain Application
This template uses open toolchain headers provides a starting point for any application without Interface Builder.

Requirements and Info:
(1) No Interface Builder
(2) Default use open toolchain headers
(3) For pwned iPhone / iPod Touch with patched MobileInstallation
(4) codesign certificate "iPhone Pwned Developer" installed in Mac
(5) XCode SDK Info.plist Setting has removed Provisioning Profile requirement
(6) Cannot build to Simulator


View-Based Application
This template provides a starting point for an application that uses a single view. It provides a view controller to manage the view, and a nib file that contains the view.

Requirements and Info:
(1) Default use official iPhone SDK headers
(2) For pwned iPhone / iPod Touch with patched MobileInstallation
(3) codesign certificate "iPhone Pwned Developer" installed in Mac
(4) XCode SDK Info.plist Setting has removed Provisioning Profile requirement

Window-Based Application
This template provides a starting point for any application without Interface Builder and provides a minimal iPhone application with a single view, ready for customizing.
This is a good starting point for your first application. In the UIView subclass, you can implement methods to draw content on the screen and to respond to touches. In a more full-featured application, you typically add a view controller to handle data and to manage rotation.

Requirements and Info:
(1) No Interface Builder
(2) Default use official iPhone SDK headers
(3) For pwned iPhone / iPod Touch with patched MobileInstallation
(4) codesign certificate "iPhone Pwned Developer" installed in Mac
(5) XCode SDK Info.plist Setting has removed Provisioning Profile requirement

Bypassing iPhone Code Signatures


Due to popular demand, I am putting some of the content I have written for the Cydia information portal here on my website so people an link to it directly. Given the original distribution medium, the material is therefore quite condensed. If I have time I may flesh out more details.

Starting with the recent beta releases of the iPhoneOS, Apple has started requiring that all code on the device is signed. This is mostly to make it impossible for programs running through Apple's AppStore to download more software and run it (so no competition for AppStore).

In order to get around this (and thereby to install our own code onto the device) the iPhone Dev Team has patched the signature verification out of the kernel. However, another half of the codesign problem is that the binary contains a number of SHA1 verification hashes that are checked in numerous locations throughout the kernel. Patching this out is A) difficult (especially to track as Apple makes changes) and B) of marginal benefit as adding these hashes is easy. This means you do still have to at least pay lipservice to the code signature process. There are currently three viable options.

Option #1: Self-Signing

This method is the simplest to understand: using Apple's codesign tool to sign the binary. Because the signature verification checks have been hacked out of the kernel, you can use any signature to do this, not just ones that are approved by Apple's developer program. For instructions on how to make a self-signing certificate you can read this article from Apple's website: Obtaining a Signing Identity.

mac$ platform=/Developer/Platforms/iPhoneOS.platform mac$ allocate=${platform}/Developer/usr/bin/codesign_allocate mac$ export CODESIGN_ALLOCATE=${allocate} mac$ codesign -fs "Name" Program mac$ scp Program mobile@iphone:

Option #2: Pseudo-Signing

For me, the previous option just doesn't work. I do not use Macs to do my development and the entire codesign path requires not only a Mac but console access because codesign is, at some level, a graphical utility (the way it uses Keychain to get the signatures may prompt, with dialogs, for passwords). To get around this, I wrote a tool called ldid that, among other things, can generate the SHA1 hashes that are checked by Apple's iPhoneOS kernel. This tool is easily installed on the iPhone using Cydia or APT.

iphone# apt-get install ldid iphone$ scp user@desktop:Program . iphone$ ldid -S Program

Option #3: Disable Checks

Finally, an option that is really convenient for development purposes is just to disable the check. Now, technically, this disables a lot more than just the codesign check, and its also more disabling the penalty than the check itself. I have run my phone for a while in this state, but I have heard that in some (many?) configurations it causes problems: being unable to connect to insecure WiFi networks being the largest. This is done by using sysctl to deactivate the enforcement and can be undone either by resetting the variables back on or by rebooting the phone.

sysctl -w security.mac.proc_enforce=0 sysctl -w security.mac.vnode_enforce=0

As this does seem to cause some problems, I'll make a note about how to undo this (as it's really simple). You just need to reset the variables back to 1 or reboot the device (every time the phone starts these default back to on).

sysctl -w security.mac.proc_enforce=1 sysctl -w security.mac.vnode_enforce=1

Have more questions about code signatures? Send them to me and I might put up more information (if I have it) here. One thing I do intend to discuss is entitlements.

2008年10月10日星期五

Some random iPhone stuff

Some random iPhone stuff

Have fun ...

Using gdb to debug on the iPhone (no need for OSX)

The goal here is to debug application on real-hardware. To do that you must follow some conditions :

  • Get gdb itself

    Good news, gdb is now available on cydia. So you can install it very easily. It comes all setup with all the needed signature / entitlements to have full functionalities. If you're interested in building it yourself, you'll find the original apple sources and the needed patches / build scripts on the telesphoreo repository

    The other way (previously only way) is to grab it from the SDK. Inside the .dmg you will find a package named iPhoneHostSideTools.pkg. Inside, there is Platforms/iPhoneOS.platform/Developer/usr/libexec/gdb/gdb-arm-apple-darwin. Just copy it to the iPhone, it's a universal binary that supports armv6 just fine.
    gdb is under GNU licence so you should be free to redistribute the binary as well I think.

    For information, the original Apple source are here.

    To enhance functionality you should add some entitlements to the apple binary. Use theses : for codesign, for ldid. To apply them :

    codesign -s "iPhone developer" --entitlements gdb.xcent -f gdb-arm-apple-darwin

    or

    ldid -Sgdb.xml gdb-arm-apple-darwin
  • Taget armv6

    Apple's modified gdb is kinda picky. It will only work properly on ARMv6 arch. ARM isn't sufficient ... You can see the different in cpusubtype in the output of otool :

    lain:iPhone tnt$ otool -h test.arm
    test.arm:
    Mach header
    magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
    0xfeedface 12 0 0x00 2 12 860 0x00000085

    lain:iPhone tnt$ otool -h test.armv6
    test.armv6:
    Mach header
    magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
    0xfeedface 12 6 0x00 2 12 860 0x00000085

    To make sure of that you must make sure you have -arch armv6 (or -march=armv6 -mcpu=arm1176jzf-s, depending on your compiler) in you CFLAGS / LDFLAGS. Like this :

    /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.0 -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk test.c -o test
  • Sign the binary with entitlements

    Update: Actually if you used the entitlements I provide to sign gdb, or if you used the gdb of cydia, this step is no longer required.

    Next, you need to sign the binary you want to debug with entitlements. For details on that, see the 'Using XCode with Pwned iPhone', it's described there. I do it with codesign on OSX but ldid also support theses.

    If you're planning on doing it with ldid, the easier it to apt-get install it on your phone and do it there.

    ldid -Smyapp.xml myapp

    The myapp.xml is the XML file describing the entitlements. I generate them using a simple script but for this simple purpose you could just always use a static one that would look like this :





    application-identifier
    test
    get-task-allow



    Update : If you applied enhanced entitlements to gdb as described in the first point, you don't need entitlements on your binary.

Using XCode with Pwned iPhone

The idea here is to use the official SDK to create applications without having to pay for an 'official' certificate and provisioning profile.

Note that the instructions here are experimental. I can't be held responsible for whatever happens if you try them ... That being said, I can hardly imagine any issue that couldn't be solved by a DFU restore ...

Make Build & Go" + Debugging works

If you're not a fully registred / paying developer, you can use XCode to compile apps but some functions won't work. Like the 'Build & go' button, the automatic signing or the debugger. It's however possible to make them work ! Just follow the steps ...

  • Create a self-signed signing certificate

    Apple has a nice page explaining how to do that. Make sure to name your certificate 'iPhone developer'.

  • Add a custom build step to sign executables

    A pwned iPhone doesn't need a valid signature ... but it still needs one, or at least the hashes ... (for more details see on www.saurik.com). Jay Freeman made a small utility called ldid that add thoses hashes. However here we will use the official codesign utility, provided by Apple, with our self-signed identity.

    To make remote debugging work, we also need to add entitlements to the Application. This will be handled by codesign as well. We will however need a small python utility gen_entitlements.py to generate the entitlement file. Download it, place it somewhere on your disk and make it executable.

    So, to execute codesign properly during the build, you will need to add a custom build step to each of your XCode projects. Select the menu options "Project > New Build Phase > New Run Script Build Phase", and enter the following script (don't forget to replace /Users/youruser/bin by the correct path to gen_entitlements.py) :

    export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
    if [ "${PLATFORM_NAME}" == "iphoneos" ]; then
    /Users/youruser/bin/gen_entitlements.py "my.company.${PROJECT_NAME}" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent";
    codesign -f -s "iPhone developer" --resource-rules "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/ResourceRules.plist" \
    --entitlements "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/"
    fi
  • Remove signature checks from MobileInstallation & SpringBoard

    The final step is to bypass some security checks in some executable. The idea is simple to patch them, using a small software. Of course, after patching, you need to re-generate a signature for the new binaries to get loaded properly. All-in-all, it's easier to do all that on your Mac. So open a console and cut & paste the instructions (I assume you have ssh on the iPhone and that it's IP is 192.168.0.1) :

    The binary patch has been written for 2.0.0 and I didn't get a change to test/update it to 2.0.1 ... so make sure you have a backup of the files and an active ssh if needed

    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    osx:~ user$
    mkdir iphone_tmp
    cd iphone_tmp
    scp root@192.168.0.1:/System/Library/PrivateFrameworks/MobileInstallation.framework/MobileInstallation .
    scp root@192.168.0.1:/System/Library/CoreServices/SpringBoard.app/SpringBoard .
    cp MobileInstallation MobileInstallation.bak
    cp SpringBoard SpringBoard.bak
    curl -O http://www.246tNt.com/iPhone/iphone_binary_patch
    curl -O http://www.246tNt.com/iPhone/SpringBoard.xcent
    chmod +x ./iphone_binary_patch
    ./iphone_binary_patch
    export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
    codesign -s "iPhone developer" -f MobileInstallation
    codesign -s "iPhone developer" --entitlements SpringBoard.xcent -f SpringBoard
    scp MobileInstallation root@192.168.0.1:/System/Library/PrivateFrameworks/MobileInstallation.framework/MobileInstallation
    scp SpringBoard root@192.168.0.1:/System/Library/CoreServices/SpringBoard.app/SpringBoard

    Finally, reboot the phone ... and enjoy :) Note that you may have to restart XCode and re-plug the iPhone for the connection to work. Also, if you had done previous attempts without following this how-to, you might need to clear the /var/mobile/Media/PublicStaging directory on the iPhone.

2008年10月9日星期四

iTunes下载的.ipsw存放的位置

/Users/Your Name/Library/iTunes/iPod Software Updates