Strange UI behaviour when app is opened with a URL Scheme
-
30-06-2021 - |
Question
The easiest way for me to explain this is to upload a test app, so I've stripped out everything that is unecessary and uploaded a bare bones project to SendSpace.
http://www.sendspace.com/file/z86g0z
I'm writing an app that will receive custom URL's from Mobile safari like "plasma://hello-world". These are then processed by my app and it displays a notification on screen.
In the test app the main class is a singleton and there is a "test" button this demonstrates what should happen when you open it via a URL. You get the custom link shown in a label on the screen and a green notification label at the top which then fades away. During this process the Xcode console shows this .....
2012-09-05 21:53:29.719 PlasmaLinker[2866:f803] Link: plasma:Test+Link
2012-09-05 21:53:29.778 PlasmaLinker[2866:f803] Show Alert: Success - Link Received
2012-09-05 21:53:35.779 PlasmaLinker[2866:f803] Remove Alert
All works fine.
Now I launch it from Mobile Safari by entering "plasma://hello-world" I hit enter and Safari switches to my app, the console shows EXACTLY the same output (with what ever URL you launch it with), however I get nothing in the UI at all. The link isn't placed in the text box, the notification is never shown.
What am I doing wrong? After its been launched with the URL you can again click the test button and see the UI elements change as desired. Its driving me nuts but I don't know enough to be able to get it to work when launched by a URL. Can someone please shed some light on this?
Kind Regards
Plasma
Solution
Your singleton method that gets the PlasmaLinker_ViewController
is returning a different instance to the one that is created by the storyboard and added to the main window.
One example that works, (in your app delegate)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSString *link = [NSString stringWithFormat:@"%@",url];
vc = (PlasmaLinker_ViewController*) self.window.rootViewController;
[vc processLink:link];
return YES;
}
Another option is to override the view controller's -(id)initWithCoder:(NSCoder *)decoder
and set your shared instance to self
in that method.
+ (PlasmaLinker_ViewController *) sharedInstance
{
return _sharedInstance;
}
-(id)initWithCoder:(NSCoder *)decoder {
NSLog(@"In initWithCoder!");
if (!_sharedInstance) {
if (self = [super initWithCoder:decoder]) {
_sharedInstance = self;
}
}
return _sharedInstance;
}