11

I'm having problems implementing this functionality after following Facebook's tutorial: https://developers.facebook.com/docs/howtos/share-appid-across-multiple-apps-ios-sdk/

Basically, I have 2 targets under the same project - one for the free version and one for the premium. I have created and set up a single Facebook app for this and added 2 url scheme suffixes:

two URL scheme suffices

Also, I've added the suffixes within the respective plist files. For example, the premium app is defined with:

screenshot of info.plist file

Where the "xxxxx..." is just my Facebook app ID.

After all this, I still can't log in to the application on iOS 5.0 and 6.0 (using native FB dialog or the SDK dialog within Safari). This is what I get when I log in via Safari:

alert with message "cannot open page, safari cannot open the page because the address is invalid"

The code doing the login is:

NSArray *permissions = kInitialPermissions;

BOOL result = NO;
FBSession *session = [[FBSession alloc] initWithAppID:@"xxxxxxxxxxx"
                                          permissions:@[]
                                      urlSchemeSuffix:@"premium"
                                   tokenCacheStrategy:nil];

if (allowLoginUI ||
    (session.state == FBSessionStateCreatedTokenLoaded)) {
    [FBSession setActiveSession:session];
}

result = [FBSession openActiveSessionWithReadPermissions:permissions
                                                allowLoginUI:allowLoginUI
                                           completionHandler:completion];
return result;

Any ideas will be appreciated. Thanks!

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Stavash
  • 14,244
  • 5
  • 52
  • 80

3 Answers3

1

As well as adding the fbxxxxxxxxxxpremium URL Scheme to the the URL Types array in the plist file (which you seem to be doing), add a new key/value:

key = FacebookUrlSchemeSuffix
value = premium

So in the plist file itself:

<key>FacebookUrlSchemeSuffix</key>
<string>premium</string>

This fixed it for me. The Facebook docs make reference to this but don't make it clear IMO.

Dunc
  • 18,404
  • 6
  • 86
  • 103
0

Your issue is that the custom URL scheme is not being recognized by iOS. Here are some possible causes:

  1. I see that you've correctly set the CFBundleURLSchemes key in your plist to an array of supported schemes. You also need to set CFBundleURLName to something like com.myApp.MyURLScheme. Have you done this?

  2. After calling will and did finish launching methods, iOS will call your app delegate's - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation method. You must also implement this method and it must return YES. Have you?

  3. Make sure your URL format is correct. fbxxxxxxpremium://foo=bar won't work, but fbxxxxxxpremium://?foo=bar will (the ? is mandatory).

Please note that the delegate methods will be slightly different if you are targeting iOS 4.1 or earlier.

For more info and sample code, see the Communicating with Other Apps section of Advanced App Tricks in the iOS SDK documentation.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks for your answer, Aaron. I tried several CFBundleURLNames as you suggested in 1 but I still don't get switched back to the app after successful login in Safari. 2 was already implemented in my AppDelegate. Any suggestions? – Stavash Feb 23 '13 at 16:39
  • What does the URL you're typing into Safari look like? Does it work if you pass no parameters? (that is, just type `fbxxxxxxpremium://`) – Aaron Brager Feb 23 '13 at 18:58
  • Typing "fbxxxxxxpremium://" into safari DOES switch back to the app. What's going on here?? :O – Stavash Feb 23 '13 at 21:31
  • You probably forgot the `?` after the `fbxxxxxxpremium://`. You can't pass parameters before the question mark. `fbxxxxxxpremium://foo=bar` won't work, but `fbxxxxxxpremium://?foo=bar` will. (I believe this more rigid behavior is new in iOS 6). – Aaron Brager Feb 23 '13 at 23:28
  • So after we figured this out, what am I supposed to define in my plist in order for the app to handle the said url scheme? – Stavash Feb 24 '13 at 08:14
  • I don't understand your question. If iOS is opening your app, then your plist is configured correctly. – Aaron Brager Feb 25 '13 at 03:45
  • Little misunderstanding - when I type fbxxxxxxpremium:// MANUALLY in Safari, it opens my app. – Stavash Feb 25 '13 at 07:32
  • And when you type fbxxxxxxpremium://?foo=bar in Safari, it also opens your app. The issue is that the URL you are passing to your app doesn't have the "?". In your return URL you specify for the Facebook app, you need to prepend your parameters with a question mark. – Aaron Brager Feb 26 '13 at 04:46
  • Are you testing this on iOS 5, or iOS 6? On iOS 6, Facebook should be getting the settings from iOS, not ever leaving your app. (according to https://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/#step4). Also, what happens if you pass `nil` to parameters instead of `@[]`? – Aaron Brager Feb 26 '13 at 04:59
  • Also, make sure you're running Facebook SDK 3.2: https://developers.facebook.com/blog/post/2013/02/25/facebook-sdk-3-2-for-ios/ – Aaron Brager Feb 26 '13 at 15:45
0

I think the problem is when you're calling :

result = [FBSession openActiveSessionWithReadPermissions:permissions
                                            allowLoginUI:allowLoginUI
                                       completionHandler:completion];

the session that is being opened doesn't have the url scheme suffix.

So you should call

[session openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent 
        completionHandler:completion];
Moxy
  • 4,162
  • 2
  • 30
  • 49
  • Thanks @Moxy, I tried this but am still getting the exact same result. I also have a problem with this approach - it's not equivalent to my call (no where to specify initial permissions) – Stavash Feb 24 '13 at 08:13
  • You can specify the permissions when you initialize the session. In your code your code you're passing an empty array. You can pass the permissions you want. – Moxy Feb 24 '13 at 12:12