0

I tried to open facebook app using this

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"fb://"]];

when my app is running in foreground , it works fine but if my app is in background it didn't work .

help me out .

I'm very new in programming

Basheer_CAD
  • 4,908
  • 24
  • 36
user3152918
  • 31
  • 1
  • 4

2 Answers2

2

You are think impossible task. Think and first ask your self what are you trying.

  • Your app in to background it means (minimized). then where to trigger for calling open Facebook Native app method.

  • If app in background then you can't have any event for handle your app event from iPhone screen.

That bellow code for open Fb native app from app. but from background it out of logic think. that not relevant at all.

you can open facebook app using this bellow :-

- (IBAction)OpenFB: (id) sender
{
    NSURL* facebookURL = [NSURL URLWithString: @"fb://profile/157893597658332"];
    UIApplication* app = [ UIApplication sharedApplication ];
    if( [ app canOpenURL: facebookAppURL ] ) {
        [ app openURL: facebookAppURL ];
    } else {
       // url wrong
    }
}

Here it is very nice Article

http://www.plungeinteractive.com/blog/2012/12/31/open-facebook-and-twitter-native-apps-from-your-ios-appgame/

UPDATE:-

I dont think this is suitable for your requirement but using Custom URL Scheme. you can able to Open your app from from safari browser or other app. just setting URL type--> setting URL schemes like bellow enter image description here

and then try like i did in bellow image that open your app from browser:-

enter image description here

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • Thanks by the way .. but my problem is I'm trying to start facebook when my app state is fore-ground (*not in background)... in background it worked fine .. but in fore-ground it didn't work – user3152918 Mar 28 '14 at 07:50
  • can't getting your what are you trying and what is your task – Nitin Gohel Mar 28 '14 at 07:54
  • i mean background sorry for my confusion .. my app is on background and I want to start fb from my app. how can I do that – user3152918 Mar 28 '14 at 07:56
0

You cannot launch another app from the background mode because that would be pre-empting the user's current task without user interaction. There is only one application that is permitted to pre-empt the user's current action and that is the phone in the case of an incoming call.

What you could do is display a local notification and if the user actions that notification to launch your app - placing it back into foreground - then you could launch Facebook.

UILocalNotification* localNotification = [[UILocalNotificationalloc] init]; 
localNotification.fireDate = nil;
localNotification.alertBody = @"I want to launch Facebook";
localNotification.timeZone = nil;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

then in your app delegate -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {
        // Override point for customization after application launch.

        // Handle launching from a notification
        UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (locationNotification) {
            // Open Facebook
            [application openURL:[NSURL URLWithString:@"fb://"];
        }

        return YES;
    }

But if the user chooses not to action your notification there is nothing you can do.

Paulw11
  • 108,386
  • 14
  • 159
  • 186