Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4fc0d63

Browse files
wanghenghengEase
authored and
Ease
committed
添加对WebView的分享
1 parent 6220d7f commit 4fc0d63

File tree

6 files changed

+74
-69
lines changed

6 files changed

+74
-69
lines changed

‎Coding_iOS/Controllers/WebViewController.m‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#import "NJKWebViewProgress.h"
1111
#import "NJKWebViewProgressView.h"
1212
#import "BaseViewController.h"
13+
#import "CodingShareView.h"
1314

1415
@interface WebViewController ()<UIWebViewDelegate>
15-
//@property (strong, nonatomic) UIWebView *myWebView;
1616
@property (strong, nonatomic) NJKWebViewProgress *progressProxy;
1717
@property (strong, nonatomic) NJKWebViewProgressView *progressView;
1818
@end
@@ -65,6 +65,7 @@ - (void)viewDidLoad{
6565
_progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
6666
_progressView.progressBarView.backgroundColor = [UIColor colorWithHexString:@"0x3abd79"];
6767

68+
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"moreBtn_Nav"] style:UIBarButtonItemStylePlain target:self action:@selector(shareItemClicked)] animated:YES];
6869
}
6970

7071
- (void)viewWillAppear:(BOOL)animated{
@@ -77,6 +78,13 @@ - (void)viewWillDisappear:(BOOL)animated{
7778
[_progressView removeFromSuperview];
7879
}
7980

81+
- (void)shareItemClicked{
82+
NSURL *url = self.webView.request.URL;
83+
if (url.absoluteString && ![url.absoluteString isEmpty]) {
84+
[CodingShareView showShareViewWithObj:self.webView];
85+
}
86+
}
87+
8088
#pragma mark UIWebViewDelegate 覆盖
8189

8290
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

‎Coding_iOS/Vendor/SVWebViewController/SVWebViewController.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#import "BaseViewController.h"
1010

1111
@interface SVWebViewController : BaseViewController
12+
@property (nonatomic, strong, readonly) UIWebView *webView;
13+
@property (nonatomic, strong, readonly) NSURLRequest *request;
1214

1315
- (instancetype)initWithAddress:(NSString*)urlString;
1416
- (instancetype)initWithURL:(NSURL*)URL;

‎Coding_iOS/Views/Cell/TweetCell.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ - (void)locationBtnClicked:(id)sender{
676676
}
677677
}
678678
- (void)shareBtnClicked:(id)sender{
679-
[CodingShareView showShareViewWithTweet:_tweet];
679+
[CodingShareView showShareViewWithObj:_tweet];
680680
}
681681
#pragma mark TTTAttributedLabelDelegate
682682
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components{

‎Coding_iOS/Views/Cell/TweetDetailCell.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ - (void)locationBtnClicked:(id)sender{
408408
}
409409

410410
- (void)shareBtnClicked:(id)sender{
411-
[CodingShareView showShareViewWithTweet:_tweet];
411+
[CodingShareView showShareViewWithObj:_tweet];
412412
}
413413

414414
#pragma mark Collection M

‎Coding_iOS/Views/CodingShareView.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "Tweet.h"
1111

1212
@interface CodingShareView : UIView
13-
+ (void)showShareViewWithTweet:(Tweet *)curTweet;
13+
+ (void)showShareViewWithObj:(NSObject *)curObj;
1414
@end
1515

1616
@interface CodingShareView_Item : UIView

‎Coding_iOS/Views/CodingShareView.m‎

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ @interface CodingShareView ()<UMSocialUIDelegate>
2525
@property (strong, nonatomic) UIButton *dismissBtn;
2626
@property (strong, nonatomic) UIScrollView *itemsScrollView;
2727

28-
@property (strong, nonatomic) NSObject *objToShare;
2928
@property (strong, nonatomic) NSArray *shareSnsValues;
29+
@property (weak, nonatomic) NSObject *objToShare;
3030
@end
3131

3232
@implementation CodingShareView
@@ -158,8 +158,8 @@ + (NSDictionary *)snsNameDict{
158158

159159
}
160160

161-
+ (void)showShareViewWithTweet:(Tweet *)curTweet{
162-
[[self sharedInstance] showShareViewWithTweet:curTweet];
161+
+ (void)showShareViewWithObj:(Tweet *)curObj{
162+
[[self sharedInstance] showShareViewWithObj:curObj];
163163
}
164164

165165
+(NSArray *)supportSnsValues{
@@ -198,8 +198,8 @@ +(BOOL)p_canOpen:(NSString*)url{
198198
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]];
199199
}
200200

201-
- (void)showShareViewWithTweet:(Tweet *)curTweet{
202-
self.objToShare = curTweet;
201+
- (void)showShareViewWithObj:(NSObject *)curObj{
202+
self.objToShare = curObj;
203203
[self p_show];
204204
}
205205

@@ -245,15 +245,6 @@ - (void)p_dismissWithCompletionBlock:(void (^)(void))completionBlock{
245245
}
246246
}];
247247
}
248-
- (void)p_checkTitle{
249-
NSString *title;
250-
if ([_objToShare isKindOfClass:[Tweet class]]) {
251-
title = @"冒泡分享到";
252-
}else{
253-
title = @"分享到";
254-
}
255-
_titleL.text = title;
256-
}
257248
- (void)p_checkShareSnsValues{
258249
self.shareSnsValues = [CodingShareView supportSnsValues];
259250
}
@@ -274,55 +265,11 @@ - (void)p_doShareToSnsName:(NSString *)snsName{
274265
}else if ([snsName isEqualToString:@"coding"]){
275266
PrivateMessage *curMsg = [PrivateMessage privateMessageWithObj:[self p_shareLinkStr] andFriend:nil];
276267
[self willTranspondMessage:curMsg];
277-
// }else if ([snsName isEqualToString:@"sina"]){
278-
// NSString *shareTitle, *shareText, *shareTail;
279-
// shareTitle = [NSString stringWithFormat:@"「%@」", [self p_shareTitle]];
280-
// shareText = [self p_shareText];
281-
// shareTail = [NSString stringWithFormat:@"%@(分享自@Coding)", [self p_shareLinkStr]];
282-
// NSInteger maxShareLength = 140;
283-
// NSInteger maxTextLength = maxShareLength - shareTitle.length - shareTail.length;
284-
// if (shareText.length > maxTextLength) {
285-
// shareText = [shareText stringByReplacingCharactersInRange:NSMakeRange(maxTextLength - 3, shareText.length - (maxTextLength - 3)) withString:@"..."];
286-
// }
287-
// NSString *shareContent = [NSString stringWithFormat:@"%@%@%@", shareTitle, shareText, shareTail];
288-
// [NSObject showStatusBarQueryStr:@"正在分享到新浪微博"];
289-
//
290-
// UMSocialUrlResource *urlResource = nil;
291-
// NSString *imageUrl = [self p_imageUrlSquare:NO];
292-
// if (imageUrl.length > 0) {
293-
// urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeImage url:imageUrl];
294-
// }
295-
// [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToSina] content:shareContent image:nil location:nil urlResource:urlResource presentedController:[BaseViewController presentingVC] completion:^(UMSocialResponseEntity *response) {
296-
// if (response.responseCode == UMSResponseCodeSuccess) {
297-
// [NSObject showStatusBarSuccessStr:@"分享成功"];
298-
// }else{
299-
// [NSObject showStatusBarErrorStr:@"分享失败"];
300-
// }
301-
// }];
302268
}else if ([snsName isEqualToString:@"evernote"]){
303-
ENNote *noteToSave = [ENNote new];
304-
noteToSave.title = [self p_shareTitle];
305-
NSString *htmlStr;
306-
if ([_objToShare respondsToSelector:NSSelectorFromString(@"htmlMedia")]) {
307-
HtmlMedia *htmlMedia = [_objToShare valueForKey:@"htmlMedia"];
308-
htmlStr = htmlMedia.contentOrigional;
309-
}else{
310-
htmlStr = [self p_shareText];
311-
}
312-
htmlStr = [htmlStr stringByAppendingFormat:@"<p><a href=\"%@\">冒泡原始链接</a></p>", [self p_shareLinkStr]];
313-
noteToSave.content = [ENNoteContent noteContentWithSanitizedHTML:htmlStr];
314-
315-
if (![[ENSession sharedSession] isAuthenticated]) {
316-
[[ENSession sharedSession] authenticateWithViewController:[BaseViewController presentingVC] preferRegistration:NO completion:^(NSError *authenticateError) {
317-
if (!authenticateError) {
318-
[self p_uploadENNote:noteToSave];
319-
}else if (authenticateError.code != ENErrorCodeCancelled){
320-
[NSObject showHudTipStr:@"授权失败"];
321-
}
322-
}];
323-
}else{
324-
[self p_uploadENNote:noteToSave];
325-
}
269+
__weak typeof(self) weakSelf = self;
270+
[self p_shareENNoteWithompletion:^(ENNote *note) {
271+
[weakSelf p_willUploadENNote:note];
272+
}];
326273
}else{
327274
[[UMSocialControllerService defaultControllerService] setSocialUIDelegate:self];
328275
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:snsName];
@@ -331,8 +278,24 @@ - (void)p_doShareToSnsName:(NSString *)snsName{
331278
}
332279
}
333280
}
334-
335-
- (void)p_uploadENNote:(ENNote *)noteToSave{
281+
- (void)p_willUploadENNote:(ENNote *)noteToSave{
282+
if (!noteToSave) {
283+
[NSObject showHudTipStr:@"不支持保存到印象笔记"];
284+
return;
285+
}
286+
if (![[ENSession sharedSession] isAuthenticated]) {
287+
[[ENSession sharedSession] authenticateWithViewController:[BaseViewController presentingVC] preferRegistration:NO completion:^(NSError *authenticateError) {
288+
if (!authenticateError) {
289+
[self p_doUploadENNote:noteToSave];
290+
}else if (authenticateError.code != ENErrorCodeCancelled){
291+
[NSObject showHudTipStr:@"授权失败"];
292+
}
293+
}];
294+
}else{
295+
[self p_doUploadENNote:noteToSave];
296+
}
297+
}
298+
- (void)p_doUploadENNote:(ENNote *)noteToSave{
336299
if (noteToSave) {
337300
[NSObject showStatusBarQueryStr:@"正在保存到印象笔记"];
338301
[[ENSession sharedSession] uploadNote:noteToSave notebook:nil completion:^(ENNoteRef *noteRef, NSError *uploadNoteError) {
@@ -344,11 +307,25 @@ - (void)p_uploadENNote:(ENNote *)noteToSave{
344307
}];
345308
}
346309
}
310+
#pragma mark objToShare
311+
- (void)p_checkTitle{
312+
NSString *title;
313+
if ([_objToShare isKindOfClass:[Tweet class]]) {
314+
title = @"冒泡分享到";
315+
}else if ([_objToShare isKindOfClass:[UIWebView class]]){
316+
title = @"链接分享到";
317+
}else{
318+
title = @"分享到";
319+
}
320+
_titleL.text = title;
321+
}
347322

348323
- (NSString *)p_shareLinkStr{
349324
NSString *linkStr;
350325
if ([_objToShare isKindOfClass:[Tweet class]]) {
351326
linkStr = [(Tweet *)_objToShare toShareLinkStr];
327+
}else if ([_objToShare isKindOfClass:[UIWebView class]]){
328+
linkStr = [(UIWebView *)_objToShare request].URL.absoluteString;
352329
}else{
353330
linkStr = [NSObject baseURLStr];
354331
}
@@ -358,6 +335,8 @@ - (NSString *)p_shareTitle{
358335
NSString *title;
359336
if ([_objToShare isKindOfClass:[Tweet class]]) {
360337
title = [NSString stringWithFormat:@"%@ 的冒泡", [(Tweet *)_objToShare owner].name];
338+
}else if ([_objToShare isKindOfClass:[UIWebView class]]){
339+
title = @"Coding 链接";
361340
}else{
362341
title = @"Coding";
363342
}
@@ -367,6 +346,8 @@ - (NSString *)p_shareText{
367346
NSString *text;
368347
if ([_objToShare isKindOfClass:[Tweet class]]) {
369348
text = [(Tweet *)_objToShare content];
349+
}else if ([_objToShare isKindOfClass:[UIWebView class]]){
350+
text =[(UIWebView *)_objToShare stringByEvaluatingJavaScriptFromString:@"document.title"];
370351
}else{
371352
text = @"Coding 让开发更简单!";
372353
}
@@ -391,6 +372,20 @@ - (NSString *)p_imageUrlSquare:(BOOL)needSquare{
391372
}
392373
return imageUrl;
393374
}
375+
- (void)p_shareENNoteWithompletion:(ENNotePopulateFromWebViewCompletionHandler)completion{
376+
if ([_objToShare respondsToSelector:NSSelectorFromString(@"htmlMedia")]) {
377+
ENNote *note = [ENNote new];
378+
note.title = [self p_shareTitle];
379+
NSString *htmlStr;
380+
HtmlMedia *htmlMedia = [_objToShare valueForKey:@"htmlMedia"];
381+
htmlStr = htmlMedia.contentOrigional;
382+
htmlStr = [htmlStr stringByAppendingFormat:@"<p><a href=\"%@\">原始链接</a></p>", [self p_shareLinkStr]];
383+
note.content = [ENNoteContent noteContentWithSanitizedHTML:htmlStr];
384+
completion(note);
385+
}else if ([_objToShare isKindOfClass:[UIWebView class]]){
386+
[ENNote populateNoteFromWebView:(UIWebView *)_objToShare completion:completion];
387+
}
388+
}
394389
#pragma mark TranspondMessage
395390

396391
- (void)willTranspondMessage:(PrivateMessage *)message{
@@ -415,7 +410,7 @@ -(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)respon
415410
if(response.responseCode == UMSResponseCodeSuccess){
416411
NSString *snsName = [[response.data allKeys] firstObject];
417412
NSLog(@"share to sns name is %@",snsName);
418-
[self performSelector:@selector(showStatusBarSuccessStr:) withObject:@"分享成功" afterDelay:0.3];
413+
[NSObject performSelector:@selector(showStatusBarSuccessStr:) withObject:@"分享成功" afterDelay:0.3];
419414
}
420415
}
421416

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /