Cordovacordova/phonegap Cordova开发

使用Cordoval常碰到的问题

2017-04-10  本文已影响738人  XLsn0w

1:如何在Cordoval加载远程的URL网址

在Config.xml配置文件时增加下面两个,便可以打开URL的HTML页面

2:在Cordoval中加载同一个域的URL是在APP打开,跳转到其它却是用safari浏览器打开

同样是在Config.xml配置中把下面两个删除,这样它便会一直在APP里面进行跳转

3:如何加载不同的启动页URL地址

在配置Config.xml文件中有个content的节点,里面默认是有一个打开本地的地址index.html(比如:);这个就是跳转到本地包里面的html页面,也可以修改成(比如:);

上面这种只是修改默认的地址,可能不符合对于项目实际用法,项目中要加载Cordova都会有一个viewController的控制器继承于CDVViewController,它时就有一个属性startPage用于设置跳到webView加载的html页面;其中使用CDVViewController通常需要设置wwwFolderName的目录名称,和startPage首页的名称即可。默认wwwFolderName为www,startPage默认为index.html;这个也是模板直接生成时文件的名称;

self.viewController.startPage=@"http://www.cnblogs.com";

4:如何加载HTML页面存放在盒沙中

self.viewController =[[MainViewController alloc] init];

NSString*curFilePath=[NSString stringWithFormat:@"file://%@/www",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];

NSLog(@"路径为:%@",curFilePath);if([[NSFileManager defaultManager] fileExistsAtPath:curFilePath]) {

self.viewController.wwwFolderName=curFilePath;

}

self.viewController.startPage=@"index.html";

同样是在wwwFolderName上做文章,因为它是前缀文件夹的路径,这边要注意是关于路径要运用file://方式进行读取;

因为可以读取沙盒里面的HTML页面,这样我们就可以更加灵活运用,比如HTML通过服务端去下载到沙盒解压,这样就可以做到动态修改;

5:加载页面跟结束加载页面的监听,有两个通知可以监听,用来处理等待效果展现

- (void)viewDidLoad

