環(huán)境:ios9
重要的提示:
應(yīng)用A:和應(yīng)用B:都同時(shí)寫(xiě)上同樣的url schemes為URLSA,然后在應(yīng)用C中通過(guò)]openURL:@“URLSA”,那么此時(shí)應(yīng)用會(huì)跳到哪個(gè)應(yīng)用了?
答案是,應(yīng)用A或者應(yīng)用B,誰(shuí)先安裝到手機(jī),然后點(diǎn)擊應(yīng)用C就是跳到先安裝的(A或者B)應(yīng)用里去。
常用的app啟動(dòng)有2種,
1:點(diǎn)擊圖標(biāo)啟動(dòng);
{
1:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
【launchOptions 沒(méi)有值】
}
2:通過(guò)url打開(kāi)
{
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];來(lái)進(jìn)行打開(kāi)另一個(gè)app
URL Scheme是類(lèi)似http://, ftp://這樣的東西,同樣你也可以為自己的應(yīng)用自定URL Scheme,其他應(yīng)用通過(guò)此標(biāo)識(shí)就可以訪問(wèn)你的應(yīng)用,如果自定的URL Scheme 和系統(tǒng)應(yīng)用的相同,則會(huì)調(diào)用系統(tǒng)應(yīng)用,而不會(huì)調(diào)用自定的應(yīng)用程序。
例如: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(應(yīng)用的名字、例如weixin)
3:在 url types 的URL Schemes 添加TestB(例如微信的urlschema是wx0fff8fc7685bb2c6)
代碼:
vc中的點(diǎn)擊:
- (IBAction)show:(id)sender {
NSLog(@"打開(kāi)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傳過(guò)來(lái)的值
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{
NSString *strs = [[url host] stringByRemovingPercentEncoding];
NSLog(@"B穿過(guò)來(lái)的值--》%@",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中的點(diǎn)擊:
- (IBAction)show:(id)sender {
NSLog(@"打開(kāi)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傳過(guò)來(lái)的值
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{
NSString *strs = [[url host] stringByRemovingPercentEncoding];
NSLog(@"B穿過(guò)來(lái)的值--》%@",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穿過(guò)來(lái)的值strs-》%@",strs);
NSLog(@"A穿過(guò)來(lái)的值strs1-》%@",strs1);
NSLog(@"A穿過(guò)來(lái)的值strs2-》%@",strs2);
NSLog(@"A穿過(guò)來(lái)的值strs3-》%@",strs3);
NSLog(@"A穿過(guò)來(lái)的值strs4-》%@",strs4);
NSLog(@"A穿過(guò)來(lái)的值strs5-》%@",strs5);
NSLog(@"A穿過(guò)來(lái)的值options--》%@",options);
return YES;
}
options:(NSDictionary *)options中的optiongs打印結(jié)果:
{
UIApplicationOpenURLOptionsOpenInPlaceKey = 0;
UIApplicationOpenURLOptionsSourceApplicationKey = "com.***.***";
}
=============
檢測(cè)當(dāng)前是否有有安裝軟件canOpenURL
BOOL iscanOpen =? [[UIApplication sharedApplication]openURL:url];
if (iscanOpen) {
NSLog(@"iscanOpe有安裝");
}else{
NSLog(@"iscanOpen沒(méi)有安裝");
[self addWebView];
//??????? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.baidu.com"]];
}
----------------
怎樣判斷iOS App是通過(guò)哪種途徑啟動(dòng)的?【http://www.cnblogs.com/daguo/p/3759514.html】
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
說(shuō)明:當(dāng)應(yīng)用程序啟動(dòng)時(shí)執(zhí)行,應(yīng)用程序啟動(dòng)入口。只在應(yīng)用程序啟動(dòng)時(shí)執(zhí)行一次。application參數(shù)用來(lái)獲取應(yīng)用程序的狀態(tài)、變量等,值得注意的是字典參數(shù):(NSDictionary *)launchOptions,該參數(shù)存儲(chǔ)程序啟動(dòng)的原因。
1.若用戶(hù)直接啟動(dòng),lauchOptions內(nèi)無(wú)數(shù)據(jù);
2.若由其他應(yīng)用程序通過(guò)openURL:啟動(dòng),則UIApplicationLaunchOptionsURLKey對(duì)應(yīng)的對(duì)象為啟動(dòng)URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey對(duì)應(yīng)啟動(dòng)的源應(yīng)用程序的bundle ID (NSString);
3.若由本地通知啟動(dòng),則UIApplicationLaunchOptionsLocalNotificationKey對(duì)應(yīng)的是為啟動(dòng)應(yīng)用程序的的本地通知對(duì)象(UILocalNotification);
4.若由遠(yuǎn)程通知啟動(dòng),則UIApplicationLaunchOptionsRemoteNotificationKey對(duì)應(yīng)的是啟動(dòng)應(yīng)用程序的的遠(yuǎn)程通知信息userInfo(NSDictionary);
其他key還有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,
UIApplicationLaunchOptionsNewsstandDownloadsKey。 如果要在啟動(dòng)時(shí),做出一些區(qū)分,那就需要在下面的代碼做處理。 比如:應(yīng)用可以被某個(gè)其它應(yīng)用調(diào)起(作為該應(yīng)用的子應(yīng)用),要實(shí)現(xiàn)單點(diǎn)登錄,那就需要在啟動(dòng)代碼的地方做出合理的驗(yàn)證,并跳過(guò)登錄。
【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應(yīng)用間通信和分享數(shù)據(jù)的機(jī)制
iOS平臺(tái)無(wú)法直接通過(guò)文件系統(tǒng)來(lái)分享數(shù)據(jù)。
1,使用UIDocumentInteractionController
受到UIDocumentInteractionController的UI設(shè)計(jì)限制,其只能支持最多6個(gè)第三方應(yīng)用,IOS6上UIDocumentInteractionController被拋棄了,取而代之的是UIActivityViewController,它提供了更靈活的解決方案
2,使用UIActivityViewController
上面提到了第一種方案在iOS6被拋棄了,取代方案就是UIActivityViewController,因此這和第一種方案非常類(lèi)似。在UI方面通過(guò)分頁(yè)面板解決了最多6個(gè)第三方應(yīng)用的問(wèn)題,另外你可以通過(guò)創(chuàng)建自己的UIActivity子類(lèi)來(lái)提供客制化的服務(wù)
3,使用KeychainGroup Access
自iOS3.0始我們?cè)谕患易宓腁pp間分享Keychain數(shù)據(jù),這里說(shuō)的同一家族的App指的是具有相同Bundle
Seed ID的應(yīng)用[蘋(píng)果制定的應(yīng)用ID是由兩部分組成,.
Identifier>]。
4,客制化的URLScheme
允許應(yīng)用間通過(guò)URL進(jìn)行數(shù)據(jù)傳輸。URL Scheme是iOS平臺(tái)目前應(yīng)用間通訊的常用解決方案。
5,Web Service
通過(guò)第三方服務(wù)(例如dropbox)或者自己定制的服務(wù)器來(lái)進(jìn)行數(shù)據(jù)分享,[當(dāng)然也可以在本地App內(nèi)創(chuàng)建Web
Server,但是如果App切入后臺(tái)之后,尤其是內(nèi)存吃緊時(shí),一切就變得不靠譜了]。
6,UIPasteBoard + URL Scheme
上面的方案或許足以滿(mǎn)足你的應(yīng)用需求,但這些方案或多或少存在某些明顯短板,都為另一潛在的解決方案留有余地。如果你想精確的控制App間數(shù)據(jù)通訊并且不
受離線的影響,可以選擇UIPasteBoard+URL
Scheme的方案。[遵循x-callback-url規(guī)范的應(yīng)用iPGMail就使用了這種方案]
像上面提到過(guò)的URL Scheme方案一樣,我們可以通過(guò)URL來(lái)進(jìn)行應(yīng)用間通訊,而對(duì)于數(shù)據(jù)的傳輸,可以使用剪貼板來(lái)進(jìn)行,可以選擇成熟的數(shù)據(jù)結(jié)構(gòu)序列化反序列化方案來(lái)封裝通訊及數(shù)據(jù)傳輸協(xié)議,可以定義回調(diào)方法