I have successfully implemented deep linking in iOS app by Facebook. But I want to redirect the link opened by the Facebook post to a particular screen in my iOS app.
For this I am following this link : https://developers.facebook.com/docs/applinks/ios
So the openURL looks like this:
- (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;
}
The problem is that, the url is not getting parsed. I have mentioned in the code above: query
is nil
.
[url query]
- Here url is : fb654127854605525://authorize/#target_url=http%3A%2F%2FmyDomain%2FmyApp%2Fregister.php%3Fcampaign_id%3D999%26l_id%3D133%26share%3Dtrue%26customer_id%3DNDQ1%26domain%3D1
Where am I getting wrong? Is there anything wrong with the url?