`
jsntghf
  • 浏览: 2512784 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

程序间通信的实现

    博客分类:
  • 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

 

分享到:
评论

相关推荐

    电信设备-一种通过进程间通信实现软件日志实时监控的方法.zip

    本文档“一种通过进程间通信实现软件日志实时监控的方法”深入探讨了如何利用进程间通信(IPC,Inter-Process Communication)技术来达到这一目标。下面将详细阐述这个主题。 首先,我们需要理解什么是进程间通信。...

    socket 实现进程间通信

    本文将深入探讨如何利用socket来实现在同一台机器上三个进程间的通信,同时涉及select()函数、信号处理以及进程重启的技术。 首先,让我们理解什么是socket。Socket是网络编程中的接口,它允许应用程序发送和接收...

    MFC程序通过SendMessage或PostMessage实现进程间通信,实现很简单

    MFC(Microsoft Foundation Classes)是微软提供的一个C++库,用于简化Windows应用程序开发,包括对进程间通信的支持。本篇文章将深入探讨如何使用MFC中的`SendMessage`和`PostMessage`函数来实现简单的进程间通信。...

    linux进程间通信与同步.pdf

    在Linux这样的虚拟内存系统中,每个进程都有自己的地址空间,因此进程间通信是通过专门的机制来实现的。而在像UCOS这样的扁平内存系统中,所有任务共享相同的地址空间,使得进程间通信与同步机制可以在同一地址空间...

    C#与C++进程间通信

    本示例以“C#与C++进程间通信”为主题,利用命名管道(Named Pipe)作为通信媒介,实现了不同类型数据结构的高效传输。 命名管道是一种在操作系统中提供半双工或全双工通信的机制,适用于在同一台计算机上的进程间...

    跨平台进程间通信源码

    3. **示例程序**:演示如何使用这个库进行进程间通信的实例代码。 4. **文档**:包含API参考、使用指南和示例说明。 5. **配置文件**:用于编译和构建的Makefile或CMakeLists.txt。 6. **测试用例**:验证库功能是否...

    利用内存映射文件技术实现进程间通信

    利用内存映射文件技术实现进程间通信,自pudn程序员联合开发网下载

    使用 AIDL实现进程间通信

    总结来说,AIDL是Android中实现进程间通信的强大工具,它简化了服务的创建和调用,让开发者能够构建分布式系统,实现应用程序间的深度协作。理解并熟练掌握AIDL的使用,对于提升Android应用的复杂性和可扩展性至关...

    linux无亲缘关系间进程同步通信实现(互斥锁+条件变量+共享内存模式)

    总结来说,互斥锁、条件变量和共享内存是Linux下实现进程间通信的关键工具,它们共同确保了多进程环境下的数据安全和程序的正确执行。通过合理地使用这些工具,开发者能够构建出高效且可靠的多进程应用程序。在实际...

    Android 进程间通信实现原理分析

    只有你允许客户端从不同的应用程序为了进程间的通信而去访问你的service,以及想在你的service处理多线程,下面为大家详细介绍下

    VB实现进程间通信的源码

    在Visual Basic(VB)中实现进程间通信可以帮助开发者构建更复杂、多组件的应用程序。本资源包含两个子文件:“接收”和“发送”,分别代表IPC过程中的数据接收端和发送端。 在VB中,实现进程间通信有多种方法,...

    通过Windows窗口消息实现 QT进程间通信

    在编程领域,进程间通信(IPC, Inter-Process Communication)是多个独立运行的程序之间交换数据的重要手段。在Windows操作系统环境下,利用Windows窗口消息机制进行进程间通信是一种常见的方法,尤其当涉及到跨语言...

    C#实现进程间通信(使用消息队列实现)

    标题"**C#实现进程间通信(使用消息队列实现)**"主要探讨的是如何使用C#语言通过消息队列来实现在不同进程之间的通信。这种方法适用于那些需要进程独立性和消息缓冲的应用场景。 在C#中,我们可以使用System....

    UNIX网络编程_卷2_进程间通信

    本书详细介绍了UNIX系统中进程间通信(IPC)的各种形式,这些通信机制对于提高UNIX程序性能至关重要,同时是开发不同主机间网络应用程序的基础。 书中首先从Posix IPC和System V IPC的内部结构开始讨论。Posix IPC...

    Linux进程间通信

    在Linux操作系统中,进程间通信(IPC,Inter-Process ...总结来说,Linux进程间通信是实现多进程协同工作、提高系统效率的关键。理解并灵活运用各种IPC方式,有助于我们设计出高效、可靠的多线程和多进程应用程序。

    Linux进程间通信.pdf

    在现代操作系统如Linux中,进程间通信(IPC,Inter-Process Communication)是实现多进程协同工作的重要机制。通过IPC,不同进程能够共享信息、同步状态以及协作完成复杂的任务。本文将深入探讨Linux下的进程间通信...

    linux进程间通信ppt

    Linux的进程通信机制源自UNIX,融合了AT&T的System V IPC和BSD的套接字机制。UNIX IPC主要包括管道、FIFO和信号,System V IPC涉及消息队列、信号量和共享内存,而Posix IPC对应Posix消息队列、信号量和共享内存。...

    .net4.0多进程间共享内存实现通信

    在.NET Framework 4.0中,...总的来说,.NET 4.0的内存共享功能为多进程通信提供了一个强大的工具,尤其适合需要高速、低延迟数据交换的应用。通过理解和熟练运用这项技术,开发者可以构建更加高效和灵活的多进程系统。

    Messenger进程间通信

    在Android系统中,进程间通信(IPC,Inter-Process Communication)是实现不同应用程序或同一应用内不同进程之间数据共享和协同工作的重要手段。"Messenger进程间通信"是一种轻量级的IPC方式,常用于简单的双向通信...

    linux进程间通信讲义及代码.

    为了实现进程间的有效通信,Linux提供了多种机制,包括管道(Pipe)、有名管道(FIFO)、信号量(Semaphore)、消息队列(Message Queue)、共享内存(Shared Memory)、套接字(Socket)等。下面将对这些通信方式...

Global site tag (gtag.js) - Google Analytics