【发布时间】:2016-05-09 15:42:15
【问题描述】:
我已经构建了一个 Cordova 应用程序,我只是在 xcode 中添加了一些本机功能。我想从我的应用程序中截取 url,如下所示:
How to invoke Objective C method from Javascript and send back data to Javascript in iOS?
所以在我的 HTML 中我包含了一个链接
index.html
<a class="MyButton" id="id" href="req://ResultA">Item A</a>
然后我只有一个从 UIViewController 继承的非常基本的头文件
MyViewController.h
#import <UIKit/UIKit.h>
#import <Cordova/CDVViewController.h>
@interface MyViewController : UIViewController
@end
然后在我的MyViewController.m 文件中,我要做的就是从我的链接中解释 url。我想实现以下目标。
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.
}
但对于 CDVViewController。除此之外,我一直在阅读,尝试将 ViewController 用作 CDVViewController 的委托对于此方法是不好的做法,因为它会与 API 调用混淆?
或者,我也可以尝试复制 Cordova 应用程序提供的 MainViewController 并创建一个继承 CDVViewController 的 ViewController...
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
但是我不确定如何适当地修改它以适当地利用 URL 命令。有人有什么想法吗?
【问题讨论】:
-
当您必须从 HTML/javascript 与本机代码进行通信时,您可以使用这种方法,但您将不得不处理 Cordova 创建的本机项目并在 WebView 委托方法中创建条件块(不好)。我通常更喜欢为这些场景创建一个原生插件,f or me 更干净、更容易。你可以谷歌搜索 iOS 插件示例并查看 here。
-
非常感谢您的回复!这非常有帮助!不过,对于创建自己的插件的结构,我仍然有些困惑。我有一个新的 Cordova 项目。你能告诉我我需要哪些组件以及它们应该去哪里吗?我不确定
using the plugin.xml file to inject this markup automatically之类的事情。我知道我需要 xcodeproject 插件文件夹中的 CDVPlugin 文件,我可以在 ios 平台中配置 config.xml 文件,我只需要知道东西在哪里。谢谢!
标签: javascript ios cordova delegates