{

[super viewDidLoad];//Do any additional setup after loading the view from its nib.NSNotificationCenter*center =[NSNotificationCenter defaultCenter];

[center addObserver:self

selector:@selector(onNotification:)

name:CDVPluginResetNotification//开始加载object:nil];

[center addObserver:self

selector:@selector(onNotificationed:)

name:CDVPageDidLoadNotification//加载完成object:nil];

}- (void)onNotification:(NSNotification *)text{

NSLog(@"-----开始等待------");

}- (void)onNotificationed:(NSNotification *)text{

NSLog(@"-----结束等待------");

}

6:刷新UIWebView,UIWebView直接更改url并reload是没有用的。必须声明一个NSURLRequest,并重新loadRequest。刷新时的url必须是符合Cordova规则的url。在Cordova源码中有一个appUrl的方法,通过这个方法转出的url才能被CDVViewController正常加载;

localWebVC.wwwFolderName =@"www";

localWebVC.startPage=@"local.html";

NSURL*url =[self.localWebVC performSelector:@selector(appUrl)];if(url)

{

NSURLRequest*request =[[NSURLRequest alloc] initWithURL:url];

[self.localWebVC.webView loadRequest:request];

}

7:使用pod管理Cordoval及其插件

pod'Cordova'

如果需要引入一些相关的插件,可以加入如下配置,下面的这些插件可以通过pod搜索到:

pod'CordovaPlugin-console'pod'cordova-plugin-camera'pod'cordova-plugin-contacts'pod'cordova-plugin-device'pod'cordova-plugin-device-orientation'pod'cordova-plugin-device-motion'pod'cordova-plugin-globalization'pod'cordova-plugin-geolocation'pod'cordova-plugin-file'pod'cordova-plugin-media-capture'pod'cordova-plugin-network-information'pod'cordova-plugin-splashscreen'pod'cordova-plugin-inappbrowser'pod'cordova-plugin-file-transfer'pod'cordova-plugin-statusbar'pod'cordova-plugin-vibration'

注意:如果没有用pod来管理Cordova,默认工程都会有一个CordovaLib.xcodeproj来把Cordova的类引入,所以建议Cordova用pod引入,就可以调用,而关于html、JS等静态模板还是在工程中;可以查看下面两个网址

iOS中Cordova开发初探 地址:http://blog.devzeng.com/blog/hello-cordova-ios.html

Cordova使用Pod实例 地址:https://github.com/phonegap/phonegap-webview-ios

三:插件内容

对于Cordova在插件上面还是比较多,也可以自定义插件的开发,对于插件下面已经有列出一些,其它插件可以上Cordova或者gitHub进行查找;

支付宝支付插件:

iOS/Android 地址:https://github.com/fami2u/cordova-plugin-alipay.git微信支付插件:

iOS/Android 地址:https://github.com/fami2u/cordova-plugin-weipay.gitping++支付插件:

iOS 地址:https://github.com/fami2u/cordova-ping-fami.git扫描二维码和条形码插件:

iOS/Android 地址:https://github.com/fami2u/cordova-barcodescanner-fami.git拍照插件:

iOS/Android 地址:https://github.com/fami2u/cordova-plugin-camera.git极光推送插件:

iOS/Android 地址:https://github.com/fami2u/jpush-phonegap-plugin.gitiOS 地址:https://github.com/fami2u/cordova-Jpush-fami.git第三方登录插件:

iOS 地址:https://github.com/fami2u/cordova-UMLogin-fami.gitJS 地址:https://github.com/fami2u/cordova-plugin-wechat.git第三方分享插件:

iOS 地址:https://github.com/fami2u/cordova-UMShare-fami.git跳转地图插件:

iOS 地址:https://github.com/fami2u/cordova-plugin-map.git视频播放插件:

iOS 地址:https://github.com/fami2u/cordova-player-fami.git

四:有可能出现的问题

1:在使用cordova6.0的过程中,编译好的APP运行在IOS7+系统上默认是与状态栏重叠的,而运行在IOS6及老版本中时是于状态栏分离的。

解决办法:把文件MainViewController.m中的方法viewWillAppear进行相关修改如下。作用是更改view的边界,使其下移20px,刚好是状态栏的高度。

- (void)viewWillAppear:(BOOL)animated

{if([[[UIDevice currentDevice]systemVersion ] floatValue]>=7)

{

CGRect viewBounds=[self.webView  bounds];

viewBounds.origin.y=20;

viewBounds.size.height=viewBounds.size.height-20;

self.webView.frame=viewBounds;

}

[super viewWillAppear:animated];

}

2:在html页面内调用系统相机以后再返回,整个页面底部会有白色的空白控件,用调试工具查看后空白区域的高度是20px.该如何解决?

解决办法:由于整个cordova项目相当于一个页面的应用,不同的模块聚集在一起,所以当当前屏幕消失后(比如进入系统相机拍照页面)再出现的时候,还是会执行上面的代码,所以界面高度再次减少20px.

-(void)viewWillDisappear:(BOOL)animated

{if([[[UIDevice currentDevice]systemVersion ] floatValue]>=7)

{

CGRect viewBounds=[self.webView  bounds];

viewBounds.origin.y=20;

viewBounds.size.height=viewBounds.size.height+20;

self.webView.frame=viewBounds;

}

[super viewWillDisappear:animated];

}

六:JS跟OC交互实例

            Capture Photo                //简单跟OC交互,没有回调//function test()//{//options={quality:"200"};//cordova.exec(null,null,'HelloWorld','sayHello',[options]);//}function test()

{

options={quality:"200"};

cordova.exec(

function(result){vars=result;

alert(s);

},

function(error)

{

alert("error",error);

}

,'HelloWorld','sayHello',[options]);

}                交互OC
   

可以查看文章对于插件的编写有进一步的说明,http://www.jianshu.com/p/e982b9a85ae8

七:分享Cordova不错的文章:

使用Cordova进行iOS开发 (环境配置及基本用法) :http://www.jianshu.com/p/d24219c008b6

使用Cordova进行iOS开发 (第三方插件的使用:Camera插件):http://www.jianshu.com/p/1e3d0c915dbc

使用Cordova进行iOS开发 (已存的项目中添加Cordova及自定义插件):http://www.jianshu.com/p/e982b9a85ae8

Cordova插件开发入门(IOS版OC跟JS交互):http://my.oschina.net/crazymus/blog/516388

浅析 Cordova for iOS(OC跟JS交互的说明):http://www.cocoachina.com/industry/20130520/6238.html

cordova CDVViewController解析:http://blog.csdn.net/u011417590/article/details/50895734

附整理的Cordova小实例:https://github.com/wujunyang/jiaCordovaDemo

上一篇下一篇

猜你喜欢

热点阅读