gpt4 book ai didi

javascript - iOS Cordova 应用程序 - shouldStartLoadWithRequest

转载 作者:行者123 更新时间:2023-11-29 12:00:52 27 4
gpt4 key购买 nike

我已经构建了一个 Cordova 应用程序,我只是在 xcode 中添加一些 native 功能。我想从我的应用程序中拦截 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 的委托(delegate)的不良做法,因为它会混淆 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 命令。有人有什么想法吗?

最佳答案

这是创建 iOS 插件所需的内容:

  • 插件.xml
  • src/ios/你的插件名称.h
  • src/ios/YourPluginName.m
  • www/YourPluginName.js

插件.xml

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="yourpluginid"
version="1.0.0">
<name>YourPluginName</name>
<description>Your Plugin Description</description>
<license>Apache 2.0</license>
<js-module src="www/YourPluginName.js" name="YourPluginName">
<clobbers target="YourPluginName" />
</js-module>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="YourPluginName">
<param name="ios-package" value="YourPluginName"/>
</feature>
</config-file>
<header-file src="src/ios/YourPluginName.h" />
<source-file src="src/ios/YourPluginName.m" />
</platform>
</plugin>

src/ios/你的插件名称.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>

@interface YourPluginName : CDVPlugin

- (void)pluginMethodName:(CDVInvokedUrlCommand*)command;

@end

src/ios/你的插件名称.m

#import "YourPluginName.h"

@implementation YourPluginName

- (void)pluginMethodName:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;
//Get param
NSString *param = [command.arguments objectAtIndex:0];
//Do something
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

www/你的插件名称.js

var exec = require('cordova/exec');

var YourPluginName = {
pluginMethodName:function(param, successCallback, errorCallback) {
exec(successCallback, errorCallback, "YourPluginName", "pluginMethodName", [param]);
}
};

module.exports = YourPluginName;

将每个文件放在一个文件夹中,然后从您的 cordova 项目的根文件夹中键入:

cordova plugin add pluginfolderpath

然后从您的 javascript(在 onDeviceReady 事件之后)您可以:

YourPluginName.pluginMethodName("param", function(){}, function(){});

关于javascript - iOS Cordova 应用程序 - shouldStartLoadWithRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37119944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com