两个应用之间的跳转以及传值

环境:ios9

重要的提示:

应用A:和应用B:都同时写上同样的url schemes为URLSA,然后在应用C中通过]openURL:@“URLSA”,那么此时应用会跳到哪个应用了?

答案是,应用A或者应用B,谁先安装到手机,然后点击应用C就是跳到先安装的(A或者B)应用里去。

常用的app启动有2种,

1:点击图标启动;

{

1:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

【launchOptions 没有值】

}

2:通过url打开

{

1:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

【launchOptions 有值】

2:- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options;

有url

}

利用[[UIApplication sharedApplication]openURL:url];来进行打开另一个app

URL Scheme是类似http://, ftp://这样的东西,同样你也可以为自己的应用自定URL Scheme,其他应用通过此标识就可以访问你的应用,如果自定的URL Scheme 和系统应用的相同,则会调用系统应用,而不会调用自定的应用程序。

例如:invoking://com.hello/yourpath/?username=WT&password=123456&callback=myapp

其中invoking是URL Scheme 即[url scheme],

com.hello是host,即[url host],

yourpath是path,即[url path],

username=WT&password=123456&callback=myapp是query,即[url query]。

【链接格式:Native app URL string:

http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=156093464&id=156093462&s=143441】

【接收方的接收格式http://www.2cto.com/kf/201403/283996.html

-

(BOOL)application:(UIApplication *)application openURL:(NSURL *)url

sourceApplication:(NSString *)sourceApplication

annotation:(id)annotation

{

NSLog(@"%@", url);

if([[url scheme] isEqualToString:@"invoked"]) {

if([[url host] isEqualToString:@"com.hello"]) {

NSString *query = [url query];

NSArray *array = [query componentsSeparatedByString:@"&"];

NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10];

for(NSString *item in array) {

NSArray *valueArray = [item componentsSeparatedByString:@"="];

[dic setValue:[valueArray objectAtIndex:1] forKey:[valueArray objectAtIndex:0]];

}

[self application:application didFinishLaunchingWithOptions:dic];

}

returnYES;

}

returnNO;

}

【1

传:scheme://host/path/?name=txj&age=20

收:接收获取如上

2:

传:scheme://host/path/?将字典变成jsonString

收:怎么加密就怎么解密【将jsonString变成字典】

+ (NSMutableDictionary *)dictionaryWithJsonString:(NSString *)jsonString{

if (jsonString ==nil) {

return nil;

}

NSData * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSError * err;

NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];

if (err) {

NSLog(@"json解析失败:%@",err);

return nil;

}

return dic;

}

例子:

TestA:

配置

1:App Transport Security Settings Dic

Allow Arbitrary Loads bool=yes

2:LSApplicationQueriesSchemes array

添加 TestB(应用的名字、例如weixin)

3:在 url types 的URL Schemes 添加TestB(例如微信的urlschema是wx0fff8fc7685bb2c6)

代码:

vc中的点击:

- (IBAction)show:(id)sender {

NSLog(@"打开A");

NSString *urlString = [NSString stringWithFormat:@"TestB://%@",self.textB.text];

NSURL * url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];

if([[UIApplication sharedApplication] canOpenURL:url]) {

[[UIApplication sharedApplication] openURL:url];

}else{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"message"message:[NSString

stringWithFormat:@"%@", url] delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];

[alertView show];

}

}

在AppDelegate获取  从B传过来的值

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{

NSString *strs = [[url host] stringByRemovingPercentEncoding];

NSLog(@"B穿过来的值--》%@",strs);

return YES;

}

TestB:

配置

1:App Transport Security Settings Dic

Allow Arbitrary Loads bool=yes

2:LSApplicationQueriesSchemes array

添加 TestA

3:在 url types 的URL Schemes 添加TestB

代码:

vc中的点击:

- (IBAction)show:(id)sender {

NSLog(@"打开A");

NSString *urlString = [NSString stringWithFormat:@"TestB://%@",self.textB.text];

NSURL * url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];

if([[UIApplication sharedApplication] canOpenURL:url]) {

[[UIApplication sharedApplication] openURL:url];

}else{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"message"message:[NSString

stringWithFormat:@"%@", url] delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];

[alertView show];

}

}

在AppDelegate获取  从B传过来的值

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{

NSString *strs = [[url host] stringByRemovingPercentEncoding];

NSLog(@"B穿过来的值--》%@",strs);

return YES;

}

