Reputation: 23
I am trying to call a function LoadWebpage
in ViewController.m from AppDelegate.m.
I have enabled my app with a URL Scheme such that "urlscheme://?querystring" links in Safari open my app. I am capturing the url scheme in AppDelegate (I can tell by logging that this is working) and would like to fill a global variable with the querystring and then call my LoadWebPage
method so that the view controller can use the global variable and open the requested website in a WebView control.
I am following this tutorial.
This is is AppDelegate.m:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
NSLog(@"URL scheme:%@", [url scheme]);
NSLog(@"URL query:%@", [url query]);
URLString = (NSString *)[url query]; //extern variable
[self.ViewController loadWebpage];
return YES;
}
And in ViewController.m:
- (void)LoadWebpage{
NSString *strURL=URLString; //more logic will be required to get the appropriate url from the query string.
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:urlRequest];
}
But the line [self.ViewController loadWebpage];
has an error "Property 'ViewController' not found on object of type 'AppDelegate'".
I'm guessing I need to synthesize a reference to the open viewcontroller, but I can't find info on how to do this. Is this the best way to handle this?
Edit: Based on Yan's comment below, I tried to add
ViewController* mainController = (ViewController*) self.window.rootViewController;
[mainController LoadWebpage];
Instead of [self.ViewController loadWebpage];
but it just ends debugging with lldb?
Upvotes: 2
Views: 4906
Reputation: 484
For the ones, who have want it in Swift!
First, create NotificationCenter post method (In Swift 2.0 - NSNotification Center), where you want to send data:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NOTIFICATION_NAME"), object: nil)
return true
}
In your ViewController class, where you want to receive the data, add the following in super.viewDidLoad():
NotificationCenter.default.addObserver(self,selector: #selector(self.YOUR_METHOD_NAME),
name: NSNotification.Name(rawValue: "NOTIFICATION_NAME"),
object: nil)
And the method you want to call:
func YOUR_METHOD_NAME(notification: NSNotification) {
// Your method calling, or what you want to do with received data
}
Upvotes: 3
Reputation: 4737
Rather than trying to have the AppDelegate trigger something off in your View Controller, I would suggest having the View Controller register to receive a notification when the app starts. You can then check the variable you're setting in AppDelegate and react accordingly.
For example:
YourViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
// This notification is sent the first time the app launches
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(applicationBecameActive:)
name: UIApplicationDidBecomeActiveNotification
object: nil];
// This notification is sent when the app resumes from the background
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(applicationEnteredForeground:)
name: UIApplicationWillEnterForegroundNotification
object: nil];
}
Upvotes: 6