Cordova 創(chuàng)建iOS fb分享插件

這個其實很簡單,主要分2步:
1、創(chuàng)建cordova 插件,這個可以看我的Cordova自定義iOS插件 調(diào)用原生sdk
2、在插件中接入fb分享sdk,這個可以看iOS facebook 分享接入總結(jié)

我接下來只做一個demo,關(guān)于fb 分享圖片的插件:
主要說一下建成后的文件:

plugin.xml

<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-fbshare" version="1.0.0"
  xmlns="http://apache.org/cordova/ns/plugins/1.0"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <name>crabcrab-plugin-fbshare</name>
  <js-module name="FBSharing" src="www/FBSharing.js">
    <clobbers target="cordova.plugins.FBSharing" />
  </js-module>
  <platform name="ios">
    <config-file parent="/*" target="config.xml">
      <feature name="FBSharing">
        <param name="ios-package" value="FBSharing" />
      </feature>
    </config-file>
    <header-file src="src/ios/FBSharing.h" />
    <source-file src="src/ios/FBSharing.m" />
    <framework src="src/ios/Bolts.framework" custom="true"/>
    <framework src="src/ios/FBSDKCoreKit.framework" custom="true"/>
    <framework src="src/ios/FBSDKShareKit.framework" custom="true"/>
  </platform>
</plugin>

我們看到這個<clobbers target="cordova.plugins.FBSharing" />這其實就是我們調(diào)用時的前綴。

FBSharing.js

var exec = require('cordova/exec');
var fbShare ={

coolMethod:(arg0, success, error) => {
    exec(success, error, 'FBSharing', 'coolMethod', [arg0]);
},

fbIOSSharing:(imageUrl) =>{
    exec(null,null, 'FBSharing', 'fbShareIOS',[imageUrl]);
},

}
module.exports = fbShare

我們可以看到fbIOSSharing,這就是我們調(diào)用的主要方法,這是分享圖片調(diào)用的方法。
FBSharing.h

#import <Cordova/CDV.h>

@interface FBSharing : CDVPlugin 


- (void)coolMethod:(CDVInvokedUrlCommand*)command;

//fb link share ios
-(void)fbShareIOS:(CDVInvokedUrlCommand*)command;

@end

FBSharing

/********* FBSharing.m Cordova Plugin Implementation *******/

#import "FBSharing.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>

@interface FBSharing ()<FBSDKSharingDelegate>
@end

@implementation FBSharing

- (void)coolMethod:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString* echo = [command.arguments objectAtIndex:0];

    if (echo != nil && [echo length] > 0) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

//contentUrl, Title, description, imageUrl
-(void)fbShareIOS:(CDVInvokedUrlCommand*)command{
    NSString *imageUrl = [[command arguments] objectAtIndex:0];
    UIImage *shareImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
    FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
    photo.image = shareImg;
    photo.userGenerated = YES;
    FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
    content.photos = @[photo];
    [FBSDKShareDialog showFromViewController:self.viewController withContent:content delegate:self];
}

#pragma mark FBSDKSharingDelegate
-(void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results{
    NSLog(@"success");
}

-(void)sharerDidCancel:(id<FBSDKSharing>)sharer{
    NSLog(@"cancel");
}

-(void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error{
    NSLog(@"faile");
}


@end

我們接下來看調(diào)用:

cordova.plugins.FBSharing.fbIOSSharing("https://b-ssl.duitang.com/uploads/item/201611/10/20161110190055_fwshe.jpeg");

cordova-ios-facebook-share demo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • cordova官網(wǎng):https://cordova.apache.org/ cordova插件: 1、獲取當前應用...
    魚與熊掌不能兼得閱讀 1,852評論 0 1
  • 1、獲取當前應用的版本號 cordova plugin add cordova-plugin-app-versio...
    XuTong閱讀 2,471評論 2 11
  • 前言 正如上一篇文章提到的,我們很多時候會創(chuàng)建很多個文件夾---例如說創(chuàng)建5月份每一天的文件夾(詳情請見:《快速創(chuàng)...
    汪汪家的寶貝閱讀 20,271評論 0 4
  • 手機顯示加入長投已經(jīng)89天了,眨眼間居然過了三個月。 今年六月份畢業(yè)的我,八月份換了家新公司,正正式式的開始了工作...
    空繚閱讀 463評論 3 0
  • 我絲毫沒有想到,這一次短暫的行程會讓我如此難忘,每每想起,仿若依然身臨其境,使我的心如海潮般涌起,久久未能平復……...
    柳二山閱讀 576評論 2 3

友情鏈接更多精彩內(nèi)容