======

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{

NSString *strs = [[url host] stringByRemovingPercentEncoding];

NSNumber *strs1 = [url port] ;

NSString *strs2 = [[url password] stringByRemovingPercentEncoding];

NSString *strs3 = [[url fragment] stringByRemovingPercentEncoding];

NSString *strs4 = [[url query] stringByRemovingPercentEncoding];

NSString *strs5 = [[url relativePath] stringByRemovingPercentEncoding];

NSLog(@"A穿过来的值strs-》%@",strs);

NSLog(@"A穿过来的值strs1-》%@",strs1);

NSLog(@"A穿过来的值strs2-》%@",strs2);

NSLog(@"A穿过来的值strs3-》%@",strs3);

NSLog(@"A穿过来的值strs4-》%@",strs4);

NSLog(@"A穿过来的值strs5-》%@",strs5);

NSLog(@"A穿过来的值options--》%@",options);

return YES;

}

options:(NSDictionary *)options中的optiongs打印结果:

{

UIApplicationOpenURLOptionsOpenInPlaceKey = 0;

UIApplicationOpenURLOptionsSourceApplicationKey = "com.***.***";

}

=============

检测当前是否有有安装软件canOpenURL

BOOL iscanOpen =  [[UIApplication sharedApplication]openURL:url];

if (iscanOpen) {

NSLog(@"iscanOpe有安装");

}else{

NSLog(@"iscanOpen没有安装");

[self addWebView];

//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.baidu.com"]];

}

----------------

怎样判断iOS App是通过哪种途径启动的?【http://www.cnblogs.com/daguo/p/3759514.html】

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

说明:当应用程序启动时执行,应用程序启动入口。只在应用程序启动时执行一次。application参数用来获取应用程序的状态、变量等,值得注意的是字典参数:(NSDictionary *)launchOptions,该参数存储程序启动的原因。

1.若用户直接启动,lauchOptions内无数据;

2.若由其他应用程序通过openURL:启动,则UIApplicationLaunchOptionsURLKey对应的对象为启动URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey对应启动的源应用程序的bundle ID (NSString);

3.若由本地通知启动,则UIApplicationLaunchOptionsLocalNotificationKey对应的是为启动应用程序的的本地通知对象(UILocalNotification);

4.若由远程通知启动,则UIApplicationLaunchOptionsRemoteNotificationKey对应的是启动应用程序的的远程通知信息userInfo(NSDictionary);

其他key还有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,

UIApplicationLaunchOptionsNewsstandDownloadsKey。 如果要在启动时,做出一些区分,那就需要在下面的代码做处理。 比如:应用可以被某个其它应用调起(作为该应用的子应用),要实现单点登录,那就需要在启动代码的地方做出合理的验证,并跳过登录。

【http://www.cnblogs.com/letougaozao/p/3979096.html  参考】

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];

if(url)

{

}

NSString *bundleId = [launchOptions objectForKey:UIApplicationLaunchOptionsSourceApplicationKey];

if(bundleId)

{

}

UILocalNotification * localNotify = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if(localNotify)

{

}

NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if(userInfo)

{

}

}

/////////////----//////////

http://blog.csdn.net/tenfyguo/article/details/9063675

ios应用间通信和分享数据的机制

iOS平台无法直接通过文件系统来分享数据。

1,使用UIDocumentInteractionController

受到UIDocumentInteractionController的UI设计限制,其只能支持最多6个第三方应用,IOS6上UIDocumentInteractionController被抛弃了,取而代之的是UIActivityViewController,它提供了更灵活的解决方案

2,使用UIActivityViewController

上面提到了第一种方案在iOS6被抛弃了,取代方案就是UIActivityViewController,因此这和第一种方案非常类似。在UI方面通过分页面板解决了最多6个第三方应用的问题,另外你可以通过创建自己的UIActivity子类来提供客制化的服务

3,使用KeychainGroup Access

自iOS3.0始我们在同一家族的App间分享Keychain数据,这里说的同一家族的App指的是具有相同Bundle

Seed ID的应用[苹果制定的应用ID是由两部分组成,.

Identifier>]。

4,客制化的URLScheme

允许应用间通过URL进行数据传输。URL Scheme是iOS平台目前应用间通讯的常用解决方案。

5,Web Service

通过第三方服务(例如dropbox)或者自己定制的服务器来进行数据分享,[当然也可以在本地App内创建Web

Server,但是如果App切入后台之后,尤其是内存吃紧时,一切就变得不靠谱了]。

6,UIPasteBoard + URL Scheme

上面的方案或许足以满足你的应用需求,但这些方案或多或少存在某些明显短板,都为另一潜在的解决方案留有余地。如果你想精确的控制App间数据通讯并且不

受离线的影响,可以选择UIPasteBoard+URL

Scheme的方案。[遵循x-callback-url规范的应用iPGMail就使用了这种方案]

像上面提到过的URL Scheme方案一样,我们可以通过URL来进行应用间通讯,而对于数据的传输,可以使用剪贴板来进行,可以选择成熟的数据结构序列化反序列化方案来封装通讯及数据传输协议,可以定义回调方法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容