環境: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平臺無法直接通過文件系統來分享數據。
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來進行應用間通訊,而對于數據的傳輸,可以使用剪貼板來進行,可以選擇成熟的數據結構序列化反序列化方案來封裝通訊及數據傳輸協議,可以定義回調方法