I've built a Cordova application and I'm just adding some native functionality in xcode. I want to intercept the urls from my application as is demonstrated here:
How to invoke Objective C method from Javascript and send back data to Javascript in iOS?
So in my HTML I have included a link
index.html
<a class="MyButton" id="id" href="req://ResultA">Item A</a>
Then I just have a really basic header file which inherits from the UIViewController
MyViewController.h
#import <UIKit/UIKit.h>
#import <Cordova/CDVViewController.h>
@interface MyViewController : UIViewController
@end
And then in my MyViewController.m
file all I want to do is to interpret the url from my link. I want to achieve something like below.
MyViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CDVViewController* StorySelectCDVViewController = [CDVViewController new];
StorySelectCDVViewController.view.frame = self.view.frame;
[self.view addSubview:StorySelectCDVViewController.view];
}
...
-(bool)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[url scheme] isEqualToString:@"req"])
//Store ResultA as a variable for later use.
}
but for the CDVViewController. As well as this I've been reading that it's bad practice to try to use the ViewController as a delegate for the CDVViewController with regards to this method since it messes with the API calls?
Alternatively I could also try to copy the MainViewController provided by the Cordova application and create a ViewController which inherits CDVViewController...
MyViewController.h
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVCommandDelegateImpl.h>
#import <Cordova/CDVCommandQueue.h>
@interface MyViewController : CDVViewController
@end
@interface MyCommandDelegate : CDVCommandDelegateImpl
@end
@interface MyCommandQueue : CDVCommandQueue
@end
MyViewController.m
#import "MyViewController.h"
@implementation MyViewController
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Uncomment to override the CDVCommandDelegateImpl used
// _commandDelegate = [[MyCommandDelegate alloc] initWithViewController:self];
// Uncomment to override the CDVCommandQueue used
// _commandQueue = [[MyCommandQueue alloc] initWithViewController:self];
}
return self;
}
- (id)init
{
self = [super init];
if (self) {
// Uncomment to override the CDVCommandDelegateImpl used
// _commandDelegate = [[MyCommandDelegate alloc] initWithViewController:self];
// Uncomment to override the CDVCommandQueue used
// _commandQueue = [[MyCommandQueue alloc] initWithViewController:self];
}
return self;
}
@end
@implementation MyCommandDelegate
#pragma mark CDVCommandDelegate implementation
- (id)getCommandInstance:(NSString*)className
{
return [super getCommandInstance:className];
}
- (NSString*)pathForResource:(NSString*)resourcepath
{
return [super pathForResource:resourcepath];
}
@end
@implementation MyCommandQueue
- (BOOL)execute:(CDVInvokedUrlCommand*)command
{
return [super execute:command];
}
@end
However I'm not sure how to modify this appropriately to tap into the URL Command appropriately. Does anybody have any ideas?