美文网首页
Cordova 创建iOS fb分享插件

Cordova 创建iOS fb分享插件

作者: 后浪普拉斯 | 来源:发表于2019-06-25 15:15 被阅读0次

    这个其实很简单,主要分2步:
    1、创建cordova 插件,这个可以看我的Cordova自定义iOS插件 调用原生sdk
    2、在插件中接入fb分享sdk,这个可以看iOS facebook 分享接入总结

    我接下来只做一个demo,关于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" />这其实就是我们调用时的前缀。

    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,这就是我们调用的主要方法,这是分享图片调用的方法。
    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
    
    

    我们接下来看调用:

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

    cordova-ios-facebook-share demo

    相关文章

      网友评论

          本文标题:Cordova 创建iOS fb分享插件

          本文链接:https://www.haomeiwen.com/subject/qagtcctx.html