在执行openUrl:
启动应该回调我的应用程序的应用程序之前(就像根据x-callback-url规范行事的应用程序一样),我怎样才能以编程方式检查我的应用程序回调是否正常工作打电话给另一个应用程序?
答案 0 :(得分:12)
这是我目前的解决方案:
- (BOOL) respondsToUrl:url
{
BOOL schemeIsInPlist = NO; // find out if the sceme is in the plist file.
NSBundle* mainBundle = [NSBundle mainBundle];
NSArray* cfBundleURLTypes = [mainBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"];
if ([cfBundleURLTypes isKindOfClass:[NSArray class]] && [cfBundleURLTypes lastObject]) {
NSDictionary* cfBundleURLTypes0 = [cfBundleURLTypes objectAtIndex:0];
if ([cfBundleURLTypes0 isKindOfClass:[NSDictionary class]]) {
NSArray* cfBundleURLSchemes = [cfBundleURLTypes0 objectForKey:@"CFBundleURLSchemes"];
if ([cfBundleURLSchemes isKindOfClass:[NSArray class]]) {
for (NSString* scheme in cfBundleURLSchemes) {
if ([scheme isKindOfClass:[NSString class]] && [url hasPrefix:scheme]) {
schemeIsInPlist = YES;
break;
}
}
}
}
}
BOOL canOpenUrl = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: url]];
return schemeIsInPlist && canOpenUrl;
}
限制是我们正在检查为该方案注册的此应用,某些应用程序响应该网址。
AFAIK这并不保证您的应用程序是该方案的实际响应者(如果另一个应用程序也注册了该方案)。
根据我的尝试,似乎iOS为每个独特的url方案打开了第一个安装的应用程序。
答案 1 :(得分:0)
以下是我在Swift中解决它的方法:
var plistPath = NSBundle.mainBundle().pathForResource("Info", ofType: "plist")
var urlStuff = NSMutableDictionary(contentsOfFile: plistPath!)
var urlType = NSDictionary(objectsAndKeys: "com.appprefix.AppName", "CFBundleURLName", NSArray(object: "fb123456789"), "CFBundleURLSchemes")
urlStuff?.setObject(NSArray(object: urlType), forKey: "CFBundleURLTypes")
urlStuff?.writeToFile(plistPath!, atomically: true)
答案 2 :(得分:0)
更简单的解决方案:
+ (BOOL)isBundleURL:(NSURL *)url
{
NSArray *urlTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
NSArray *urlSchemes = [urlTypes.firstObject objectForKey:@"CFBundleURLSchemes"];
return [urlSchemes containsObject:url.scheme];
}
+ (BOOL)respondsToURL:(NSURL *)url
{
return [self isBundleURL:url] && [[UIApplication sharedApplication] canOpenURL:url];
}