2016-03-15 210 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>