Full ChatGPT conversation to create https://github.com/emptybox/SocialShareEBM
Creating a module for Titanium (Appcelerator) to share an image via text, email, Instagram, Facebook, or save to the local gallery requires several steps. Below is a detailed guide and sample code for creating an iOS module compatible with the latest version of Xcode and Swift.
Step-by-Step Guide:Set Up Your Development Environment:
- Ensure you have the latest version of Xcode installed.
- Install Titanium SDK and Appcelerator CLI.
- Create a new Titanium module project.
Create a New Module:
- Use the Titanium CLI to create a new module:
titanium create --type=module --name=ShareImage --platforms=iphone --id=com.example.shareimage
- Use the Titanium CLI to create a new module:
Open the Project in Xcode:
- Navigate to the module’s
ios
directory and open the.xcodeproj
file.
- Navigate to the module’s
Add Sharing Functionality Using Swift:
- Create a new Swift file in the Xcode project.
- Implement the sharing functionality using
UIActivityViewController
to handle different sharing options.
swiftimport UIKit
@objc(ShareImageModule)
class ShareImageModule: NSObject {
@objc(share:image:callback:)
func share(text: String, image: UIImage, callback: @escaping TiModuleCallback) {
DispatchQueue.main.async {
if let topController = UIApplication.shared.keyWindow?.rootViewController {
let activityViewController = UIActivityViewController(activityItems: [text, image], applicationActivities: nil)
activityViewController.excludedActivityTypes = [.assignToContact, .addToReadingList, .saveToCameraRoll]
activityViewController.completionWithItemsHandler = { (activityType, completed, returnedItems, error) in
if completed {
callback([["success": true]])
} else {
callback([["success": false]])
}
}
topController.present(activityViewController, animated: true, completion: nil)
} else {
callback([["success": false]])
}
}
}
}
Expose the Swift Code to Objective-C:
- Ensure the Swift code is accessible from Objective-C by adding the necessary bridging headers if needed.
- Update the
module.xcconfig
andmodule.xcodeproj
files to include Swift support.
Update Module Configuration:
- In
timodule.xml
, define the methods and properties exposed by your module.
xml<module> <api-version>3</api-version> <version>1.0.0</version> <description>Share Image Module</description> <platform>iphone</platform> <minsdk>9.0.0</minsdk> <apis> <api name="Ti.Module"> <method name="share" type="void"> <parameter name="text" type="String"/> <parameter name="image" type="Ti.Blob"/> <parameter name="callback" type="Function"/> </method> </api> </apis> </module>
- In
Compile the Module:
- Build the module using Titanium CLI:
appc run -p ios --build-only
- Build the module using Titanium CLI:
Distribute the Module:
- Once the module is built, package it for distribution.
- The compiled module can be found in the
dist
directory.
Integrate the Module in Your Titanium App:
- Copy the module to the
modules
directory of your Titanium app. - Reference the module in your
tiapp.xml
file:
xml<modules> <module platform="iphone">com.example.shareimage</module> </modules>
- Copy the module to the
Use the Module in Your Titanium App:
- Below is an example of how to use the module in your Titanium app.
var shareImage = require('com.example.shareimage');
var imageBlob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg').read();
shareImage.share('Check out this image!', imageBlob, function(e) {
if (e.success) {
Ti.UI.createAlertDialog({message: 'Shared successfully!'}).show();
} else {
Ti.UI.createAlertDialog({message: 'Failed to share.'}).show();
}
});
Summary:This guide walks you through the process of creating an iOS module for the Titanium ecosystem to share images via text, email, Instagram, Facebook, or save to the local gallery. It includes setting up your development environment, writing the Swift code, compiling the module, and integrating it into your Titanium app.
You should name the new Swift file something descriptive, such as ShareImageModule.swift
. This file should be placed within the Classes
directory of your Titanium module project. Here
is a more detailed breakdown:
- Navigate to the
Classes
directory of your Titanium module project. If it does not exist, create it. - Create a new Swift file named
ShareImageModule.swift
in theClasses
directory.
vbnetShareImage
├── assets
├── example
├── ios
│ ├── Classes
│ │ ├── ShareImageModule.swift
│ ├── Resources
│ ├── ShareImage.xcodeproj
│ ├── ShareImage_Prefix.pch
│ ├── classes
│ ├── module.xcconfig
├── lib
├── timodule.xml
└── LICENSE
Full Swift Code for ShareImageModule.swift
:swiftimport UIKit
@objc(ShareImageModule)
class ShareImageModule: NSObject {
@objc(share:image:callback:)
func share(text: String, image: TiBlob, callback: @escaping TiModuleCallback) {
DispatchQueue.main.async {
if let topController = UIApplication.shared.keyWindow?.rootViewController {
let activityViewController = UIActivityViewController(activityItems: [text, image.image()], applicationActivities: nil)
activityViewController.excludedActivityTypes = [.assignToContact, .addToReadingList]
activityViewController.completionWithItemsHandler = { (activityType, completed, returnedItems, error) in
if completed {
callback([["success": true]])
} else {
callback([["success": false]])
}
}
topController.present(activityViewController, animated: true, completion: nil)
} else {
callback([["success": false]])
}
}
}
}
Bridging Header:If the project does not already include a bridging header file, you need to create one. You can name it ShareImage-Bridging-Header.h
and place it in the ios
directory.
ShareImage-Bridging-Header.h
):objc#import "TiModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
Updating module.xcconfig
:Ensure the module.xcconfig
file includes the path to the bridging header. Add the following line to module.xcconfig
:
cssSWIFT_OBJC_BRIDGING_HEADER = ios/ShareImage-Bridging-Header.h
Compile and Test:- Compile the module as mentioned previously:
appc run -p ios --build-only
- Integrate and test the module in your Titanium app.
By following these steps, you will have a working Titanium module that includes Swift code to share images via various platforms.
Certainly! Here is the complete Swift file for ShareImageModule.swift
:
swiftimport UIKit
@objc(ShareImageModule)
class ShareImageModule: NSObject {
@objc(share:image:callback:)
func share(text: String, image: TiBlob, callback: @escaping TiModuleCallback) {
DispatchQueue.main.async {
if let topController = UIApplication.shared.keyWindow?.rootViewController {
let activityViewController = UIActivityViewController(activityItems: [text, image.image()], applicationActivities: nil)
activityViewController.excludedActivityTypes = [.assignToContact, .addToReadingList]
activityViewController.completionWithItemsHandler = { (activityType, completed, returnedItems, error) in
if completed {
callback([["success": true]])
} else {
callback([["success": false]])
}
}
topController.present(activityViewController, animated: true, completion: nil)
} else {
callback([["success": false]])
}
}
}
}
Instructions:Create the Swift File:
- Navigate to the
Classes
directory of your Titanium module project. - Create a new Swift file named
ShareImageModule.swift
.
- Navigate to the
Paste the Above Code:
- Copy and paste the complete code provided above into
ShareImageModule.swift
.
- Copy and paste the complete code provided above into
Create Bridging Header:
- If your project does not have a bridging header, create one named
ShareImage-Bridging-Header.h
in theios
directory. - Add the following to the bridging header:objc
#import "TiModule.h" #import "TiBase.h" #import "TiHost.h" #import "TiUtils.h"
- If your project does not have a bridging header, create one named
Update
module.xcconfig
:- Ensure the
module.xcconfig
includes the path to the bridging header by adding:cssSWIFT_OBJC_BRIDGING_HEADER = ios/ShareImage-Bridging-Header.h
- Ensure the
Build the Module:
- Use the Titanium CLI to build the module:
appc run -p ios --build-only
- Use the Titanium CLI to build the module:
Integrate and Test:
- Integrate the module into your Titanium app and test it using the example provided in the previous steps.
If there is no Classes
directory in your Titanium module project, you can create it manually. Here is a more detailed step-by-step guide to create the Classes
directory and place the
Swift file within it:
Create the Classes Directory:
- Navigate to the
ios
directory within your Titanium module project. - Create a new directory named
Classes
.
- Navigate to the
Create the Swift File:
- Inside the
Classes
directory, create a new Swift file namedShareImageModule.swift
.
- Inside the
Add the Complete Swift Code:
- Open
ShareImageModule.swift
and paste the following code:
- Open
swiftimport UIKit
@objc(ShareImageModule)
class ShareImageModule: NSObject {
@objc(share:image:callback:)
func share(text: String, image: TiBlob, callback: @escaping TiModuleCallback) {
DispatchQueue.main.async {
if let topController = UIApplication.shared.keyWindow?.rootViewController {
let activityViewController = UIActivityViewController(activityItems: [text, image.image()], applicationActivities: nil)
activityViewController.excludedActivityTypes = [.assignToContact, .addToReadingList]
activityViewController.completionWithItemsHandler = { (activityType, completed, returnedItems, error) in
if completed {
callback([["success": true]])
} else {
callback([["success": false]])
}
}
topController.present(activityViewController, animated: true, completion: nil)
} else {
callback([["success": false]])
}
}
}
}
- Create a Bridging Header:
- In the
ios
directory, create a file namedShareImage-Bridging-Header.h
. - Add the following content to the
ShareImage-Bridging-Header.h
file:
- In the
objc#import "TiModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
- Update
module.xcconfig
:- Open the
module.xcconfig
file located in theios
directory. - Add the following line to the
module.xcconfig
file to specify the bridging header:
- Open the
cssSWIFT_OBJC_BRIDGING_HEADER = ios/ShareImage-Bridging-Header.h
Compile the Module:
- Use the Titanium CLI to build the module:
appc run -p ios --build-only
- Use the Titanium CLI to build the module:
Integrate the Module in Your Titanium App:
- Copy the built module to the
modules
directory of your Titanium app. - Reference the module in your
tiapp.xml
file:
xml<modules> <module platform="iphone">com.example.shareimage</module> </modules>
- Copy the built module to the
Use the Module in Your Titanium App:
- Below is an example of how to use the module in your Titanium app.
var shareImage = require('com.example.shareimage'); var imageBlob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg').read(); shareImage.share('Check out this image!', imageBlob, function(e) { if (e.success) { Ti.UI.createAlertDialog({message: 'Shared successfully!'}).show(); } else { Ti.UI.createAlertDialog({message: 'Failed to share.'}).show(); } });
These steps guide you through creating a Titanium module with a Swift file for sharing images, setting up the necessary directories and files, compiling the module, and integrating it into your Titanium app.
If you prefer to use Objective-C instead of Swift, you can achieve the same functionality by creating an Objective-C class to handle the image sharing. Here is a detailed guide and the corresponding code for
creating the ShareImageModule
in Objective-C.
Create the Classes Directory:
- Navigate to the
ios
directory within your Titanium module project. - Create a new directory named
Classes
if it doesn’t already exist.
- Navigate to the
Create the Objective-C Files:
- Inside the
Classes
directory, create two files:ShareImageModule.h
andShareImageModule.m
.
- Inside the
ShareImageModule.h
:objc#import "TiModule.h"
#import <UIKit/UIKit.h>
@interface ShareImageModule : TiModule
- (void)share:(id)args;
@end
ShareImageModule.m
:objc#import "ShareImageModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
@implementation ShareImageModule
- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[[[TiApp app] controller] topPresentedController] topPresentedController];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
@end
- Update the
timodule.xml
File:- Define the methods and properties exposed by your module.
xml<module>
<api-version>3</api-version>
<version>1.0.0</version>
<description>Share Image Module</description>
<platform>iphone</platform>
<minsdk>9.0.0</minsdk>
<apis>
<api name="Ti.Module">
<method name="share" type="void">
<parameter name="text" type="String"/>
<parameter name="image" type="Ti.Blob"/>
<parameter name="callback" type="Function"/>
</method>
</api>
</apis>
</module>
Compile the Module:
- Build the module using the Titanium CLI:
appc run -p ios --build-only
- Build the module using the Titanium CLI:
Integrate the Module in Your Titanium App:
- Copy the built module to the
modules
directory of your Titanium app. - Reference the module in your
tiapp.xml
file:
xml<modules> <module platform="iphone">com.example.shareimage</module> </modules>
- Copy the built module to the
Use the Module in Your Titanium App:
- Below is an example of how to use the module in your Titanium app.
var shareImage = require('com.example.shareimage');
var imageBlob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg').read();
shareImage.share({
text: 'Check out this image!',
image: imageBlob,
callback: function(e) {
if (e.success) {
Ti.UI.createAlertDialog({message: 'Shared successfully!'}).show();
} else {
Ti.UI.createAlertDialog({message: 'Failed to share.'}).show();
}
}
});
SummaryThese steps guide you through creating a Titanium module with Objective-C for sharing images. You create the necessary Objective-C files, compile the module, and integrate it into your Titanium app.
The error you’re encountering typically occurs when there is a syntax issue or an incorrect data type declaration in your Objective-C method. Let’s ensure that the method declaration and its implementation are correct and conform to the expected Titanium module patterns.
Here’s a revised approach:
ShareImageModule.h
:objc#import "TiModule.h"
#import <UIKit/UIKit.h>
@interface ShareImageModule : TiModule
- (void)share:(id)args;
@end
ShareImageModule.m
:objc#import "ShareImageModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiBlob.h"
#import "TiApp.h"
@implementation ShareImageModule
- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[[[TiApp app] controller] topPresentedController] topPresentedController];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
@end
Explanation and Checklist:Header File (
ShareImageModule.h
):- Ensure it includes necessary imports.
- Declare the interface correctly.
Implementation File (
ShareImageModule.m
):- Ensure you import required headers.
- Make sure that
TiBlob
,TiUtils
, andKrollCallback
are correctly imported. ENSURE_SINGLE_ARG
is a Titanium macro to ensure that the argument is a dictionary.- The method implementation retrieves parameters from the dictionary, handles image conversion, and uses
UIActivityViewController
for sharing.
Create the
Classes
Directory (if not existing):- Navigate to the
ios
directory within your Titanium module project. - Create a new directory named
Classes
.
- Navigate to the
Add Files:
- Create
ShareImageModule.h
andShareImageModule.m
inside theClasses
directory. - Copy the provided code into these files.
- Create
Compile the Module:
- Use the Titanium CLI to build the module:
appc run -p ios --build-only
- Use the Titanium CLI to build the module:
Integrate and Test:
- Copy the compiled module to your Titanium app’s
modules
directory. - Reference the module in your
tiapp.xml
. - Use the module in your Titanium app as previously described.
- Copy the compiled module to your Titanium app’s
By following these steps, you should be able to resolve the syntax errors and properly implement the image sharing functionality in your Titanium module using Objective-C.
The ShareImageModule.m
file should be an Objective-C implementation file (.m
). It should be placed under the Classes
directory of your Titanium module project. The class in
this file should inherit from TiModule
, which is the base class for all Titanium modules.
Here’s a detailed breakdown of where and how to create this file:
Directory Structure:Your Titanium module’s ios
directory should look like this:
vbnetShareImage
├── assets
├── example
├── ios
│ ├── Classes
│ │ ├── ShareImageModule.h
│ │ ├── ShareImageModule.m
│ ├── Resources
│ ├── ShareImage.xcodeproj
│ ├── ShareImage_Prefix.pch
│ ├── module.xcconfig
├── lib
├── timodule.xml
└── LICENSE
Create ShareImageModule.h
:- In the
Classes
directory, create a file namedShareImageModule.h
. - Add the following code to
ShareImageModule.h
:
objc#import "TiModule.h"
#import <UIKit/UIKit.h>
@interface ShareImageModule : TiModule
- (void)share:(id)args;
@end
Create ShareImageModule.m
:- In the
Classes
directory, create a file namedShareImageModule.m
. - Add the following code to
ShareImageModule.m
:
objc#import "ShareImageModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiBlob.h"
#import "TiApp.h"
@implementation ShareImageModule
- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[[[TiApp app] controller] topPresentedController] topPresentedController];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
@end
Update timodule.xml
:Ensure your timodule.xml
is correctly configured to expose the share method. It should look like this:
xml<module>
<api-version>3</api-version>
<version>1.0.0</version>
<description>Share Image Module</description>
<platform>iphone</platform>
<minsdk>9.0.0</minsdk>
<apis>
<api name="Ti.Module">
<method name="share" type="void">
<parameter name="text" type="String"/>
<parameter name="image" type="Ti.Blob"/>
<parameter name="callback" type="Function"/>
</method>
</api>
</apis>
</module>
Build the Module:Use the Titanium CLI to build the module:
Integrate and Test:appc run -p ios --build-only
- Copy the built module to your Titanium app’s
modules
directory. - Reference the module in your
tiapp.xml
file:
xml<modules>
<module platform="iphone">com.example.shareimage</module>
</modules>
- Use the module in your Titanium app as described in the previous steps:
var shareImage = require('com.example.shareimage');
var imageBlob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg').read();
shareImage.share({
text: 'Check out this image!',
image: imageBlob,
callback: function(e) {
if (e.success) {
Ti.UI.createAlertDialog({message: 'Shared successfully!'}).show();
} else {
Ti.UI.createAlertDialog({message: 'Failed to share.'}).show();
}
}
});
By following these steps, you should have a working Objective-C implementation for sharing images in your Titanium module.
The error you’re encountering indicates that the definition of TiBlob
is not available to your code. To resolve this, ensure you have the correct import statements for the Titanium Kit framework.
Below are the adjusted steps and code snippets to fix the issue:
ShareImageModule.m
:- Import Titanium Kit:
Ensure you import the Titanium Kit headers properly to use
TiBlob
.
ShareImageModule.h
:objc#import "TiModule.h"
#import <UIKit/UIKit.h>
@interface ShareImageModule : TiModule
- (void)share:(id)args;
@end
ShareImageModule.m
:objc#import "ShareImageModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiBlob.h"
#import "TiApp.h"
@implementation ShareImageModule
- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[[[TiApp app] controller] topPresentedController] topPresentedController];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
@end
Directory Structure:Ensure the directory structure and file placements are correct.
vbnetShareImage
├── assets
├── example
├── ios
│ ├── Classes
│ │ ├── ShareImageModule.h
│ │ ├── ShareImageModule.m
│ ├── Resources
│ ├── ShareImage.xcodeproj
│ ├── ShareImage_Prefix.pch
│ ├── module.xcconfig
├── lib
├── timodule.xml
└── LICENSE
Build and Integrate:Build the Module:
appc run -p ios --build-only
Integrate the Module:
- Copy the compiled module to your Titanium app’s
modules
directory. - Reference the module in your
tiapp.xml
file:xml<modules> <module platform="iphone">com.example.shareimage</module> </modules>
- Copy the compiled module to your Titanium app’s
Use the Module:
var shareImage = require('com.example.shareimage'); var imageBlob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg').read(); shareImage.share({ text: 'Check out this image!', image: imageBlob, callback: function(e) { if (e.success) { Ti.UI.createAlertDialog({message: 'Shared successfully!'}).show(); } else { Ti.UI.createAlertDialog({message: 'Failed to share.'}).show(); } } });
By ensuring the proper imports and structure, the Objective-C implementation should work as expected for your Titanium module to share images.
The error you’re encountering is due to the incorrect method call for obtaining the top-most view controller. The method topPresentedController
doesn’t exist. Instead, you should use the presentedViewController
property to find the top-most view controller.
ShareImageModule.m
:Replace the following line:
objcUIViewController *rootViewController = [[[[TiApp app] controller] topPresentedController] topPresentedController];
With this code to properly get the top-most view controller:
objcUIViewController *rootViewController = [[TiApp app] controller];
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
Updated ShareImageModule.m
:Here is the modified part of your code:
objc- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[TiApp app] controller];
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
This change ensures that the rootViewController
is correctly identified as the top-most view controller before presenting the UIActivityViewController
.
Given the format of your timodule.xml
, you will need to update the module’s XML to define the methods and properties exposed by your Objective-C module. Below is how you can structure your timodule.xml
to include the share
method.
timodule.xml
:xml<?xml version="1.0" encoding="UTF-8"?>
<ti:module xmlns:ti="http://ti.tidev.io" xmlns:android="http://schemas.android.com/apk/res/android">
<api-version>3</api-version>
<version>1.0.0</version>
<description>Share Image Module</description>
<platform>iphone</platform>
<minsdk>9.0.0</minsdk>
<iphone>
<apis>
<api name="Ti.Module">
<method name="share" type="void">
<parameter name="text" type="String"/>
<parameter name="image" type="Ti.Blob"/>
<parameter name="callback" type="Function"/>
</method>
</api>
</apis>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Android specific configuration can go here -->
</android>
</ti:module>
Explanation:- The
iphone
section contains the API definitions for the iOS platform. - The
apis
tag defines the APIs exposed by your module. - The
api
tag specifies the class and the methods exposed by your module. - The
method
tag defines theshare
method with its parameters.
ShareImageModule.m
Code:The code remains largely the same, but make sure it matches the defined method in timodule.xml
.
ShareImageModule.m
:objc#import "ShareImageModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiBlob.h"
#import "TiApp.h"
@implementation ShareImageModule
- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[TiApp app] controller];
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
@end
Summary:- Update your
timodule.xml
to define theshare
method and its parameters. - Ensure your Objective-C implementation matches the defined API in the
timodule.xml
.
By following these steps, you should have a properly configured Titanium module for sharing images on iOS.
To build a Titanium module for iOS, you should use the appc ti build
command instead. Here are the corrected steps for building the module:
Navigate to the Module Directory: Open a terminal and navigate to the root directory of your module.
cd path/to/your/module
Build the Module: Use the following command to build the iOS module. Make sure you specify the correct target and platform.
appc ti build -p ios --build-only
Navigate to the Module Directory:
cd path/to/your/module
Build the Module:
appc ti build -p ios --build-only
Integrate the Module in Your Titanium App:
- Copy the compiled module from the
dist
directory to your Titanium app’smodules
directory.
- Copy the compiled module from the
Reference the Module in Your
tiapp.xml
:xml<modules> <module platform="iphone">com.example.shareimage</module> </modules>
Use the Module in Your Titanium App:
var shareImage = require('com.example.shareimage'); var imageBlob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg').read(); shareImage.share({ text: 'Check out this image!', image: imageBlob, callback: function(e) { if (e.success) { Ti.UI.createAlertDialog({message: 'Shared successfully!'}).show(); } else { Ti.UI.createAlertDialog({message: 'Failed to share.'}).show(); } } });
ShareImageModule.m
Code:Ensure the Objective-C implementation matches the defined method in timodule.xml
.
ShareImageModule.m
:objc#import "ShareImageModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiBlob.h"
#import "TiApp.h"
@implementation ShareImageModule
- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[TiApp app] controller];
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
@end
Correct timodule.xml
:xml<?xml version="1.0" encoding="UTF-8"?>
<ti:module xmlns:ti="http://ti.tidev.io" xmlns:android="http://schemas.android.com/apk/res/android">
<api-version>3</api-version>
<version>1.0.0</version>
<description>Share Image Module</description>
<platform>iphone</platform>
<minsdk>9.0.0</minsdk>
<iphone>
<apis>
<api name="Ti.Module">
<method name="share" type="void">
<parameter name="text" type="String"/>
<parameter name="image" type="Ti.Blob"/>
<parameter name="callback" type="Function"/>
</method>
</api>
</apis>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Android specific configuration can go here -->
</android>
</ti:module>
By following these steps, you should be able to successfully build and integrate your Titanium module for sharing images on iOS.
Certainly! To help troubleshoot your build failure, let’s follow a systematic approach:
Check the Build Log:
- Carefully review the build log for any error messages or warnings. This will provide specific clues about what went wrong.
Common Issues and Fixes:
Missing Headers: Ensure all necessary headers are included in
ShareImageModule.m
:objc#import "ShareImageModule.h" #import "TiBase.h" #import "TiHost.h" #import "TiUtils.h" #import "TiBlob.h" #import "TiApp.h"
Correct Import Path: Double-check the paths to the headers to make sure they are correct and the files exist.
Objective-C Syntax: Verify that there are no syntax errors in your Objective-C code.
Run
appc ti build
with Verbose Logging:- Run the build command with verbose logging to get more detailed output:
appc ti build -p ios --build-only --log-level trace
- Run the build command with verbose logging to get more detailed output:
Ensure Xcode and Titanium SDK are Up-to-Date:
- Make sure you are using the latest versions of Xcode and the Titanium SDK.
Clean the Build:
- Sometimes, cleaning the build can resolve issues:
appc ti clean
- Sometimes, cleaning the build can resolve issues:
Sample Code and Configuration:
- Here’s the complete setup for your module again, in case there are any discrepancies.
ShareImageModule.h
:objc#import "TiModule.h"
#import <UIKit/UIKit.h>
@interface ShareImageModule : TiModule
- (void)share:(id)args;
@end
ShareImageModule.m
:objc#import "ShareImageModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiBlob.h"
#import "TiApp.h"
@implementation ShareImageModule
- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[TiApp app] controller];
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
@end
timodule.xml
:xml<?xml version="1.0" encoding="UTF-8"?>
<ti:module xmlns:ti="http://ti.tidev.io" xmlns:android="http://schemas.android.com/apk/res/android">
<api-version>3</api-version>
<version>1.0.0</version>
<description>Share Image Module</description>
<platform>iphone</platform>
<minsdk>9.0.0</minsdk>
<iphone>
<apis>
<api name="Ti.Module">
<method name="share" type="void">
<parameter name="text" type="String"/>
<parameter name="image" type="Ti.Blob"/>
<parameter name="callback" type="Function"/>
</method>
</api>
</apis>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Android specific configuration can go here -->
</android>
</ti:module>
Additional Steps:Ensure you are in the correct directory: Make sure you are running the build command in the root directory of your module.
Xcode Project Settings:
- Open the generated Xcode project file in
ios/ShareImage.xcodeproj
. - Ensure that the correct deployment target is set.
- Check for any issues or warnings in Xcode.
- Open the generated Xcode project file in
If you still encounter issues, please provide the specific error messages from the build log so I can offer more precise assistance.
To convert an image into a TiBlob
in Titanium, you can use the Ti.Filesystem
and Ti.UI
APIs. Below are the steps and an example code snippet to convert an image into a
blob:
Load an Image from the Filesystem:
- Use
Ti.Filesystem
to load an image file and convert it into a blob.
- Use
Convert an Image View into a Blob:
- Use
Ti.UI.ImageView
to load an image and then use itstoBlob()
method to convert it into a blob.
- Use
var imageFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg');
var imageBlob = imageFile.read();
Example: Convert an Image View into a Blobvar imageView = Ti.UI.createImageView({
image: 'example.jpg',
width: 200,
height: 200
});
// Convert the image view to a blob
var imageBlob = imageView.toBlob();
Full Example: Using the Share Image ModuleHere’s how you can integrate this into your Titanium app, including loading an image, converting it to a blob, and using the share
method from your custom module.
var shareImage = require('com.example.shareimage');
// Load an image from the filesystem and convert it to a blob
var imageFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg');
var imageBlob = imageFile.read();
// Alternatively, you can use an ImageView to load and convert the image
// var imageView = Ti.UI.createImageView({ image: 'example.jpg', width: 200, height: 200 });
// var imageBlob = imageView.toBlob();
shareImage.share({
text: 'Check out this image!',
image: imageBlob,
callback: function(e) {
if (e.success) {
Ti.UI.createAlertDialog({ message: 'Shared successfully!' }).show();
} else {
Ti.UI.createAlertDialog({ message: 'Failed to share.' }).show();
}
}
});
Troubleshooting Build FailuresIf you encounter build failures when working with your custom module, make sure:
All Required Headers are Included:
- Ensure you have included all necessary headers in your Objective-C files.
Check for Correct Method Implementations:
- Ensure your methods in the module match the definitions in
timodule.xml
.
- Ensure your methods in the module match the definitions in
Verbose Build Logs:
- Use verbose logging to get detailed information about the build errors:
appc ti build -p ios --build-only --log-level trace
- Use verbose logging to get detailed information about the build errors:
Clean Build:
- Perform a clean build to ensure there are no stale artifacts:
appc ti clean appc ti build -p ios --build-only
- Perform a clean build to ensure there are no stale artifacts:
If you encounter specific errors during the build, please provide the error messages so I can offer more targeted assistance.
If you are creating an image out of multiple UI components, you can use the toImage()
method in Titanium to convert a view hierarchy into a blob. Here’s how you can do it:
Create a Container View:
- Create a parent view that will hold all the UI components.
Add Components to the Container View:
- Add your UI components (e.g., labels, images, buttons) to the container view.
Convert the Container View to an Image:
- Use the
toImage()
method to capture the container view as an image blob.
- Use the
Here is an example that demonstrates how to create an image from multiple UI components and then share it using your custom module:
var shareImage = require('com.example.shareimage');
// Step 1: Create a container view
var containerView = Ti.UI.createView({
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
backgroundColor: 'white'
});
// Step 2: Add components to the container view
var label = Ti.UI.createLabel({
text: 'Hello, World!',
color: 'black',
top: 10,
left: 10
});
containerView.add(label);
var imageView = Ti.UI.createImageView({
image: 'example.jpg',
width: 200,
height: 200,
top: 40,
left: 10
});
containerView.add(imageView);
var button = Ti.UI.createButton({
title: 'Click Me!',
top: 260,
left: 10
});
containerView.add(button);
// Step 3: Convert the container view to an image blob
var imageBlob = containerView.toImage();
// Step 4: Use the custom share module to share the image
shareImage.share({
text: 'Check out this composite image!',
image: imageBlob,
callback: function(e) {
if (e.success) {
Ti.UI.createAlertDialog({ message: 'Shared successfully!' }).show();
} else {
Ti.UI.createAlertDialog({ message: 'Failed to share.' }).show();
}
}
});
Explanation:- Container View: A parent view (
containerView
) is created to hold all the UI components. - Add Components: Various UI components (e.g., a label, an image view, and a button) are added to the container view.
- Convert to Image: The
toImage()
method is called on the container view to convert it to a blob. - Share Image: The
share
method of the custom module is called to share the created image blob.
- Customizing Components: You can add more UI components or customize them as needed. The
toImage()
method will capture the entire view hierarchy. - Handling Layout: Ensure that your layout is properly sized and positioned to fit within the container view.
- Asynchronous toImage(): If your view is complex or if you want to ensure that the view is fully rendered before capturing it, you can use the asynchronous version of
toImage()
:containerView.toImage(function(blob) { // Share the image blob shareImage.share({ text: 'Check out this composite image!', image: blob, callback: function(e) { if (e.success) { Ti.UI.createAlertDialog({ message: 'Shared successfully!' }).show(); } else { Ti.UI.createAlertDialog({ message: 'Failed to share.' }).show(); } } }); });
This approach allows you to create a composite image from multiple UI components and then share it using your custom Titanium module.
The module we have created leverages the UIActivityViewController
, which is part of the iOS SDK. This controller provides a standard interface for sharing content with various services, including
social media apps that are installed on the user’s device.
Here’s how it works:
UIActivityViewController: This controller automatically detects and lists the sharing options available on the device. These options can include email, text message, social media apps (like Facebook, Twitter, Instagram), and other apps that support the type of content you are sharing.
Excluded Activity Types: You can exclude certain sharing options if needed. For example, if you do not want to include certain types of activities (like saving to a reading list), you can specify those in the
excludedActivityTypes
property.
In the provided code, we are already using UIActivityViewController
:
objc- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
UIImage *image = [imageBlob image];
NSArray *activityItems = @[text, image];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[TiApp app] controller];
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
How to Use the Module:Include the Module in Your App: Ensure the module is correctly integrated into your Titanium app.
Prepare Content to Share: Use the module to share content, such as text and images, as demonstrated in the previous examples.
Here’s how you can use the module to share an image composed of multiple UI components:
var shareImage = require('com.example.shareimage');
// Create a container view with multiple UI components
var containerView = Ti.UI.createView({
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
backgroundColor: 'white'
});
var label = Ti.UI.createLabel({
text: 'Hello, World!',
color: 'black',
top: 10,
left: 10
});
containerView.add(label);
var imageView = Ti.UI.createImageView({
image: 'example.jpg',
width: 200,
height: 200,
top: 40,
left: 10
});
containerView.add(imageView);
var button = Ti.UI.createButton({
title: 'Click Me!',
top: 260,
left: 10
});
containerView.add(button);
// Convert the container view to an image blob
var imageBlob = containerView.toImage();
// Share the image using the custom module
shareImage.share({
text: 'Check out this composite image!',
image: imageBlob,
callback: function(e) {
if (e.success) {
Ti.UI.createAlertDialog({ message: 'Shared successfully!' }).show();
} else {
Ti.UI.createAlertDialog({ message: 'Failed to share.' }).show();
}
}
});
Supported Social Media Apps:When the UIActivityViewController
is presented, it will automatically display sharing options for any social media apps installed on the device that support sharing the type of content you are
providing (text, images, etc.). Common supported apps include:
- Email Clients (Mail app)
- Messaging Apps (iMessage, SMS)
If you want to customize the list of available sharing options or exclude certain services, you can modify the excludedActivityTypes
property:
objcactivityViewController.excludedActivityTypes = @[
UIActivityTypeAssignToContact,
UIActivityTypeAddToReadingList,
// Add any other types you want to exclude
];
By using the UIActivityViewController
, your module will automatically support a wide range of sharing options, including popular social media apps, without needing to write specific integrations for
each one.
To update the module version in your Titanium module, you need to change the version number in the timodule.xml
file. This file is located in the root directory of your module project.
Open
timodule.xml
:- Navigate to the root directory of your Titanium module project.
- Open the
timodule.xml
file in a text editor.
Locate the
<version>
Tag:- Find the
<version>
tag inside thetimodule.xml
file.
- Find the
Update the Version Number:
- Change the version number to the new version you want to set.
Here is how your timodule.xml
might look before and after updating the version number.
xml<?xml version="1.0" encoding="UTF-8"?>
<ti:module xmlns:ti="http://ti.tidev.io" xmlns:android="http://schemas.android.com/apk/res/android">
<api-version>3</api-version>
<version>1.0.0</version>
<description>Share Image Module</description>
<platform>iphone</platform>
<minsdk>9.0.0</minsdk>
<iphone>
<apis>
<api name="Ti.Module">
<method name="share" type="void">
<parameter name="text" type="String"/>
<parameter name="image" type="Ti.Blob"/>
<parameter name="callback" type="Function"/>
</method>
</api>
</apis>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Android specific configuration can go here -->
</android>
</ti:module>
After Update:xml<?xml version="1.0" encoding="UTF-8"?>
<ti:module xmlns:ti="http://ti.tidev.io" xmlns:android="http://schemas.android.com/apk/res/android">
<api-version>3</api-version>
<version>1.1.0</version>
<description>Share Image Module</description>
<platform>iphone</platform>
<minsdk>9.0.0</minsdk>
<iphone>
<apis>
<api name="Ti.Module">
<method name="share" type="void">
<parameter name="text" type="String"/>
<parameter name="image" type="Ti.Blob"/>
<parameter name="callback" type="Function"/>
</method>
</api>
</apis>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Android specific configuration can go here -->
</android>
</ti:module>
Summary:- Open the
timodule.xml
file in the root directory of your Titanium module project. - Locate the
<version>
tag. - Update the version number to the new version you want to set.
This change will update the version number of your module, which is important for version tracking and ensuring that your app uses the correct module version.
To make the module more universal and capable of sharing either an image or text (or both), you can adjust the share
method to accept either type of content and handle the logic accordingly. Here’s
how you can modify the code to support sharing text, an image, or both:
Modify the share
method in ShareImageModule.m
to handle both text and image content dynamically.
ShareImageModule.h
:No changes needed here; keep the interface declaration as is.
objc#import "TiModule.h"
#import <UIKit/UIKit.h>
@interface ShareImageModule : TiModule
- (void)share:(id)args;
@end
ShareImageModule.m
:Update the share
method to conditionally add text and image to the activity items.
objc#import "ShareImageModule.h"
#import "TiBase.h"
#import "TiHost.h"
#import "TiUtils.h"
#import "TiBlob.h"
#import "TiApp.h"
@implementation ShareImageModule
- (void)share:(id)args {
ENSURE_SINGLE_ARG(args, NSDictionary);
NSString *text = [TiUtils stringValue:@"text" properties:args def:nil];
TiBlob *imageBlob = [args objectForKey:@"image"];
KrollCallback *callback = [args objectForKey:@"callback"];
NSMutableArray *activityItems = [NSMutableArray array];
if (text != nil) {
[activityItems addObject:text];
}
if (imageBlob != nil) {
UIImage *image = [imageBlob image];
[activityItems addObject:image];
}
if ([activityItems count] == 0) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(NO), @"message": @"No content to share"};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[TiApp app] controller];
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
activityViewController.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (callback != nil) {
NSDictionary *event = @{@"success": @(completed)};
[self _fireEventToListener:@"callback" withObject:event listener:callback thisObject:nil];
}
};
[rootViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
@end
Update timodule.xml
:Ensure your timodule.xml
file reflects the updated method signature, allowing both text and image parameters to be optional.
xml<?xml version="1.0" encoding="UTF-8"?>
<ti:module xmlns:ti="http://ti.tidev.io" xmlns:android="http://schemas.android.com/apk/res/android">
<api-version>3</api-version>
<version>1.1.0</version>
<description>Universal Share Module</description>
<platform>iphone</platform>
<minsdk>9.0.0</minsdk>
<iphone>
<apis>
<api name="Ti.Module">
<method name="share" type="void">
<parameter name="text" type="String" optional="true"/>
<parameter name="image" type="Ti.Blob" optional="true"/>
<parameter name="callback" type="Function" optional="true"/>
</method>
</api>
</apis>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Android specific configuration can go here -->
</android>
</ti:module>
Using the Module in Your Titanium App:You can now use the module to share text, an image, or both. Here are a few examples:
Share Text Only:var shareImage = require('com.example.shareimage');
shareImage.share({
text: 'Check out this text!',
callback: function(e) {
if (e.success) {
Ti.UI.createAlertDialog({ message: 'Shared successfully!' }).show();
} else {
Ti.UI.createAlertDialog({ message: 'Failed to share.' }).show();
}
}
});
Share Image Only:var shareImage = require('com.example.shareimage');
var imageBlob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg').read();
shareImage.share({
image: imageBlob,
callback: function(e) {
if (e.success) {
Ti.UI.createAlertDialog({ message: 'Shared successfully!' }).show();
} else {
Ti.UI.createAlertDialog({ message: 'Failed to share.' }).show();
}
}
});
Share Both Text and Image:var shareImage = require('com.example.shareimage');
var imageBlob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'example.jpg').read();
shareImage.share({
text: 'Check out this image!',
image: imageBlob,
callback: function(e) {
if (e.success) {
Ti.UI.createAlertDialog({ message: 'Shared successfully!' }).show();
} else {
Ti.UI.createAlertDialog({ message: 'Failed to share.' }).show();
}
}
});
This approach makes your sharing module versatile, allowing it to handle various types of content dynamically.