I am newbie in iOS Development. i want to add deep linking on my app so i prefer url schema for that, so now i want to open my app from another app then app is open and instantly crashed not any one method is called.
like as here i added alert on didFinishLaunchingWithOptions method
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Info" message:#"Finish Called" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil,nil];
[alert show];
[self.window makeKeyAndVisible];
return YES;
}
And also this method is not call when my app is open
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Info" message:[NSString stringWithFormat:#"%#",url] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil,nil];
[alert show];
return YES;
}
It just open Splash screen and crashed can any one help me for this?
Thank you in advance.
There are 2 situations
1. app is in suspended state
- when u click on url then applicationdidFinishLaunchingWithOptions will get called and u will get url in options dictionary and the openUrl method will get skipped
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
2. if app is in background then openUrl method only get called .
Related
I am using local notification for alarm system but I am facing some problem while handling local notification, When i clicked on alarm notification (When app is closed) its launches app but the problem is it should go to didFinishLaunchingWithOptions function but it's not going inside any of the function in appDelegate (I used breakpoints to check).
I'm using story board with navigation controller, I want to open a specific view controller on notification click when app is closed.
but when I'm launching that app normally it's going inside didFinishLaunchingWithOptions function.
Please suggest.
Any help would be appreciated.
main.m
#import "DEMOAppDelegate.h"
int main(int argc, char * argv[])
{
#autoreleasepool
{
return UIApplicationMain(argc, argv, nil, NSStringFromClass([DEMOAppDelegate class]));
}
}
DEMOAppDelegate.m
#import "DEMOAppDelegate.h"
#implementation DEMOAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
NSLog(#"Recieved Notification %#",localNotif);
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
#end
It's not possible, when your app is not running notification it will not react on notification directly.
didFinishLaunchingWithOptions will contain information about notification only when user opened your app through this notifcation. If he cancels it and opens your app through dashboard icon you are not gonna see this in this method.
Unfortunatelly if you need to react on all notification that happened from last time the user opened the app, only way is to build your own tracking logic and get all events that were in the past based on time.
Also there is no way to even get a list of notification you scheduled for your app, so usually it is a good idea to build in time-based event logic and use notification on top of it, but all the logic happens on your own time-based code. This way even if user disable notifications your critical logic will work.
When the application is running in background you will get notification through application:didReceiveRemoteNotification. You can gather those notifications inside your app, and process them on applicationDidBecomeActive if you want to do anything specific inside your application for them after user comes back to the app from background.
I am on some document sharing issue. My iPhone has two applications. One app "SharingApp" shares a file from its bundle to another app "ViewerApp" using Document Interaction Controller. By default, the shared file will be saved to the document directory of "ViewerApp" under folder named "Inbox". I can get the url in didFinishLaunchingWithOptions: of "ViewerApp" as
NSURL *url = (NSURL*)[launchOptions valueForKey: UIApplicationLaunchOptionsURLKey];
This scenario works perfectly if the "ViewerApp" is not been launched.
The problem I have is, incase, if the "ViewerApp" is in background state(or not killed) and if the file is shared from "SharingApp", applicationDidBecomeActive: is called in appDelegate of "ViewerApp". So, I couldn't able to get the url as the didFinishLaunchingWithOptions: method is not called(ViewerApp is already launched). "ViewerApp" just opens with the last shared url before entering to the background state.
How can I handle to get the url in applicationDidBecomeActive:? Please, share some ideas if you have come across this kind of issue.
Thanks for your ideas.
You should implement the following UIApplicationDelegate method
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
This should solve your problem since this method will be called when your viewer app is in background
I ran into this exact same issue. The problem is that didFinishLaunchingWithOptions is not called when an application is already open but brought to the foreground. I took my code out of didFinishLaunchingWithOptions and instead put it inside handleOpenURL (also on the app delegate page)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
if (url != nil && [url isFileURL])
{
MainViewController *frontViewController = [[MainViewController alloc] init];
[frontViewController handleOpenURL:url]; //function on my main view controller class to do the necessary action
return YES;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Cannot Handle Opening This File." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
return NO;
}
}
I want to make such function:
When the notification of my app is fired, like the following image:
I swipe the app icon in the bar to the right, and the app should run and show a certain view.
But I don't know how to do it.
In my application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions, I write:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *objectIDURL = [localNotif.userInfo objectForKey:#"objectIDURI"];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
EventViewController *eventViewController = [storyboard instantiateViewControllerWithIdentifier:#"EventViewController"];
[eventViewController setEvent:[Event getEventByObjectIDURL:objectIDURL]];
[(UINavigationController*)self.window.rootViewController pushViewController:eventViewController animated:NO];
}
return YES;
}
But after my swiping the icon to the right, my app does not run at all.
Can anyone help?
Plus, I'm using storyboard, I don't know is it relevant.
You never said if this was a local or remote notification, but the messaging works pretty much the same for both. You must keep in mind that the system notifies you differently if the app is running, or if its not running. That is, if you double click the home button and see the app's icon at the bottom, then its "running" (my terminology).
What you need to do is make sure you have all the relevant delegate methods implemented, and please, NSLog every one of them, so you can verify which are being messaged as you test. Copy and paste them from UIApplication.h, so that you don't have a typo (that is, a misspelling, as the system gives you no warnings on these!)
Implement all of the following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// if launched due to a notification, you will get a non-nil launchOptions
NSLog(#"didFinishLaunchingWithOptions: launchOptions=%#", launchOptions);
...
}
// if using remote notifications
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// Better get this for remote notifications
NSLog(#"didRegisterForRemoteNotificationsWithDeviceToken: token=%#", deviceToken);
...
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"YIKES! didFailToRegisterForRemoteNotificationsWithError");
...
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// if already running, notification came in
NSLog(#"didReceiveRemoteNotification: userInfo=%#", userInfo);
...
}
// if using local notifications
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// if already running, notification came in
NSLog(#"didReceiveLocalNotification: notification=%#", notification);
...
}
Thanks for reading. I solved the problem. The reason was the following:
In the info.plist file I altered the row "Localization native development region" to "Germany". After that the app no longer ran on the iphone but in the simulator. After resetting it to "en" it worked again on the iphone device.
Does anybody have an idea why it does not run on the iphone for this particular case?
I have a really really strange problem. I can run my iPhone app in the simulator but not on my iphone device. If I run it on my device it cashes on the following line
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); <-- crashes here
}
}
In my AppDelegate.m file I put a NSLog in each method:
#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#implementation AppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(#"didFinishLaunchingWithOptions");
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"applicationWillEnterForeground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"applicationDidBecomeActive");
if( ![[Logic si] network_is_available] ) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"AlertView_error_title", #"") message:NSLocalizedString(#"No_Internet_connection_available",#"") delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil] show];
}
if( ![CLLocationManager locationServicesEnabled] ) {
NSLog(#"not locationServicesEnabled");
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"AlertView_error_title", #"") message:NSLocalizedString(#"No_location_management_available",#"") delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil] show];
} else {
NSLog(#"locationServicesEnabled");
}
}
- (void)applicationWillTerminate:(UIApplication *)application
{
NSLog(#"applicationWillTerminate");
}
#end
Nothing is printed to the console if I run the app it immediatly crashes. Other apps I programmed work fine when started from within xcode on the iphone.
I know this could be everything, but do you have any ideas what might cause this problem?
You have nothing in didFinishLaunchingWithOptions:. This is where you need to create your window. Start a fresh new project, Master-Detail for example, and see how the window is created in this delegate.
Example
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
DashboardViewController * dashboard = [[DashboardViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:dashboard];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
I'm trying to implement SSO following the Tutorial:
https://developers.facebook.com/docs/mobile/ios/build/
At the end of Step 3, I'm supposed to Test my App running it on Simulator. The Run succeeded but all I see is a Blank View (screen). I'm not sure if I need to create the view called "authorization dialog" including the FB logo, buttons, etc or if it's automatically created by code I've implemented using Facebook SDK.
Also, I'm using a storyboard and wonder if that's the problem.
I think that it's because you have once signed in and now you haven't logged out so it is already authorized and hence you cannot see anything :)
Is your initial ViewController blankView ? Because the SSO only validates/autorize the session, you need to implement the UI in your ViewController.
If you implement it in Storyboard I suggest you to do most of the code in your controller file, since I was stucked there for quite long time. But make sure to put this code in the app delegate file.
#pragma mark Facebook
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [self.facebook handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication: (NSString *)sourceApplication annotation:(id)annotation {
return [self.facebook handleOpenURL:url];
}
Also in the delegate file you should instantiate the Facebook object before you use the in the above methods.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
viewController *controller = [[viewController alloc] init];
// Initialize Facebook
facebook = [[Facebook alloc] initWithAppId:#"Your app ID in string" andDelegate:controller];
// Override point for customization after application launch.
return YES;
}