在移动通信领域,短信服务仍然是一个重要的组成部分。本文将介绍如何开发一个简单易用的短信发送应用,该应用能够通过API向全球发送短信。将从应用的基本界面设计开始,逐步深入到后端逻辑的实现,包括如何与设备地址簿进行交互,以及如何处理可能出现的错误。
短信发送应用界面非常简洁,用户只需输入发送者名称或号码、接收者国家代码和电话号码以及消息内容,然后点击“发送”按钮即可。发送成功后,会弹出一个确认号码。
在应用的核心逻辑中,定义了一个名为“send”的函数,该函数负责构建发送短信的请求URL。URL的构建包括用户名称、密码、国家代码、接收者号码、发送者名称以及消息内容。构建完成后,通过NSURLConnection发送请求,并处理响应。
void send() {
@autoreleasepool {
NSString *url = [NSString stringWithFormat:
@"http://sms1.cardboardfish.com:9001/HTTPSMS?S=H&UN=%@&P=%@&DA=%@%@&SA=%@&M=%@&DC=4",
"",
"", txtCountryCode.text,
txtDestinationNo.text, txtName.text,[self stringToHex:txtMessage.text]];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"url %@", url);
NSError* connError = 0;
NSURLResponse* response;
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&connError];
NSString* resp = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response: %@", resp);
}
}
为了简化用户选择联系人的过程,使用了名为“openContacts”的函数,该函数利用了ABPeoplePickerNavigationController类。这个类允许用户从地址簿中选择联系人或其联系信息。
- (IBAction)openContacts:(id)sender {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
NSArray *displayedItems = [NSArray arrayWithObjects:
[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
picker.displayedProperties = displayedItems;
[self presentModalViewController:picker animated:YES];
[picker release];
}
为了增强用户体验,在应用中添加了国家国旗的展示功能。收集了226个国家的国旗图片,并以国家代码命名。例如,德国的国旗图片命名为49.png。
#import "CountryCodesViewController.h"
@interface CountryCodesViewController ()
@end
@implementation CountryCodesViewController
@synthesize mainViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"Flags"];
countryCodes = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:sourcePath error:NULL];
[countryCodes retain];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [countryCodes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
NSString *strFlagName = [countryCodes objectAtIndex:indexPath.row];
cell.textLabel.text = [strFlagName substringToIndex:[strFlagName length] - 4];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Flags/%@",
strFlagName]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *strFlagName = [countryCodes objectAtIndex:indexPath.row];
[mainViewController selectCountryCode:[strFlagName substringToIndex:[strFlagName length] - 4]];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)okPressed:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
@end
在发送短信的过程中,可能会遇到各种错误。例如,如果设备没有连接到互联网,希望提醒用户,并将短信放入队列,等待设备连接到互联网后再发送。此外,还需要处理其他可能的错误情况。
if (connError) {
NSLog(@"%@", [connError description]);
NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
[message setValue:txtCountryCode.text forKey:@"countryCode"];
[message setValue:txtDestinationNo.text forKey:@"destinationNo"];
[message setValue:txtName.text forKey:@"name"];
[message setValue:txtMessage.text forKey:@"message"];
[self performSelectorOnMainThread:@selector(addToQueue:)
withObject:message waitUntilDone:NO];
[message release];
UIAlertView* alert = [[UIAlertView alloc] init];
alert.title = @"Send SMS";
alert.message = @"Can't access Internet. The message will be queued to be sent later";
[alert addButtonWithTitle:@"Ok"];
[alert show];
[alert release];
}
else {
UIAlertView* alert = [[UIAlertView alloc] init];
alert.title = @"Send SMS";
alert.message = resp;
[alert addButtonWithTitle:@"Ok"];
[alert show];
[alert release];
if ([resp rangeOfString:@"OK"].location != NSNotFound) {
[self performSelectorOnMainThread:@selector(sendComplete)
withObject:nil waitUntilDone:YES];
}
}
[resp release];
}