4

I'm creating an app that uses a custom url scheme, I have it all set up and it works when opening the app up, however, I now want to be able to add a single string to the url so that the person that opens the app can see that string. I'm really struggling with this, can someone help me please?

Here is my code

- (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;
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Send Challenge"])
    {        

        [FBWebDialogs presentRequestsDialogModallyWithSession:nil
                                                      message:[NSString stringWithFormat:@"I just scored %i points on this great game, called SumsUp. can you beat it?!", gameScore]
                                                        title:nil
                                                   parameters:nil
                                                      handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {

             if (error)
             {
                 // Error launching the dialog or sending the request.
                 NSLog(@"Error sending request.");
             }
             else
             {
                 if (result == FBWebDialogResultDialogNotCompleted)
                 {
                     // User clicked the "x" icon
                     NSLog(@"User canceled request.");
                 }
                 else
                 {
                     // Handle the send request callback
                     NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
                     if (![urlParams valueForKey:@"request"])
                     {
                         // User clicked the Cancel button
                         NSLog(@"User canceled request.");
                     }
                     else
                     {
                         // User clicked the Send button
                         NSString *requestID = [urlParams valueForKey:@"request"];
                         NSLog(@"Request ID: %@", requestID);
                     }
                 }
             }
         }];
    }

I have got the custom url setup in the P-List.

in my app delegate I have:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    UIAlertView *alertView;
    NSString *text = [NSString stringWithFormat: @"url recieved: %@", url];
    alertView = [[UIAlertView alloc] initWithTitle:@"" message:text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];

    return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication withSession:[PFFacebookUtils session]];

}

I hope this makes sense and somebody can help me. If any more information is required please let me know?

Thanks Graham

2
  • You haven't said what it does wrong. Show the app delegate calling parseURLParams:
    – Wain
    Mar 1, 2014 at 11:00
  • Thanks, I have edited the question to show the app delegate call. This all works and doesn't cause any problems, I just can't work out how to add the string that I want to send to the url. Mar 1, 2014 at 11:17

2 Answers 2

2

I had problem with custom URL Scheme on ios v. 9.x.x. when i tried open Youtube URL by App. I have found interesting fact when i have browsed through network.

iOS 9 requires your app to pre-register application schemes it intends to call.

  • Open your YourApp-Info.plist file and add the key, LSApplicationQueriesSchemes.
  • List item under LSApplicationQueriesSchemes, add a new item with the value youtube (in my case).

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>youtube</string>
</array>

0
1

My solution is simpler. I hope you will find it useful.

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    // Example URL: myapp://myapp.com/showString/yourString

    BOOL isMyAppURL = [[url scheme] isEqualToString:@"myapp"];

    if (isMyAppURL)
    {
        NSArray* pathComponents = [url pathComponents];
        NSString *command = pathComponents[0];

        // Check for showString command.
        if ([command isEqualToString:@"showString"])
        {
            NSString *stringToShow = [pathComponents[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSLog(@"String to show: %@", stringToShow);
        }
    }

    return isMyAppURL;
}

This should point you to the right direction.

1
  • Thank you for the response, I did just try that. the problem I am facing with it is when I change my url from fb[my_facebook_app_ID] to fb???????????????://myapp.com/showString/yourString (for example) in my p-list, trying to open the app up links to another app I have available in the app store! am I being completely stupid? Mar 1, 2014 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.