2015-12-10 09:11:07
来 源
中存储网
Ubuntu
想开发iPhone应用,没有Mac Book也没有Mac Mini,怎么办? 想尝试在Linux上开发iPhone吗,那就请接着往下看。 1.前提条件: 在Ubuntu上编译好toolchain 因为不能跑模拟器,需要一部iPhone手机来测试程序,iPhone3或iPhone4都可以 (目前支持到iPhone 3.1SDK

想开发iPhone应用,没有Mac Book也没有Mac Mini,怎么办?

想尝试在Linux上开发iPhone吗,那就请接着往下看。

1.前提条件:

在Ubuntu上编译好toolchain

因为不能跑模拟器,需要一部iPhone手机来测试程序,iPhone3或iPhone4都可以

(当前支持到iPhone 3.1SDK,程序可以在iPhone4上运行)

2.从UIApplication派生你的应用程序类:

头文件MyScylla.h

#import <CoreFoundation/CoreFoundation.h>  
#import <UIKit/UIKit.h>  
@class MyMainView;  
@interface MyScylla : UIApplication  
{  
UIWindow *window;  
MyMainView *mainView;  
}  
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MyMainView *mainView;
- (void)applicationDidFinishLaunching:(UIApplication *)application;  
- (void)dealloc;  
- (void) redirectConsoleLogToDocumentFolder;  
@end 

m文件MyScylla.m

#import <Foundation/Foundation.h>  
#import <UIKit/UIKit.h>  
#import <UIKit/UIAlert.h>  
#import "MyMainView.h"  
#import "MyScylla.h"  
int main(int argc, char* argv[])  
{  
  
NSAutoreleasePool *autoreleasePool = [  
[ NSAutoreleasePool alloc ] init  
];  
int returnCode = UIApplicationMain(argc, argv, @"MyScylla", @"MyScylla");  
[ autoreleasePool release ];  
return returnCode;  
}  
 
@implementation MyScylla  
@synthesize window;  
@synthesize mainView;  
- (void)applicationDidFinishLaunching:(UIApplication *)application {  
NSLog(@"applicationDidFinishLaunching");  
[self redirectConsoleLogToDocumentFolder];  
  
// 可以用bounds方法会得到整个屏幕区域,包括状态栏  
CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];  
  
// 然后用initWithFrame创建UIWindow对象  
window = [ [ UIWindow alloc ] initWithFrame: screenBounds ];  
// 用applicationFrame方法返回可显示区域,不包括状态栏  
CGRect rect = [ [ UIScreen mainScreen ] applicationFrame ];  
rect.origin.x = rect.origin.y = 0.0f;  
mainView = [[MyMainView alloc]init];  
[window addSubview: mainView];  
[window makeKeyAndVisible];  
}  
- (void)dealloc {  
[window release];  
[mainView release];  
[super dealloc];  
}  
- (void) redirectConsoleLogToDocumentFolder {  

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUSErDomainMask, YES);  
NSString *documentsDirectory = [paths objectAtIndex:0];  
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"myscylla_log.txt"];  
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);  
}  
@end

3.定义自己的ViewController

CustomViewController.h

#import <UIKit/UIKit.h>  
@class MyMainView;  
@interface CustomViewController: UIViewController {  
UITextView *textView;  
MyMainView *mainView;  
}  
- (void)viewDidLoad;  
- (void)dealloc;
@end 

CustomViewController.m

#import <UIKit/UIKit.h> 
#import "MyMainView.h" 
#import "CustomViewController.h"  
@implementation CustomViewController  
- (void)viewDidLoad {  
[super viewDidLoad];  
self.view.backgroundColor = [UIColor blueColor];  
CGRect rect = [ [ UIScreen mainScreen ] applicationFrame ];  
mainView = [ [ MyMainView alloc ] initWithFrame: rect ];
[self.view addSubview: mainView];  
}  
- (void)dealloc {
[super dealloc ];
}
@end 

4.定义View

MyMainView.h

#import <UIKit/UIKit.h>  
#import <UIKit/UINavigationBar.h>  
@interface MyMainView : UIView {  
 
UINavigationBar *navBar;  
UINavigationItem *navItem;  
BOOL isMuted;  
UITextView *textView;  
UIButton *button;  
int count;  
}  
@property (nonatomic, retain) IBOutlet UINavigationBar *navBar;
@property (nonatomic, retain) IBOutlet UINavigationItem *navItem;
@property (nonatomic, retain) IBOutlet UITextView *textView;  
@property (nonatomic, retain) IBOutlet UIButton *button;  
-(IBAction) clickButton: (id) sender;  
- (void)AlertWindow:(NSString *)transValue;  
- (id)initWithFrame:(CGRect)rect;
- (void)dealloc;
- (UINavigationBar *)createNavBar: (CGRect) rect;  
- (void)setNavBar;  
- (void)clickButton:(id)inSender;  
- (void)clickButton:(id)inSender forEvent:(UIEvent *)event;  
//- (void)navigationBar: (UINavigationBar *)navbar buttonClicked:(int) button;  
@end 

