Add MobileFirst web capabilities to an existing native app
We can add a MobileFirst web view into an existing native Android, iOS, or Windows Phone 8 application. For example, we can transform the application from pure native to hybrid. With such a change, we can add MobileFirst web capabilities to an existing native application.
To add MobileFirst web capabilities to an existing native app:
- Export the corresponding resources from an existing MobileFirst project.
- Integrate those resources into the native app and update your code to show a MobileFirst web view.
- Use either the Eclipse plug-in or the command-line interface, export the corresponding resources from an existing MobileFirst hybrid project.
When first adding the web view to a native app, select the option to export the native libraries. Subsequent exports do not need to include these libraries and will result in much smaller archives.
- With MobileFirst Studio:
- Ensure that the latest resources are built.
- Right-click Run As: Build Environment.
- Right-click the environment directory. For example, right-click iphone, android, or windowsphone8.
- Select Export MobileFirst Hybrid Resources.
- Specify the location in which to create the .zip file archive. For example, hybrid_resources.zip.
- With MobileFirst Platform Command Line Interface:
- Change the directory to the environment directory. For example: cd apps/myhybridapp/android
- Make sure that the latest resources are built. For example: mfp build
- Run the mfp export command.
- Specify the name and location where to create the .zip archive. For example: hybrid_resources.zip
- Integrate the hybrid resources into the native application to show a MobileFirst web view. Here are the steps for Android, iOS, and Windows Phone 8 devices.
- For Android devices:
- For Android devices, extract the archive, for example hybrid_resources.zip file, to the native Android project.
- If we are using the Android Native API, some files might need to be replaced, such as the libraries (lib/*.jar) and the client properties (assets/wlclient.properties).
- When we are extracting the file, if the application contains a file with the same path name as provided by the MobileFirst hybrid archive, merge the contents of the two files such as res/ and values/strings.xml.
- Update the AndroidManifest.xml file for your native Android application with the contents of the AndroidManifest-WL.xml file, which is provided in the MobileFirst hybrid archive.
- If we are using Android Studio, take into account that the project directory might differ from the Eclipse ADT. Extract the MobileFirst hybrid archive content to the app/libs directory for libraries and the app/src/main/ directory for assets and resources.
- Update the code to initialize the MobileFirst Web Framework and show a Cordova web view. See the sample in Implementing a custom startup process in Android-based hybrid applications.
- For iOS devices:
- If the WorklightAPI and worklight.plist files are in the iOS native project, delete them.
- Extract the MobileFirst hybrid resources contents from the archive, for example the hybrid_resources.zip file, into the directory containing the iOS native project where the .xcodeproj file is located.
- Add the Cordova subproject to the native application:
- Right-click Project Navigator - Add Files to Xcode_project_name.
- Select CordovaLib/CordovaLib.xcodeproj.
- Select Copy items into destination group's folder (if needed).
- Select Create groups for any added folders.
- Add the MobileFirst files to the native application:
- Right-click Project Navigator - Add Files to Xcode_project_name.
- Select the following files and directories:
buildtime.sh config.xml FipsHttp Frameworks/ Resources/ Tealeaf/ worklight.plist WorklightSDK
- Select Copy items into destination group's folder (if needed).
- Select Create groups for any added folders.
- Add the web assets to the native application:
- Right-click Project Navigator - Add Files to Xcode_project_name.
- Select www/.
- Select Copy items into destination group's folder (if needed).
- Select Create groups for any added folders.
- Add the Cordova library as a build dependency:
- Select Target.
- Select Build Phases.
- Under Target Dependencies, add CordovaLib.
- Add the Cordova library dependency, which is libCordova.a.
- Add Run Script Before Copy Bundle Resources.
- Add Run Script as last Phase.
- Select Editor > Add Build Phase > Add Run Script Build Phase.
- Open a shell. At the command prompt, type:
/bin/sh touch -cm ${PROJECT_DIR}/www
- Add Run Script After Copy Bundle Resources.
- Select Editor > Add Build Phase > Add Run Script Build Phase.
- Open a shell. At the command prompt, type:
/bin/sh script_file="buildtime.sh" echo "Running a custom build phase script: $script_file" unsecure_project_path=${PROJECT_DIR} secure_project_path="${unsecure_project_path// /\ }" eval ${secure_project_path}/${script_file} scriptExitStatus=$? echo "DONE with script: ${script_file} (exitStatus=${scriptExitStatus})\n\n" exit "${scriptExitStatus}"
- Add Linker flag to link the external dependencies:
- Select Target.
- Click Build Settings.
- Expand Linking and select All.
- Double-click the value for Other Linker Flags.
Use the search box to filter the list.
- Copy and paste the following code:
-force_load "$(BUILT_PRODUCTS_DIR)/libCordova.a" -force_load "$(SRCROOT)/WorklightSDK/libWorklightStaticLibProject.a" -ObjC
- Check the armv7 architecture.
- Select Project.
- Click Build Settings.
- Expand the Architectures section.
- Click a value for Architectures.
- Select Other.
- Remove Standard architectures (including 64-bit) or $ARCHS_STANDARD_INCLUDING_64_BIT.
- Add armv7.
- Verify that the following libraries and frameworks are referenced:
AddressBook.framework AddressBookUI.framework AssetsLibrary.framework AudioToolbox.framework AVFoundation.framework CFNetwork.framework CoreData.framework CoreGraphics.framework CoreLocation.framework CoreMedia.framework CoreMotion.framework CoreTelephony.framework Foundation.framework ImageIO.framework MediaPlayer.framework MobileCoreServices.framework Security.framework SystemConfiguration.framework QuartzCore.framework UIKit.framework libz.dylib libstdc++.6.dylib
- Update the code to initialize the MobileFirst web framework and show a Cordova web view. See the sample in Implementing a custom startup process for iOS-based Hybrid applications.
- For Windows Phone 8 devices:
- Extract the hybrid resources, for example the hybrid_resources.zip file, into a directory, such as WP8HybridResources.
- Add the hybrid resources to the native application by completing the following steps:
- Open the native application in Visual Studio.
- Add the www directory to the native application.
- Add the config.xml file to the native application.
- Add the worklight.xml file to the native application properties.
- Add the wlclient.properties file to the native application.
- Copy the WP8Hybridresources/buildtarget directory to the native application root directory.
- Add the WLCordovaClassLib.dll file to the native application.
- Add the NewtonSoft.Json.dll file to the native application.
- Open the native_application.csproj file, which is the Visual Studio project file for the native application.
<ItemGroup> <Reference Include="WLWPNativeLib.dll"> <HintPath>buildtarget\$(Platform)\WLWPNativeLib.dll</HintPath> </Reference> <Reference Include="WLCordovaClassLib.dll"> <HintPath>.\WLCordovaClassLib.dll</HintPath> </Reference> <Reference Include="NewtonSoft.Json.dll"> <HintPath>.\NewtonSoft.Json.dll</HintPath> </Reference> </ItemGroup>This step adds the DLL references to the project.
- Update the capabilities from the hybrid application:
- From the native application in Visual Studio, open Native App > Properties > WMAppManifest.xml.
- Add the missing capabilities, which are part of the WP8HybridResources/Properties/WMAppManifest.xml file.
- Create a page and copy the contents from the hybrid application:
- Add a page from native project in Visual Studio.
- Right-click the native project and select Add > New Item > Windows phone portrait page.
- Provide a name for the page. For the sake of this procedure, consider that the page name is: HybridPage.xaml
- Open the new HybridPage.xaml page, and remove the exiting Grid XML content.
- Open the WP8HybridResources/MainPage.xaml page in a text editor.
- Copy the Grid XML section and paste it into the new HybridPage.xaml page. The following code shows the new grid XML section:
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="*"/> </Grid.RowDefinitions> <my:CordovaViewHorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="CordovaView" VerticalAlignment="Stretch" StartPageUri="/www/skinLoader.html"/> </Grid>
- Copy the xmlns:mytag from the WP8HybridResources/Mainpage.xaml file to the HybridPage.xaml page for the phone tag. For example:
xmlns:my="clr-namespace:WPCordovaClassLib; assembly=WPCordovaClassLib"
- Open the App.xaml.cs file of the native application in Visual Studio.
- In the App constructor method, after the lineInitializePhoneApplicaton method, add the following code to initialize the native library:
new WPNativeLib.Initializer(); new HybridPluginInitializer.Initializer();
- Open the HybridPage.xaml.cs file of the native application in Visual Studio.
- After the constructor, add the following method:
voidCordovaView_Loaded(object sender, RoutedEventArgs e){ }
- Open the hybrid page from the native application.
- Add a button in the native application.
- For the Click method, write the following code to open the hybrid page:
NavigationService.Navigate(new Uri("/HybridPage.xaml", UriKind.Relative));
- Open the HybridPage.xaml.cs file inside the default construction and add the following entry:
WL.createInstance(); //create the instance of the ActionSender API
Select your target architecture, such as "x86" or "ARM", but not "Any CPU".
- If we use the JSONStore feature from the JavaScript API on Windows Phone 8, follow the next steps:
- Open the native_application.csproj file, which is the Visual Studio project file for the native application.
- Copy the following XML code into this project file:
<ItemGroup> <Content Include="buildtarget\$(Platform)\sqlite3.dll"> <Link>sqlite3.dll</Link> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> <Reference Include="JSONStoreWP8Lib"> <HintPath>buildtarget\$(Platform)\JSONStoreWP8Lib.dll</HintPath> </Reference> </ItemGroup>
- In Visual Studio, open the App.xaml.cs file of the native application.
- In the App constructor method, after the new WPNativeLib.Initializer() line, add the following code to initialize the JSONStore library:
new JSONStoreWP8Lib.Initializer();
Parent topic: Develop native applications