2016-03-15 207 views
2

我正面臨一個奇怪的問題。 我使用xcode 7.2,iOS 9,在真實設備iphone 4S(不是模擬器)上工作。canOpenUrl失敗,但openUrl成功

我有2個應用程序,app1和app2。 app1應該使用url方案將數據發送到app2。 APP2已經很好宣佈計劃 APP1已經引用在plist中的方案(因爲它是在iOS9需要)

<key>LSApplicationQueriesSchemes</key> 
    <array> 
     <array> 
      <string>OpenLinkMyData</string> 
     </array> 
    </array> 

這裏是我使用的代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) , ^{ 

      // build the url, using the scheme name, and the data 
      // note that json is escaped from bad url chars. 
      NSString * MyJsonDataWellEscaped = [[SomeClass getJSonDataToExport] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
      NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"OpenLinkMyData://%@",MyJsonDataWellEscaped]]; 

      // Next line should allow test if the app able to manage that scheme is installed. 
      // BUT in our case, this allways returning false. 
      bool can = [[UIApplication sharedApplication] canOpenURL:url]; 
      NSLog(@"canOpenUrl = %@", [email protected]"true":@"false"); 
     }); 
// code of the app that do stuff... 
} 

我回去以下日誌: -canOpenURL:失敗的URL: 「OpenLinkMyData://(myJsonSuff)」 - 錯誤: 「這個程序是不允許查詢計劃OpenLinkMyData」 canOpenUrl =假

但是,如果我使用下面的代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) , ^{ 
      // build the url, using the scheme name, and the data 
      // not that json is escaped from bad url chars. 
      NSString * MyJsonDataWellEscaped = [[Move2MyMHelper getJSonDataToExport] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
      NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"OpenLinkMyData://%@",MyJsonDataWellEscaped]]; 

      if([[UIApplication sharedApplication] openURL:url]) 
       { 
       NSLog(@"App launched OK"); 
       } 
      else 
       { 
       NSLog(@"App not launched"); 
       } 

     }); 

     // code of the app that do stuff... 
} 

如果我不檢查方案,請和我直接使用它,應用2很好地打開,並根據需要獲取的所有數據。 (否則,如果app2沒有安裝,我得到「應用程序未啓動」日誌)。

這裏是應用2源接收數據(其工作方式等待):

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 
    NSString *prefixToRemove = @"OpenLinkMyData://"; 
    if(url != nil && [[url absoluteString] hasPrefix:prefixToRemove]) 
     { 
     NSString * urlStr = [url absoluteString]; 
     NSString * json = [urlStr substringFromIndex:[prefixToRemove length]]; 
     json = [json stringByRemovingPercentEncoding]; 
     NSLog(@"OpenLinkMyData with json : %@", json); 
      } 
    return YES; 
} 

什麼是我的情況與canOpenUrl問題?

感謝您的任何幫助。

+3

您是否嘗試過做'LSApplicationQueriesSchemes'是一個字符串數組,而不是字符串數組的數組? – Mats

+0

恥辱。我不知道從哪裏找到字符串數組的數組。但我已經搜索了幾個小時來解決這個問題,但沒有看到:(...請添加這個答案,以便我可以接受它,謝謝。 –

回答

2

製作LSApplicationQueriesSchemes是一個字符串數組,而不是字符串數組的數組:

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>OpenLinkMyData</string> 
</array>