user3152918
user3152918

Reputation: 31

how can I open a new application using [[UIApplication sharedApplication] openURL:] from a foreground app

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

Upvotes: 0

Views: 2922

Answers (2)

Paulw11
Paulw11

Reputation: 114856

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.

Upvotes: 0

Nitin Gohel
Nitin Gohel

Reputation: 49730

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

Upvotes: 2

Related Questions