15、iOS WebViews(基礎知識)

iOS WebViews

本篇指導你如何將Cordova的WebView模塊嵌入到一個iOS應用當中,以及相互之間如何進行通信請參照plugins。
首先你需要通過Cordova CLI創建一個空工程,然後用Xcode打開根目錄/platform/ios目錄下的Xcode工程。參考這個工程結構。我們的目的就是將CordovaLib添加到已有的iOS項目中去。

添加Cleaver到你的Xcode工程(CordovaLib Sub-Project)

  1. 退出Xcode。
  2. 打開一個命令行並進入一個cordova工程的iOS目錄。
  3. 拷貝config.xml文件到iOS項目目錄。
  4. 打開Xcode,將config.xml文件添加到Xcode工程的Navigate中去。
  5. 選擇Create groups然後點Finish
  6. 拷貝/platform/ios目錄下的CordovaLib到iOS工程目錄,並導入。
  7. 打開CordovaLib.xcodeproj的Build Settings標籤。
  8. Other Linker Flags裏添加-force_load,-Obj-C。
  9. 打主工程的Build Phases標籤。
  10. 打開Link Binaries with Libraries.
  11. 選擇+添加以下的frameworks
AssetsLibrary.framework
CoreLocation.framework
CoreGraphics.framework
MobileCoreServices.framework
  1. 展開Target Dependencies
  2. 選擇+並添加CordovaLib。
  3. 展開Link Binaries with Libraries
  4. 點選+添加libCordova.a文件。
  5. 選擇Xcode打開PreferencesLocations,然後設置Derived DataAdvanced選擇Unique
  6. 打開Build Settings
  7. Header Search Paths添加如下頭文件搜索路徑:
"$(TARGET_BUILD_DIR)/usr/local/lib/include""$(OBJROOT)/UninstalledProducts/include""$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include""$(BUILT_PRODUCTS_DIR)"

As of Cordova 2.1.0, CordovaLib has been upgraded to use
Automatic Reference Counting (ARC). You don’t need to upgrade
to ARC to use CordovaLib, but if you want to upgrade your
project to use ARC, you should use the Xcode migration wizard
from the __Edit → Refactor → Convert to Objective-C
ARC…__ menu, de-select libCordova.a, then run the wizard to
completion.

使用CDVViewController

  1. 引入如下頭文件:
#import <Cordova/CDVViewController.h>
  1. 實例化一個CDVViewController對象,並作爲一個class屬性:
  CDVViewController* viewController = [CDVViewController new];
  1. 可選步驟,設置wwwFolderName屬性,默認爲www:
  viewController.wwwFolderName = @"myfolder";
  1. 可選步驟,設置config.xml文件的`標籤指定一個本地文件:
<contentsrc="index.html" />
...or a remote site:
<contentsrc="http://apache.org" />
  1. 可選步驟,設置useSplashScreen:
 viewController.useSplashScreen = YES;
  1. 設置view,總是要最後再設置這個屬性:
   viewController.view.frame = CGRectMake(0, 0, 320, 480);
  1. 添加Cleaver:
[myView addSubview:viewController.view];

添加HTML, CSS and JavaScript資源

  1. 生成一個新的目錄www。
  2. 把HTML, CSS and JavaScript放入該目錄。
  3. 將www目錄導入到Xcode的ios工程中。
  4. 在CDVViewController中設置wwwFolderName和startPage或者使用默認設置
 /*
        if you created a folder called 'myfolder' and
        you want the file 'mypage.html' initto be
        the startPage
    */
    viewController.wwwFolderName = @"myfolder";
    viewController.startPage = @"mypage.html"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章