我已成功通过Facebook在iOS应用中实现深层链接。但是我想将Facebook帖子打开的链接重定向到我的iOS应用程序中的特定屏幕。
为此,我关注此链接:https://developers.facebook.com/docs/applinks/ios
所以openURL看起来像这样:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
NSString *callingUrl=[[NSUserDefaults standardUserDefaults] stringForKey:@"url"];
if ([callingUrl isEqualToString:@"fb"]) {
BOOL urlWasHandled =
[FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:
^(FBAppCall *call) {
// Parse the incoming URL to look for a target_url parameter
NSString *query = [url query]; // **query is nil here**
NSDictionary *params = [self parseURLParams:query];
// Check if target URL exists
NSString *appLinkDataString = [params valueForKey:@"al_applink_data"];
if (appLinkDataString) {
NSError *error = nil;
NSDictionary *applinkData =
[NSJSONSerialization JSONObjectWithData:[appLinkDataString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&error];
if (!error &&
[applinkData isKindOfClass:[NSDictionary class]] &&
applinkData[@"target_url"]) {
NSString *targetURLString = applinkData[@"target_url"];
// Show the incoming link in an alert
// Your code to direct the user to the
// appropriate flow within your app goes here
[[[UIAlertView alloc] initWithTitle:@"Received link:"
message:targetURLString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
}];
return urlWasHandled;
}
parseURLParams:
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *val = [[kv objectAtIndex:1]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[params setObject:val forKey:[kv objectAtIndex:0]];
}
return params;
}
问题是,网址没有被解析。我在上面的代码中提到:query
是nil
。
[url query]
- 此处网址为:fb654127854605525://authorize/#target_url=http%3A%2F%2FmyDomain%2FmyApp%2Fregister.php%3Fcampaign_id%3D999%26l_id%3D133%26share%3Dtrue%26customer_id%3DNDQ1%26domain%3D1
我哪里出错了?这个网址有什么问题吗?
答案 0 :(得分:1)
我遇到了同样的问题。
替换:
NSString *query = [url query];
使用:
NSString *query = [url absoluteString];
然后你应该有解析的URL。
答案 1 :(得分:0)
使用Bolts框架解析URL。从第4版开始,此框架包含在FBSDK中。
答案 2 :(得分:0)
使用Bolts SDK解析FB URL的代码示例(用于RobP注释): ``
- (void)openURL:(NSURL *)url fromSource:(NSString *)source
{
NSString *s = [url resourceSpecifier];
if (([url query] == nil) && (s != nil))
{
NSRange r = [s rangeOfString:@"target_url="];
if (r.location != NSNotFound)
{
url = [NSURL URLWithString:[[s substringFromIndex:(r.location + r.length)] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
}
BFURL *parsedUrl = [BFURL URLWithInboundURL:url sourceApplication:source];
if (parsedUrl != nil)
{
NSLog(@"APP DATA=%@\nTARGET QUERY=%@\n",[[parsedUrl appLinkData] debugDescription],[[parsedUrl targetQueryParameters] debugDescription]);
if ([parsedUrl targetQueryParameters] != nil)
{
NSString *objectId = [parsedUrl targetQueryParameters][@"action_object_map"];
if (objectId == nil)
{
NSString *actionId = [parsedUrl targetQueryParameters][@"fb_action_ids"];
if (actionId != nil)
{
// Do something with FB Action Id
}
}
else
{
objectId = [objectId substringWithRange:NSMakeRange(1, [objectId length] - 2)];
// Do something with FB Object Id
}
}
}
}