Current tab from storyboard - iphone

If I create a Tab Bar Application project template with storybard and put this in my AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(#"Root: %#", self.window.rootViewController);
NSLog(#"Current ViewController: %#", /*self.window.rootViewController.selectedIndexOrSomething?*/ );
return YES;
}
It says: Root: <UITabBarController: 0x6b20360>
So I guess the TabBarController is already added as a subView of the window property as I have not touched the AppDelegate in any other way than NSLog.
How to log the current tab/ViewController?

You will want to do this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(#"Root: %#", self.window.rootViewController);
NSLog(#"Current ViewController: %#", ((UITabBarController *)self.window.rootViewController).selectedViewController );
return YES;
}

Related

Can't see a heatmap using Heatma.ps SDK

I successfully installed heatma.ps SDK (https://heatma.ps) in my iPhone application. It tracks all the touches and gestures, but I can't view a heatmap when I shake my device. Any idea why? There is no log, it just doesn't show up.
This is how I setup my heatmaps object.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//...
heatmaps = [[Heatmaps alloc] init];
heatmaps.showDebug = YES;
[heatmaps start];
//...
}
To see heatmaps you need to set showMenu property of heatmaps object to YES
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//...
heatmaps = [[Heatmaps alloc] init];
heatmaps.showDebug = YES;
heatmaps.showMenu = YES;
[heatmaps start];
//...
}
Watch the following screencast: "Viewing heatmaps"

Opening in app, getting UIApplicationLaunchOptionsURLKey from launchOptions

I'm trying to open a .pdf-file in my app. I adapted the Info.plist so a .pdf can be opened in my application.
I use the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
thePDFurl = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
return YES;
}
In an other class, where my appDelegate (containing that didFinishLaunchingWithOptions), I've got the line:
appDel = [[UIApplication sharedApplication]delegate];
[theLabel setText:[NSString stringWithFormat:#"%#", appDel.thePDFurl]];
Somehow, theLabel always shows (null). What am I missing?
I may be misunderstanding what you're trying to do. If so, ignore.
If you want the user to be able to "Open with..." a PDF using your app, you can implement
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
E.g.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(#"Open URL:\t%#\n"
"From source:\t%#\n"
"With annotation:%#",
url, sourceApplication, annotation);
NSString *filepath = [url path];
//...
return YES;
}
I'm pretty sure this works for both launching the app and calling it (i.e. if it's already in the background).
You may retain the pdfurl variable and also get the absolute string value from NSURL using the absoluteString method.
[theLabel setText:[NSString stringWithFormat:#"%#", [appDel.thePDFurl absoluteString]]]
I guess you were all right. It just so happened the view got loaded before the method applicationDidFinishLaunching was finished. Thanks all...
When you are calling within application:didFinishLaunchingWithOptions: ,
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[viewController view] was implicitly called and there was no appDel.thePDFurl assigned yet.

How to handle a custom URL in an already-open app?

I have an app based on the Utility template (i.e. Main and Flip view controllers). The Flip view allows selecting a certain item to be used on the main view. So far - works great.
Now I tried adding a custom URL. Something to the effect of: myapp://itemID=40 that will basically tell the main view: "no need to flip - you'll be dealing with item 40".
I registered the URL type scheme as "myapp" and added the following method to the app delegate:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if (!url) {
return NO;
}
NSString *urlString = [url absoluteString];
NSLog(#"URL received: %#", urlString);
NSString *itemID = [urlString stringByReplacingOccurrencesOfString:#"myapp://itemID=" withString:#""];
NSLog(#"Item received: %#", itemID);
[_mainViewController setStartupItem:itemID];
return YES;
}
As you can see, the itemID is set to a property called startupItem in the mainViewController.
I then added one line to the regular application method to verify that startupItem will be nil in case of no URL:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Make sure URL string is empty
[_mainViewController setStartupItem:nil];
// Override point for customization after application launch.
// Add the main view controller's view to the window and display.
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
And in MainViewController.m I added code to handle the item to the viewDidLoad event.
And here's my problem: this scheme works great if the app is started from a URL the first time. If it's already running, then we never reach viewDidLoad again and therefore don't handle that particular item, but go on as if none was passed.
My humble question is: which UIViewController should I put my handling code in? Or, am I approaching all this the wrong way? Should this be handled in my model?
As always, thanks in advance for your time!
Guy
I would take a look at the docs for UIApplicationDelegate protocol, specifically;
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
And this which is deprecated.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
Well definitely not in a method that gets called only once while the application starts up! You need to refactor the item handling code in its own method, then call this method from viewDidLoad (once during startup) and handleOpenURL each time it gets called.

when application become active then how we load the another view or invoke the root view controller

i have load the mailcomposer on reciving the localnotification
is it possible??
You should code as follows,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
//code for opening mail composer
}
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//code for opening mail composer
}

AppDelegate viewController memory leak?

I am just curious with regards to the correct way to create a view controller programatically. When I compile this code with the static analyser I get a leak (as you would expect) from the alloc. Should I just leave it as it needs to stay until the app exits anyways, or is there a cleaner way?
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"UIApplication application:");
RectViewController *myController = [[RectViewController alloc] init];
[window addSubview:[myController view]];
[window makeKeyAndVisible];
return YES;
}
cheers Gary
In this case, keep a reference to your view controller as an instance variable on the AppDelegate and release it in the AppDelegate's dealloc method.
#interface AppDelegate : NSObject {
// ...
RectViewController *myController;
}
// ...
#end
#implementation AppDelegate
// ...
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"UIApplication application:");
myController = [[RectViewController alloc] init];
[window addSubview:[myController view]];
[window makeKeyAndVisible];
return YES;
}
- (void) dealloc {
// ...
[myController release];
[super dealloc];
}
// ...
#end
Keep a reference to the view controller in you app delegate (instance, property, synthesize and release in dealloc).
And then instantiate it like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"UIApplication application:");
RectViewController *rootControllerTemp = [RectViewController new];
self.rootController = rootControllerTemp;
[rootControllerTemp release];
[window addSubview:[self.rootController view]];
[window makeKeyAndVisible];
return YES;
}