在iOS 或者安卓平臺上實現社會化分享功能,通常就是經過第三方的SDK進行分享服務,如友盟分享,ShareSDK分享javascript
一種是把有夢分享的SDK集成到工程裏面,直接調用SDK裏面API的方法進行分享,按照友盟分享的開發文檔直接進行集成,注意appkey 還有分享到第三方,須要去相應的平臺裏面申請相應的appkey和appsecret,而後在配置文件裏面配置相應的回調地址和網頁,直接進行分享,這種方法看集成文檔就能完成,不在作解釋,在這裏主要講一下第二種方法。java
另一種方法就是在H5網頁分享,這個適用於大型的app,工程裏面須要集成第三方的H5頁面,把第三方的H5頁面直接嵌套在控制器裏面,遵循裏面的代理方法實現加載第三方合做單位及成員機構裏面的H5頁面。經過寫入一段js代碼段,調起應用裏面的方法及相應的操做。web
安卓注入的js代碼段ajax
<script type="text/javascript">
function fx(){
$.ajax({
url: "${ctx}/app/homepage/fx.do",
dataType:"json",
success: function(data){json
//這裏是調用應用裏面的方法--安卓
window.share.postMessage({title:'測試分享的標題',content:'測試分享的內容',url:'http://www.cnblog.com'});
}
});
}
</script>app
iOS 注入的代碼段post
<script type="text/javascript">
function fx(){
$.ajax({
url: "${ctx}/app/homepage/fx.do",
dataType:"json",
success: function(data){測試
//這裏是iOS 調用的方法名url
window.webkit.messageHandlers.share.postMessage({title:'測試分享的標題',content:'測試分享的內容',url:'http://www.cnblog.com'});spa
}
});
}
</script>
//注意標明顏色爲前臺與後臺約定好的方法名,必須保持一致,不然不能進行分享操做
這裏已友盟分享,iOS端爲例
//js調用OC代碼段實現分享功能
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
NSLog(@"%@",message);
NSLog(@"body:%@",message.body);
if ([message.name isEqualToString:@"share"]) {
NSLog(@"調用成功");
[self shareing:message.body];
}
}
#pragma mark ---打開分享面板
- (void)shareing:(NSDictionary *)tempDic{
if (![tempDic isKindOfClass:[NSDictionary class]]) {
return;
}
NSString *title = [tempDic objectForKey:@"title"];
NSString *content = [tempDic objectForKey:@"content"];
NSString *url = [tempDic objectForKey:@"url"];
//顯示分享面板
// UMSocialUIManager
[UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Bottom;
[UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_None;
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
//建立分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//建立網頁內容對象
UIImage* thumbURL = [UIImage imageNamed:@"11.png"];
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:content thumImage:thumbURL];
//設置網頁地址
shareObject.webpageUrl = url;
//分享消息對象設置分享內容對象
messageObject.shareObject = shareObject;
//調用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的數據
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
[self alertWithError:error];
}];
}];
}
- (void)alertWithError:(NSError *)error
{
NSString *result = nil;
if (!error) {
result = [NSString stringWithFormat:@"分享成功"];
}
else{
NSMutableString *str = [NSMutableString string];
if (error.userInfo) {
for (NSString *key in error.userInfo) {
[str appendFormat:@"%@ = %@\n", key, error.userInfo[key]];
}
}
if (error) {
result = [NSString stringWithFormat:@"Share fail with error code: %d\n%@",(int)error.code, str];
}
else{
result = [NSString stringWithFormat:@"分享失敗"];
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享"
message:result
delegate:nil
cancelButtonTitle:NSLocalizedString(@"肯定", @"肯定")
otherButtonTitles:nil];
[alert show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[userContentController removeScriptMessageHandlerForName:@"share"]; //關閉web頁時會釋放內存
}
//還須要在工程裏面配置一些回調,來展現分享成功或分享失敗操做 返回分享成功或分享失敗標題
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
}
return result;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
}
return result;
}
//在這裏就大功告成啦,時間匆忙,若有不對的地方歡迎指正
你的讚揚是對我最大的鼓勵,謝謝