How to extend Google Analytics file with each view in ios? - ios

How to extend Google Analytics file with each view in ios ? I'm new to iOS and confused with the syntax.
I want to extend with this viewcontroller
#interface IXComposeViewController ()<UITextFieldDelegate,UIActionSheetDelegate,UITableViewDataSource,UITableViewDelegate,IXScrollViewDelegate>
So how should I combine these two extension?
#interface IXComposeViewController : GAITrackedViewController
Confused with syntax,please help.

You did it exactly right, for screen tracking in Google Analytics you only need to perform two simple steps:
the UIViewController that should be tracked needs to inherit from GAITrackedViewController, so your suggested syntax is correct: #interface IXComposeViewController : GAITrackedViewController
add the line self.screenName = #"screen: IXComposeViewController"; // or any other string in your viewWillAppear or viewDidAppear
To make it clear, in your header-file, you need to extend GAITrackedViewController, so it would look somewhat like this:
IXComposeViewController.h
#import "GAITrackedViewController.h"
#interface IXComposeViewController : GAITrackedViewController
// declare public properties and methods
#end
IXComposeViewController.m
#implementation IXComposeViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.screenName = #"About Screen";
}
#end

Related

iOS - adding a method to NSObject without importing the category .h file

i want to know if the following situation can be done. I have inherited a project of iOS 8. I'd like to add a method to NSObject so that all objects can see it. and I have done this already. Here is the category implementation i have created:
#import "NSObject+myCategory.h"
#implementation NSObject (myCategory)
-(void)localizeStringIncludeDefault{
NSLog(#"about to localize String");
}
#end
Now i go to a MyViewController.m for example and try to call the method but its not working its not seen:
heres the .h:
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController {
BOOL someFakeBoolean;
IBOutlet UIView *someView;
//etc
}
#property (nonatomic,assign) IBOutlet MyViewController *myViewController;
-(void)localizeStringIncludeDefault;
and here is the implementation *.m and my issue:
#implementation MyViewController
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self localizeStringIncludeDefault] //this call cant be done, the method is not visible
}
- (void) viewDidDisappear:(BOOL)animated
{
//etc
}
//etc
I mean it makes sense to me that i'd have to import the "NSObject+myCategory.h" into the MyViewController header to use the category but because i've inherited this code it already has a base. I dont want to go into every .h file and import this. Is a easy way to extend object so that EVERY object class sees my method ?
One option would be to add the category .h file to the pch file. Then it will be seen by every class in your project without the need to import it explicitly.
Declare your global variables or declarations in your pch file or rather make a Global.h and just import this in your pch file (helps a lot in reusability). You can declare extern items as well in your Global.h and populate in App Delegate

iOS - delegate not working

