2013-04-18 49 views
0

我正在開發一個iOS的自定義webview,當選擇網頁上的某些元素時,應該給用戶特殊的選擇,因此我擴展了UIWebview,並將我自己的按鈕添加到sharedMenuController。由於所顯示的頁面是由xml使用xsl構成的,所以在某些標籤中有額外的數據,例如從UIWebview檢索HTML標籤使用

<p data-type="MC"><img src="annotation.png"></p> 

當選擇圖像時,sharedMenuController彈出,並且如果我按下動作按鈕,我想接收包含IMG標籤的標籤。問題是,使用window.getSelection()。innerHTML.toString()給我一個空字符串和window.getSelection()。getRangeAt(0).commonAncestorContainer.innerHTML.toString()應該是什麼p標籤,給我整個html。

這是我的課:

@implementation UICustomWebView 

+ (void)initialize 
{ 
    [super initialize]; 
    UIMenuItem *itemA = [[UIMenuItem alloc] initWithTitle:@"Action" action:@selector(a:)]; 
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:itemA, nil]];  
} 

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{  
    if (action == @selector(defineSelection:)) 
    { 
     return YES; 
    } 
    else if (action == @selector(translateSelection:)) 
    { 
     return YES; 
    } 
    else if (action == @selector(copy:)) 
    { 
     return NO; 
    } 
    else if (action == @selector(a:)) 
    { 
     return YES; 
    } 

    return [super canPerformAction:action withSender:sender]; 
} 

-(void) a:(id)sender 
{ 
    NSLog(@"a %@", [self stringByEvaluatingJavaScriptFromString:@"window.getSelection().getRangeAt(0).commonAncestorContainer.innerHTML.toString()"]); 
} 
@end 

回答

0
NSString *htmlString = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]; 
+0

沒有這個給整個html文件? 編輯:只是檢查它,就像我說的,給整個文件 – Andy

1

試試這個:

NSString *htmlString=[[webView stringByEvaluatingJavaScriptFromString:@"getSelectionHtml()"]mutableCopy]; 
+0

這是調用一個非本地JavaScript函數,所以沒有函數的內容,這個答案是不完整的。我只是用代碼添加了一個答案。 – Andy

0

發現了什麼問題了。顯然,當將單個對象放在p-tag中時,可以在選擇對象時選擇整個p-tag。因此,由於p標籤是身體的孩子,因此返回完整的html是正確的。我現在在我的img周圍使用了一個簡單的自定義標籤,並且我正確地獲取了這些值。

0

發現了什麼,我一直在尋找基於阿賈伊的答案真正的答案:

的NSString * htmlString = [[webView的stringByEvaluatingJavaScriptFromString:@ 「getSelectionHtml()」] mutableCopy]。

指向一個非本地函數,因此沒有它調用的javascript函數是沒有用的。我在http://snipplr.com/view.php?codeview&id=10912找到了一個功能,這似乎是我所需要的。所以通過從我的xsl注入這個JS到我的頁面給我什麼我需要我的數據。

這是JavaScript函數:

function getSelectionHTML() 
     { 
      var userSelection; 
      if (window.getSelection) 
      { 
       // W3C Ranges 
       userSelection = window.getSelection(); 
       // Get the range: 
       if (userSelection.getRangeAt) 
        var range = userSelection.getRangeAt (0); 
       else 
       { 
        var range = document.createRange(); 
        range.setStart (userSelection.anchorNode, userSelection.anchorOffset); 
        range.setEnd (userSelection.focusNode, userSelection.focusOffset); 
       } 
       // And the HTML: 
       var clonedSelection = range.cloneContents(); 
       var div = document.createElement ('div'); 
       div.appendChild (clonedSelection); 
       return div.innerHTML; 
      } 
      else if (document.selection) 
      { 
       // Explorer selection, return the HTML 
       userSelection = document.selection.createRange(); 
       return userSelection.htmlText; 
      } 
      else 
      { 
       return ''; 
      } 
     };