解锁 iPhone 开发:Objective - C 与真机测试全攻略
1. Objective - C 基础
1.1 UIAlertViewDelegate 协议处理
当在警告视图上有多个按钮,且想知道哪个按钮被点击时,需要处理 UIAlertViewDelegate 协议中定义的方法。可以在实例化 UIAlertView 类的同一类中实现该协议,也可以创建一个新类来实现,示例代码如下:
//---SomeClass.m---
@implementation SomeClass
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog([NSString stringWithFormat:@"%d", buttonIndex]);
}
@end
为确保警告视图知道在哪里查找该方法,需创建 SomeClass 的实例并将其设置为委托:
SomeClass *myDelegate = [[SomeClass alloc] init];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Hello"
message:@"This is an alert view"
delegate:myDelegate
cancelButtonTitle:@"OK"
otherButtonTitles:@"Option 1", @"Option 2", nil];
[alert show];
1.2 选择器(Selectors)
在 Objective - C 中,选择器是用于选择要为对象执行的方法的名称,用于标识方法。以下是一个动态创建 UIButton 对象并处理其事件的示例:
//---create a Button view---
CGRect frame = CGRectMake(10, 50, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action: @selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
action: 参数接受 SEL 类型的参数。也可以使用 NSSelectorFromString 函数创建 SEL 对象:
NSString *nameOfMethod = @"buttonClicked:";
SEL methodName = NSSelectorFromString(nameOfMethod);
[button addTarget:self action:methodName
forControlEvents:UIControlEventTouchUpInside];
注意,命名选择器时,要指定方法的全名。如果方法有一个或多个参数,需要在选择器中添加 “:”。
1.3 类别(Categories)
类别允许在不子类化现有类的情况下向其添加方法,也可用于重写现有类的实现。例如,为 NSString 类添加 isEmail 方法来测试字符串是否包含有效的电子邮件地址:
//---utils.h---
#import <Foundation/Foundation.h>
@interface NSString (stringUtils)
- (BOOL) isEmail;
@end
//---utils.m---
#import "Utils.h"
@implementation NSString (Utilities)
- (BOOL) isEmail
{
NSString *emailRegEx =
@"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
@"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
@"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
@"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
@"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
@"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *regExPredicate = [NSPredicate
predicateWithFormat:@"SELF MATCHES %@",
emailRegEx];
return [regExPredicate evaluateWithObject:self];
}
@end
可以使用新添加的方法测试电子邮件地址的有效性:
NSString *email = @"weimenglee@gmail.com";
if ([email isEmail])
NSLog(@"Valid email");
else
NSLog(@"Invalid email");
2. 在真机上测试 iPhone 应用
2.1 注册 iPhone 开发者计划
要在真机上测试应用,首先需要在 iPhone 开发者计划 注册。有标准和企业两种计划可供选择,对于大多数想在 App Store 发布应用的开发者,99 美元的标准计划就足够了。
2.2 启动 Xcode
为在设备上测试 iPhone 应用,需要从 iPhone 开发者计划门户获取 iPhone 开发证书。具体步骤如下:
1. 连接设备到 Mac 并启动 Xcode,选择 Window ➪ Organizer 启动组织者应用,复制设备的 40 字符标识符并保存。
2. 生成证书签名请求(Certificate Signing Request) :
- 使用位于 Applications/Utilities/ 文件夹中的 Keychain Access 应用程序。
- 选择 Keychain Access ➪ Certificate Assistant 并选择 Request a Certificate From a Certificate Authority 。
- 在证书助手窗口中,输入电子邮件地址,选择 Saved to Disk 单选按钮,并选择 Let Me Specify Key Pair Information 复选框,点击 Continue 。
- 选择 2048 位的密钥大小和 RSA 算法,点击 Continue 。
- 保存请求到文件,使用建议的默认名称并点击 Save 。
3. 登录 iPhone 开发者计划门户 :
- 生成证书签名请求后,登录 Apple 的 iPhone 开发中心,点击页面右侧的 iPhone Developer Program Portal 链接(需支付 99 美元才能访问此页面)。
- 点击 Launch Assistant 按钮,按照提示操作:
- 创建 App ID,输入友好名称描述此 App ID,点击 Continue 。
- 提供设备 ID,点击 Continue 。
- 提交证书签名请求,点击 Choose File 选择之前创建的文件,点击 Continue 。
- 为配置文件提供描述,点击 Generate 。
- 下载生成的配置文件到 Mac,将其拖放到 Xcode 中,安装到连接的设备上。
- 下载并安装开发证书到设备,在 Keychain Access 应用程序中验证证书是否安装正确。
4. 在 Xcode 中,找到 Active SDK 项,若不在工具栏上,选择 View ➪ Customize Toolbar 添加。选择当前连接设备的 OS 版本号,按 Command - R 运行应用,点击 Allow 或 Always Allow 进行签名。
以下是在真机上测试应用的流程图:
graph LR
A[注册 iPhone 开发者计划] --> B[启动 Xcode]
B --> C[获取设备标识符]
C --> D[生成证书签名请求]
D --> E[登录 iPhone 开发者计划门户]
E --> F[创建 App ID]
F --> G[提供设备 ID]
G --> H[提交证书签名请求]
H --> I[生成配置文件]
I --> J[下载配置文件并安装到设备]
J --> K[下载并安装开发证书]
K --> L[在 Xcode 中选择设备版本]
L --> M[运行应用并签名]
通过以上步骤,你可以在真机上测试 iPhone 应用,确保应用在真实设备上正常运行。同时,掌握 Objective - C 的基础概念,如选择器和类别,能帮助你更好地进行 iPhone 应用开发。
3. 常见开发元素及操作
3.1 视图与控制器
3.1.1 视图操作
在 iPhone 开发中,视图的操作非常重要。以下是一些常见的视图操作:
- 动态添加视图 :可以使用代码动态添加视图,例如添加 UIButton 视图:
CGRect frame = CGRectMake(10, 50, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
- 切换视图 :可以通过设置过渡样式来实现视图的切换,如水平翻转:
// 设置过渡样式
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
// 切换视图
[self.view addSubview:newView];
[UIView commitAnimations];
- 视图动画 :使用
UIView的动画方法可以实现视图的动画效果,如旋转、缩放等:
// 旋转动画
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 4);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
view.transform = transform;
[UIView commitAnimations];
3.1.2 视图控制器
视图控制器在管理视图方面起着关键作用。以下是一些视图控制器的操作:
- 添加视图控制器 :可以通过代码或 Interface Builder 添加视图控制器。例如,在代码中添加视图控制器:
MySecondViewController *secondVC = [[MySecondViewController alloc] init];
[self addChildViewController:secondVC];
[self.view addSubview:secondVC.view];
[secondVC didMoveToParentViewController:self];
- 关联窗口和视图控制器 :在创建窗口时,可以将其与视图控制器关联起来:
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyViewController *viewController = [[MyViewController alloc] init];
window.rootViewController = viewController;
[window makeKeyAndVisible];
3.2 数据处理与存储
3.2.1 数据库操作
在 iPhone 开发中,SQLite3 是常用的数据库。以下是一些基本的数据库操作:
- 打开数据库 :
- (void)openDB {
NSString *docsDir;
NSArray *dirPaths;
// 获取文档目录
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"myDatabase.db"]];
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK) {
NSLog(@"数据库打开成功");
} else {
NSLog(@"数据库打开失败");
}
}
- 创建表 :
- (void)createTableNamed:(NSString *)tableName withField1:(NSString *)field1 withField2:(NSString *)field2 {
NSString *createSQL = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ TEXT, %@ TEXT);", tableName, field1, field2];
const char *create_stmt = [createSQL UTF8String];
if (sqlite3_prepare_v2(database, create_stmt, -1, &statement, NULL) == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_DONE) {
NSLog(@"表创建成功");
} else {
NSLog(@"表创建失败");
}
}
sqlite3_finalize(statement);
}
- 插入记录 :
- (void)insertRecordIntoTableNamed:(NSString *)tableName withField1:(NSString *)field1 field1Value:(NSString *)value1 andField2:(NSString *)field2 field2Value:(NSString *)value2 {
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO %@ (%@, %@) VALUES (?, ?);", tableName, field1, field2];
const char *insert_stmt = [insertSQL UTF8String];
if (sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [value1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [value2 UTF8String], -1, SQLITE_TRANSIENT);
if (sqlite3_step(statement) == SQLITE_DONE) {
NSLog(@"记录插入成功");
} else {
NSLog(@"记录插入失败");
}
}
sqlite3_finalize(statement);
}
- 查询记录 :
- (void)getAllRowsFromTableNamed:(NSString *)tableName {
NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM %@;", tableName];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *field1Value = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
NSString *field2Value = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSLog(@"Field1: %@, Field2: %@", field1Value, field2Value);
}
}
sqlite3_finalize(statement);
}
3.2.2 文件操作
文件操作包括读取和写入文件。以下是一些文件操作的示例:
- 写入文件 :
- (void)writeToFile:(NSString *)filePath withData:(NSString *)data {
[data writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
- 读取文件 :
- (NSString *)readFromFile:(NSString *)filePath {
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
return content;
}
3.3 事件处理
3.3.1 触摸事件
触摸事件是 iPhone 应用中常见的事件。以下是处理触摸事件的示例:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
NSLog(@"触摸开始,位置: (%f, %f)", location.x, location.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
NSLog(@"触摸移动,位置: (%f, %f)", location.x, location.y);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
NSLog(@"触摸结束,位置: (%f, %f)", location.x, location.y);
}
3.3.2 加速计事件
加速计事件可以用于检测设备的运动。以下是处理加速计事件的示例:
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (nonatomic, strong) CMMotionManager *motionManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.motionManager = [[CMMotionManager alloc] init];
if (self.motionManager.isAccelerometerAvailable) {
self.motionManager.accelerometerUpdateInterval = 0.1;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
if (error) {
NSLog(@"加速计数据获取失败: %@", error.localizedDescription);
} else {
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"X: %f, Y: %f, Z: %f", acceleration.x, acceleration.y, acceleration.z);
}
}];
}
}
@end
4. 总结
通过上述内容,我们全面了解了 iPhone 开发中的 Objective - C 基础,包括 UIAlertViewDelegate 协议处理、选择器和类别等概念。同时,详细介绍了在真机上测试 iPhone 应用的步骤,以及常见的开发元素和操作,如视图与控制器的管理、数据处理与存储、事件处理等。掌握这些知识和技能,能够帮助开发者更好地进行 iPhone 应用开发,创建出功能丰富、用户体验良好的应用程序。在实际开发中,开发者可以根据具体需求灵活运用这些技术,不断探索和创新,提升自己的开发水平。
以下是一个总结表格:
| 主题 | 关键内容 |
| ---- | ---- |
| Objective - C 基础 | UIAlertViewDelegate 协议处理、选择器、类别 |
| 真机测试 | 注册开发者计划、获取证书、安装配置文件和证书、运行应用 |
| 视图与控制器 | 视图操作(添加、切换、动画)、视图控制器管理(添加、关联) |
| 数据处理与存储 | 数据库操作(打开、创建表、插入记录、查询记录)、文件操作(读取、写入) |
| 事件处理 | 触摸事件、加速计事件 |
通过不断实践和学习,开发者将能够更加熟练地运用这些技术,开发出高质量的 iPhone 应用。
超级会员免费看
1638

被折叠的 条评论
为什么被折叠?