i have two view controllers, AbcViewController and XyzViewController. Both controllers behave similarly. Each has a "add" button which opens up a AddNewAbcViewController and AddNewXyzViewController respectively.
On AddNewAbcViewController, when the button "submit" is taped, it does it necessary stuff and close, bringing it back to AbcViewController. I am using delegate here where AbcViewController does the closing of AddNewAbcViewController. This works.
Now I want to do the same for XyzViewController and AddNewXyzViewController, but it is not working. When the btnSubmit is called in AddNewXyzViewController, it didn't enter into XyzViewController dimiss method. I have scanned through my codes many times but don't find anything extra not added. I even gave a different dismiss method name in XyzViewController and AddNewXyzViewController but that didn't work either. What did I miss?
here are my snippets for AbcViewController and AddAbcViewController. The codes for Xyz are identical:
class AddNewAbcViewController.h is
#import <Foundation/Foundation.h>
// protocol
#protocol AddNewAbcProtocol <NSObject>
-(void)dismiss;
#end
#interface AddNewAbcViewController : UIViewController<UITextViewDelegate>
#property(nonatomic, weak)id<AddNewAbcProtocol> delegate;
#end
class AddNewAbcViewController.m is
#interface AddNewAbcViewController() <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
...
#end
#implementation AddNewAbcViewController
...
- (IBAction)btnSubmit:(id)sender
{
[self.delegate dismiss];
}
#end
class AbcViewController.h is
#import <Foundation/Foundation.h>
#import "AddNewAbcViewController.h"
#interface AbcViewController : UIViewController<AddNewAbcProtocol, UISplitViewControllerDelegate>
...
#end
class AbcViewController.m is
#implementation AbcViewController
-(void)dismiss
{
NSLog(#"delegated to dismiss()");
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
As everyone explained, basically you forgot a line of code like ".delegate = self".
Here's a handy beginner's intro to delegates.
https://stackoverflow.com/a/4213005/294884
Use if statement to see if delegate works:
if ([self.delegate respondsToSelector:#selector(dismiss)])
{
[self.delegate dismiss];
}
Create AddNewXyzViewController as an instance variable, but not a local variable.

cannot find protocol declaration for ''

I have searched for related questions, but nothing has worked for me so I will describe my problem. I have two classes. Lets call them ClassA and RootViewController. RootViewController has a button that will trigger an action and delegate it to ClassA. The header for RootController looks like this:
#import <UIKit/UIKit.h>
#protocol RootViewControllerDelegate;
#interface RootViewController : UIViewController <UIPageViewControllerDataSource> {
}
...
#end
#protocol RootViewControllerDelegate <NSObject>
-(void)buttonPressed : (UIButton *) button;
#end
The ClassA header looks like this:
#import "RootViewController.h"
#interface RightPanelViewController : UIViewController <RootViewController>
...
#end
And I get the error :"Cannot find the protocol declaration for 'RootViewController'. Like I said, I have read some questions related to the same topic, the documentation for delegates, but I'm not able to see what the problem is. I would appreciate some help on this matter.
Change your interface line to
#interface RightPanelViewController : UIViewController <RootViewControllerDelegate>
ie. the name in angle brackets must match exactly with the name in your #protocol definition.

iOS change UIViewController

In my app I have a gradient as a background. This gradient is made programmatically. The way that I now use this is like this:
I have a UIViewController which needs to display the gradient and in that class I do this :
- (void) viewWillAppear:(BOOL)animated{
[super viewDidAppear:animated];
[Gradient gradientInViewController:self];
}
This ain't so bad but this needs to be done in all the classes which isn't very good programming. What I want is instead of making a class a UIViewController, I want it to be a GradientViewController which is a subclass of UIViewController and in this class I will handle everything.
So my question is how do I do this? I think this has to be done through categories? But I can't figure out how to get the image on the screen. Should this be done in viewWillAppear or something?
Make a GradientViewController which handles the gradient drawing
#interface GradientViewController : UIViewController
#end
#implementation GradientViewController
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[Gradient gradientInViewController:self];
}
#end
Then inherit all your other controllers from that
#interface YourViewController : GradientViewController
#end
#implementation YourViewController
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// no need to do anything
}
#end
It's more flexible to do it through helper classes or categories though, even if there's a bit of repetition.
Note as pointed out by Nguyen Duc, that you are calling [super viewDidAppear:] for viewWillAppear: which is wrong, I edited the answer.
Why not use Interface Builder and have a xib that knows how to load your image for you automatically?
You can get this behaviour "for free" throughout your app simply by subclassing UIViewController:-
#interface GradientViewController : UIViewController
#end
and implementing your gradient code in viewDidLoad: as such:-
- (void)viewDidLoad
{
[super viewDidLoad];
[Gradient gradientInViewController:self];
}
Then just use GradientViewController instead of UIViewController everywhere you need your gradient.
Alternatively you can use a category. This can be useful because you can use categories for "themes" for your app, configuring various UI elements of your UIViewControllers. But I'd set up a category on UIViewController specifically to set your gradient, and then call the category method in every view controller that needs it - do not attempt to do it by overriding viewDidLoad: or viewWillAppear: in a category.
Just create a new view controller (let's call it GradientViewController for example), put the same code that draws the gradient into its view viewWillAppear, then make all your view controllers a subclass of the view controller by replacing
#interface SomeViewController : UIViewController
with
#interface SomeViewController : GradientViewController

linking a static library in ios

I have created a math based application in Xcode 4.4. I am using tabbar based app with the help of storyboard.
I have written all my math functions in a separate class called CalculationMethods which is the subclass of NSObject.
My ViewController:
// FirstViewController.h
#import <UIKit/UIKit.h>
#import "CalculationMethods.h"
#interface FirstViewController : UIViewController
#end
// FirstViewController.m
#import "FirstViewController.h"
#import "CalculationMethods.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%f",[self julianDateFinder: [self currentDayMonthAndYearFinder]]);
}
#end
As you can see I have included my CalculationMethod.h file in both FirstViewController.h and the FirstViewController.m file, but when I use the methods of that class such as julianDateFinder and currentDayMonthAndYearFinder, Xcode errors, saying:
"No Visible #interface for 'FirstViewController' declares the selector 'CurrentDayMonthAndYearFinder'"
I am new to iOS and XCode. Can anyone help me solve the error?
In the FirstViewController, to use any of the methods in the CalculationMethods class, you need to create an instance of CalculationMethods. And then access a method using this syntax: [instanceOfCalculationMethods aMethodInCalculationMethods];
For example in your case, try this:
In the FirstViewController.h file, before the #end:
CalculationMethods *_calculationMethods;
And in the viewDidLoad method:
_calculationMethods = [CalculationMethods alloc] init];
NSLog(#"%f",[_calculationMethods julianDateFinder: [_calculationMethods currentDayMonthAndYearFinder]]);
I think you're misunderstanding how Objective C works slightly.
Without adding details of the CalculationMethods.h header file I can't help you much, but that compiler warning is telling you that FirstViewController doesn't have the method currentDayMonthAndYearFinder.
The reason that this is the case is because you're calling performing the selector CurrentDayMonthAndYearFinder on self which in the context of your FirstViewController instance is in fact the instance of FirstViewController
You said it yourself that your method CurrentDayMonthAndYearFinder is on your CalculatorMethods class so I suggest you either create an instance of your CalculatorMethods class or call the class method named CurrentDayMonthAndYearFinder on your CalculatorMethods class.
The problem here is weather or not you've defined instance methods or class methods.
Do yourself a favour and update your question with the contents of CalculationMethods.h

Resources