首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

程序间通讯的实现

2012-06-26 
程序间通信的实现在iOS里,程序之间都是相互隔离的,目前并没有一个有效的方式来做程序间通信,幸好iOS程序可

程序间通信的实现

在iOS里,程序之间都是相互隔离的,目前并没有一个有效的方式来做程序间通信,幸好iOS程序可以很方便的注册自己的URL Scheme,这样就可以通过打开特定URL的方式来传递参数给另外一个程序。

?

至于,如何注册自己的URL Scheme,可以参考:自定义URL Scheme。

?

下面,看一个具体的示例。

?

假设有两个应用:A和B,A应用有两个UITableView,一个是字母类型的,另一个是数字类型的,B应用有两个按钮,一个用来打开A应用的字母列表,另一个用来打开A应用的数字列表,其中,A应用的Info.plist信息如下:


程序间通讯的实现
?

A应用的所有代码如下:

?

NavigationAppDelegate.h

?

#import <UIKit/UIKit.h>@class NavigationViewController;@interface NavigationAppDelegate : NSObject <UIApplicationDelegate> {UIWindow *window;UINavigationController *viewController;}@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic, retain) IBOutlet UINavigationController *viewController;@end

?

NavigationAppDelegate.m

?

#import "NavigationAppDelegate.h"#import "NavigationViewController.h"#import "NumberViewController.h"@implementation NavigationAppDelegate@synthesize window;@synthesize viewController;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    NavigationViewController *nav = [[NavigationViewController alloc] init];viewController = [[UINavigationController alloc] initWithRootViewController:nav];[self.window addSubview:viewController.view];[self.window makeKeyAndVisible];[nav release];return YES;}- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {if ([[url host] isEqualToString:@"com.aurora.nav"]) {NSString *viewId = [[url query] substringFromIndex:[[url query] rangeOfString:@"viewId="].location + 7];if ([viewId isEqualToString:@"letters"]){NavigationViewController *nav = [[NavigationViewController alloc] init];[self.viewController pushViewController:nav animated:YES];[nav release];} else {NumberViewController *nav = [[NumberViewController alloc] init];[self.viewController pushViewController:nav animated:YES];[nav release];}}return YES;}- (void)dealloc {[viewController release];[window release];[super dealloc];}@end

?

NavigationViewController.h

?

#import <UIKit/UIKit.h>@interface NavigationViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {NSArray *array;}@property(nonatomic, retain) NSArray *array;@end

?

NavigationViewController.m

?

#import "NavigationViewController.h"@implementation NavigationViewController@synthesize array;- (void)viewDidLoad {[super viewDidLoad];self.title = @"Letters";NSArray *letters = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", nil];self.array = letters;[letters release];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [array count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}cell.textLabel.text = [array objectAtIndex:[indexPath row]];return cell;}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}- (void)viewDidUnload {array = nil;}- (void)dealloc {[array release];array = nil;[super dealloc];}@end

?

NumberViewController.h

?

#import <UIKit/UIKit.h>@interface NumberViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {NSArray *array;}@property(nonatomic, retain) NSArray *array;@end

?

NumberViewController.m

?

#import "NumberViewController.h"@implementation NumberViewController@synthesize array;- (void)viewDidLoad {[super viewDidLoad];self.title = @"Numbers";NSArray *numbers = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];self.array = numbers;[numbers release];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [array count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}cell.textLabel.text = [array objectAtIndex:[indexPath row]];return cell;}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}- (void)viewDidUnload {array = nil;}- (void)dealloc {[array release];array = nil;[super dealloc];}@end

?

B应用的所有代码如下:

?

RequestViewController.h

?

#import <UIKit/UIKit.h>@interface RequestViewController : UIViewController {}- (IBAction) goToLetters;- (IBAction) goToNumbers;@end

?

RequestViewController.m

?

#import "RequestViewController.h"@implementation RequestViewController- (IBAction) goToLetters{NSString *str = @"nav://com.aurora.nav?viewId=%@";NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:str, @"letters"]];[[UIApplication sharedApplication] openURL:url];}- (IBAction) goToNumbers{NSString *str = @"nav://com.aurora.nav?viewId=%@";NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:str, @"numbers"]];[[UIApplication sharedApplication] openURL:url];}@end

?

热点排行