I am using the cordova social sharing plugin. Here is my code.
public shareTo(): void {
var options = {
message: 'Check out this item',
subject: 'Item',
url: 'http://example.com',
chooserTitle: 'Share via...'
};
SocialSharing.shareWithOptions(options);
console.log(options);
}
I want to be able to change the url to the selected item url from the database. For example...
url: item.url
This is my html button.
<button ion-button (click)="shareTo()">
So when someone clicks to share, the url changes to the url of the selected item url. However, I am wondering if this is possible with shareWithOptions or not.
Short answer
Yes it's possible.
Longer answer:
I'll discuss 2 scenario's, with and without a list. The scenario's are taking into account that the item
must be defined and must be a string in the form of http://example.com/item/3
Scenario 1, on a detail page with a property item
This scenario requires a property called item
in your class. (so f.e. let item: { url: 'someUrl'}; constructor(){ ... }
)
If you're on a detail page with no list assosciated to items. (you basically provided the answer yourself to this one)
<button ion-button (click)="shareTo()">
And the ts
public shareTo(): void {
var options = {
message: 'Check out this item',
subject: 'Item',
url: item.url, //the property mentioned in the explanation
chooserTitle: 'Share via...'
};
SocialSharing.shareWithOptions(options);
console.log(options);
}
Scenario 2, on a list page
let's consider you're having a list of items, called itemList
. Now you want to be able to share any of these from the same page you're in now.
The html would be something like (notice the item object passed in the method)
<div *ngFor="let item of itemList" (click)="shareTo(item)">
{{item}}
</div>
Typescript could then be kept the same as above, just add in a parameter.
public shareTo(item: any): void {
var options = {
message: 'Check out this item',
subject: 'Item',
url: item.url, //the paramater
chooserTitle: 'Share via...'
};
SocialSharing.shareWithOptions(options);
console.log(options);
}
You're basically constructing an [Object]
with the properties message
, subject
, url
and chooserTitle
so there's no reason this shouldn't be able to work.