I'm using Safari to browse a webpage. After I click a button on this page, my Ipad will launch my app. So I implement the method - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url in the AppDelegate.m.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
if (!url) { return NO; }
NSString *URLString = [url absoluteString];
self.paramArray = [URLString componentsSeparatedByString:#"#"];
NSLog(#"%d",[self.paramArray count]);
for (int i = 1; i < [self.paramArray count]; i++) {
NSLog([self.paramArray objectAtIndex:i]);
}
return YES;
}
The url I used on the webpage was some thing like myapp://#first_part#second_part#third_part. self.paramArray stores the substrings of url (myapp:// first_part second_part third_part).
Now I want to show these strings in the textfields in my ViewController. How can I pass this NSArray to the ViewController?
This is just a few line of code, required to accomplish it.
Put this line in appDelegate.h file
First of all you need to conforms to a protocol as follow
#interface AppDelegate : UIResponder <UIApplicationDelegate>
Now need to declare one property for it to use as follow
#property (strong, nonatomic) id<UIApplicationDelegate>delagete;
Now the last and final stage to use the property in view controller is as follow
In .h file of viewcontroller
First put reference to appdelegate like this #import "AppDelegate.h"
Then one iVar as AppDelegate *appDel; in #interface
Now in .m file
put appDel = [[UIApplication sharedApplication]delegate]; in view did load method and access the appdelegate's all property there like appDel.paramArray.
Happy Coding :)
Related
I'm messing around with Rdio's iOS SDK. I've set up everything correctly in "Getting Started" outlined here (http://www.rdio.com/developers/docs/libraries/ios/). The track key in the app delegate works and plays after I launch the app.
Now I'm trying to get a simple UIButton click to play a track, and I can't for the life of me get it to work.
I have this in ViewController.h
#import <UIKit/UIKit.h>
#import <Rdio/Rdio.h>
#interface ViewController : UIViewController <RdioDelegate, RDPlayerDelegate>
#property (readonly) Rdio *rdio;
#end
And in ViewController.m
- (IBAction)playButton:(UIButton *)sender
{
[self.rdio preparePlayerWithDelegate:nil];
NSArray *sources = [NSArray arrayWithObjects:#"t1", #"p1", #"a1", nil];
[self.rdio.player playSources:sources];
}
I really appreciate the help!
I resolved my issue. My issue was I wasn't calling the initializer initWithConsumerKey... that I had in my App Delegate. I had also failed to set it as a delegate properly.
So my App Delegate looks like this:
#import "AppDelegate.h"
static AppDelegate *launchedDelegate;
#interface AppDelegate ()
#end
#implementation AppDelegate
+ (Rdio *)rdioInstance
{
return launchedDelegate.rdio;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
launchedDelegate = self;
_rdio = [[Rdio alloc] initWithConsumerKey:#"removed" andSecret:#"removed" delegate:nil];
return YES;
}
and in ViewController.m:
- (IBAction)listenButton:(UIButton *)sender
{
_rdio = [AppDelegate rdioInstance];
[self.rdio preparePlayerWithDelegate:nil];
[self.rdio.player playSource:#"p12691138"];
}
Feeling silly that I didn't get that at first! Leaving it up here in case it helps anybody.
In my iOS newsstand app i need to access the default created viewContoller object in my appDelegate to start the background download when a notification arrives.
The problem is the viewController i want to access is not the root viewcontroller, if thats the case i can access it as follows,
MyViewController* mainController = (MyViewController*) self.window.rootViewController;
It would be great if anyone could guide me on this.
thanks.
have you tried as following methods,
In your AppDelegate.h.
#property (nonatomic, retain) HAViewController * haViewController;
In your AppDelegate.m
#synthesize haViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.haViewController = [[HAViewController alloc] init];
return YES;
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[self.haViewController startupdate];
}
Thanks!
I'm very new to Objective C so please bear with me. I have been searching through a lot threads but still couldn't find suitable answer for my case yet I believe this question has been asked over and over. I found a lot of tutorials how to use AppDelegate for sharing string but I can't figure out how to use it to share NSMUtableDictionary.
I want to share one NSMutableDictionary between two classes where I add data to the NSMutableDictionary in one class and read it it another. I'm using AppDelegate class to store the data.
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic,retain) NSMutableDictionary *myArray;
#end
AppDelegate.m
#import "TestResults.h"
#synthesize myArray;
TestResults.h
#interface TestResults : UIViewController {
singletonObj * sobj;
}
TestResults.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *myArrayFromAppDelegate = appDelegate.myArray;
[myArrayFromAppDelegate setValue:#"aaa" forKey:#"bbb"];
NSLog(#"%#", myArrayFromAppDelegate);
}
When i do NSLog it return an empty array. Where did I go wrong?
I think you forget to alloc and init in AppDelegate.
In
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
myArray = [[NSMutableDictionary alloc]init];
.
.
[self.window makeKeyAndVisible];
return YES;
}
And you can give your Dictionary name : myDict it is benificial for you.
And by calling this myArray from other ViewController gives you empty NSMutableDictionary.
Oh lordy. This is indeed a VERY common beginner question.
The real answer to the question "How do I share objects in the app delegate" is "dont". It clutters up your app delegate, and makes it do work it was not intended to do. It's like storing your food from your house in your car. It works, but it weighs down the car so it doesn't work as well at it's primary job.
You should design your apps with as little global state as possible.
If you do need to share global state data, don't put it int the app delegate. Instead, create a data container singleton and use that.
You should be able to do a search on [ios] singleton and find lots of examples of creating singletons. A data container singleton is just a singleton that has properties that are used to hold and share data.
Second point:
You have an NSMutableDictionary called myArray. That is a recipe for confusion. Don't use the name of another type (the wrong type) in naming your dictionary. DO NOT DO THS! EVER! If it's not an array, don't call it an array.
Third point:
As others have pointed out, you never alloc/init your dictionary. The alloc/init should take place in the object that owns the dictionary (in your case, AppDelegate, but move it to your data container singleton.) You can ether create it in the owning class's init method, or write a custom getter that "lazy loads" the dictionary:
- (NSDictionary *)myDict;
{
if (!_myDict)
{
myDict = [NSMutableDictionary new];
}
return myDict;
}
Here you may alloc this dict.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myArray = [NSMutableDictionary dictionary];
.....
}
Otherwise you can do in TestResults.m
NSMutableDictionary *myArrayFromAppDelegate = appDelegate.myArray;
if (!myArrayFromAppDelegate)
appDelegate.myArray = [NSMutableDictionary dictionary];
You can also do this work.
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.myDictionary=[[NSMutableDictionary alloc]init];////This could be another way
NSMutableDictionary *myDictionaryFromAppDelegate = appDelegate.myDictionary;///Don't name an dictionary by array!!!!!!!
[myDictionaryFromAppDelegate setValue:#"aaa" forKey:#"bbb"];
NSLog(#"%#", myDictionaryFromAppDelegate);
}
Thanks
So I've declared this in my appDelegate.h
#property(nonatomic,strong) NSMutableArray *featured;
I've synthesized it like so in my appDelegate.m
#synthesize featured;
When I log that out in the appDelegate with the object stored in there, I get the value it's supposed to have
In a viewController.h file I have declared this
#property(nonatomic,strong) NSMutableArray *featured;
In the viewController.m file I've synthesized it like this
#synthesize featured;
I then print out this line and get a null value
NSLog(#"HERE %#", featured);
That same line prints out the correct value in my appDelegate.m file. I'm completely lost. I've set it up in the way I've done it for a previous class exercise. Thanks in advance!
Edit:
I created the array in appDelegate.m file like so in a method I called loadFeatured
featured = [NSMutableArray array];
for (id dict in tempArray)
{
//NSLog(#"dict=%#",dict);
NSString *shopName = [dict objectForKey:#"shopName"];
NSString *drinkName = [dict objectForKey:#"drinkName"];
NSNumber *likes = [dict objectForKey:#"likes"];
NSNumber *dislikes = [dict objectForKey:#"dislikes"];
NSString *review = [dict objectForKey:#"review"];
Featured *feat = [[Featured alloc] initWithName:shopName drinkName:drinkName likes:likes dislikes:dislikes review:review];
NSLog(#"feat=%#\n\n",feat);
[featured addObject:feat];
}
NSLog(#"there is %d featured",[featured count]);
NSLog(#"HERE %#", featured);
Here is the way, how to access the data stored in the app delegate from your viewcontroller.
You need not synthesize the object in the viewcontroller. Just import your appdelegate file and copy the following code wherever necessary.
NSMutableArray * nArray =[ (AppDelegate*) [[UIApplication sharedApplication] delegate] featured];
The above code gives you the required array from the app delegate.Now you can make use of the nArray object to display the details in the console.
NSLog(#"%#",nArray.description);
It's hard to say how to do this without knowing the structure of your app. You could pass a pointer to the array to your view controller, if you have access to that view controller from the app delegate. The other way is to get a reference to the app delegate in your view controller, and then access its array. That can be done like this:
AppDelegate *appDel = [UIApplication shared application].delegate;
NSArray *myControllerArray = appDel.featured;
You'll need to import your app delegate into your controller's .m file to use this approach.
Since you already declared a property in appDelegate.h you can access it in the other viewController like this:
#import "appDelegate.h"
and you can access the value it by using something like this:
((AppDelegate *)[[UIApplication sharedApplication]delegate]).featured
If you need to access an NSArray or any other object in any class, via AppDelegate, just create a property to access your ViewController, like so, in your AppDelegate class:
#import "ViewController.h"
#property (nonatomic, strong) AppDelegate *appDelegate;
#property (nonatomic, strong) ViewController *viewController;
In your ViewController class:
#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
ViewController *viewControllerREFERENCE = [appDelegate viewController];
Then you'll have access to any value on your ViewController, via AppDelegate.
I hope that helps you.
I think that I am a bit confused about iOS #property getter and setters. I am trying to set an NSString iVar in my AppDelegate.h file from another class so that it can be used by all of the classes in the project?
For example, I am working on an iPhone project that stores an iVar NSString *currentUser in AppDelegate.h. I need to be able to set this through one method in a ViewController.m and then get it through another method in a second ViewController?
Maybe Getter and Setter is the wrong direction of attack all together? I understand that i don't want to alloc init the AppDelegate as the iVar will only exist in that object and I want it accessible to ALL objects in ALL classes?
Please someone set me straight.
All the best,
Darren
Here's the setup for the app delegate.
#interface AppDelegate
{
NSString *__currentUser;
}
#property (monatomic, copy) NSString* currentUser;
#end
#implementation AppDelegate
#synthesize currentUser = __currentUser;
- (void) dealloc
{
[__currentUser release];
[super dealloc];
}
#end
From one view controller, you could set a value for the current user, and from a subsequent view controller, get that value for some nefarious purpose.
#implementation LoginController
- (void) viewDidLoad
{
...
AppDelegate *bob = [[UIApplication sharedApplication] delegate];
[bob setCurrentUser: #"Jim Kirk"];
...
}
#end
In some other view controller that appears later, the value of the current user can be accessed.
#implementation ProfileViewController
- (void) viewDidLoad
{
...
AppDelegate *bob = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString * user = [bob currentUser];
// insert nefarious purpose for current user value here
...
}
#end