MyMainView.m

#import <UIKit/UIKit.h>
#import <UIKit/UIAlert.h>

#import "MyMainView.h"

@implementation MyMainView

@synthesize navBar;
@synthesize navItem;
@synthesize textView;
@synthesize button;

- (id)initWithFrame:(CGRect)rect {
self = [ super initWithFrame: rect ];
if (self != nil) {
NSLog(@"start initWithFrame");
  
isMuted = NO;
count = 0;
//navBar = [ self createNavBar: rect];
//[ self setNavBar ];
//[ self addSubView: [navBar view] ];
//NSLog(@"after add navBar");
  
CGRect rectText = CGRectMake(100,100,300,100);
textView = [ [ UITextView alloc] initWithFrame: rectText ];
//[ textView setTextSize: 12 ];
[ textView setText: @"Hello, world!" ];
[ self addSubview: textView ];

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect = CGRectMake(120,400,100,50);
//[sendButton setFrame:rect];
//sendButton = [ [ UIButton alloc] initWithFrame: rect];
button.frame = rect;
[button setTitle:@"Test" forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickButton:)
forControlEvents:UIControlEventTouchUpInside];
//[button addTarget:self action:@selector(clickButton:forEvent:)
//forControlEvents:UIControlEventTouchUpInside];

[self addSubview:button];

}
NSLog(@"end initWithFrame in MyMainView");
return self;
}

-(void)AlertWindow:(NSString *)transValue{

UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:transValue message:transValue delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}

- (UINavigationBar *)createNavBar: (CGRect) rect {
NSLog(@"start createNavBar");
UINavigationBar *newNav = [ [ UINavigationBar alloc]
initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)
];

[ newNav setDelegate: self ];
  
// add our title
navItem = [ [UINavigationItem alloc] initWithTitle: @"My Navigation" ];
[ newNav pushNavigationItem: navItem];
[ navItem setTitle: @"Another Example" ];

NSLog(@"end createNavBar");
return newNav;
}
- (void)setNavBar {

if(isMuted == YES) {
[ navItem setTitle: @"Spouse (Muted)"];
[ navBar showLeftButton:nil withStyle: 0 rightButton:@"Mute" withStyle: 1];
} else {
[ navItem setTitle: @"Spouse"];
[ navBar showLeftButton:nil withStyle: 0 rightButton:@"Mute" withStyle: 0];
}
NSLog(@"end setNavBar");
}
-(IBAction) clickButton :(id)inSender {
count++;
NSLog(@"clickButton");

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Action invoked!" message:@"Button clicked!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}

-(IBAction) clickButton:(id)inSender forEvent:(UIEvent *)event {
NSLog(@"clickButton forEvent");
count++;
[inSender setTitle:[NSString stringWithFormat:@"count:%d", count]
forState:UIControlStateNormal];
}

/*
- (void)navigationBar: (UINavigationBar *)navbar buttonClicked:(int) button {
switch(button) {
case 1: // left
[ self AlertWindow: @"Left Button"];
break;
case 0: // right
[ self AlertWindow: @"Right Button"];
break;
}
}
*/

- (void)dealloc {
[textView release];
[navBar release];
[navItem release];
[super dealloc ];
}
@end

5.Makefile

PROJECT=MyScylla 
IP=root@192.168.1.33  
TOOL_CHAIN=/home/jack/iPhone/toolchain  
Sysroot=$(TOOL_CHAIN)/toolchain/sys  
CC = arm-apple-darwin9-gcc  
LD = $(CC)  
LDFLAGS = -lobjc   
-framework CoreFoundation   
-framework Foundation   
-framework UIKit   
-framework CoreGraphics   
-L$(Sysroot)/usr/lib  
CFLAGS =  
HEADERS= -I$(Sysroot)/usr/include/UIKit  
Target=$(PROJECT)  
Objects=MyScylla.o   
MyMainView.o
Package=$(PROJECT).app  
all:  $(Target)
$(Target): $(Objects)  
$(LD) $(LDFLAGS) -o $@ $^  
%.o:   %.m  
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@  
%.o:   %.c  
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@  
%.o:   %.cpp  
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@  
ldid: $(Target)  
./myldid.sh  
package: ldid  
cp -p MyScylla ./$(PROJECT).app/  
install: ldid package  
scp -r $(Package) $(IP):/var/root  
ssh $(IP) chmod -R 755 $(Package)
ssh $(IP) cp -rf $(Package) /Applications/  
ssh $(IP) killall SpringBoard  
clean:  
rm -f *.o $(Target)  
rm -f ./$(PROJECT).app/MyScylla 

6.编译

ldid.sh:

export CODESIGN_ALLOCATE=/home/jack/iPhone/toolchain/toolchain/pre/bin/arm-apple-darwin9-codesign_allocate
ldid -S MyScylla 

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。