2015-10-07 23 views
13

Yerel uygulamanıza Facebook Comments Plugin'u dahil etmeye çalışıyorum.Login to Facebook Comments iOS (hızlı) yanıt vermiyor

Facebook Comments Box tamamiyle sunuldu. Ben "to post a comment Facebook'a Girişi" tuşuna bastığınızda

enter image description here

ama hiçbir şey olmaz. Olayı bu işlevle yakalamaya çalışıyorum: func userContentController(userContentController: WKUserContentController,didReceiveScriptMessage message: WKScriptMessage) Ancak çalışmıyor.

import UIKit 
import WebKit 

class FacebookCommentsViewController: UIViewController, WKScriptMessageHandler{ 


    var webView: WKWebView! 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     // WKWebView 
     let contentController = WKUserContentController(); 
     contentController.addScriptMessageHandler(self,name: "callbackHandler") 

     let configuration = WKWebViewConfiguration() 
     configuration.userContentController = contentController 

     webView = WKWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), configuration: configuration) 
     webView.scrollView.bounces = false 
     self.view.addSubview(webView) 

     let facebookCommentsURL = "<!DOCTYPE html><html> <head> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> </head> <body> <div id=\"fb-root\"></div><script>(function(d, s, id){var js, fjs=d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js=d.createElement(s); js.id=id; js.src=\"http://connect.facebook.net/en_US/all.js#xfbml=1&appId=527957123932456&status=0\"; fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script> <div class=\"fb-comments\" data-href=\url-to-my-item data-width=\"470\" data-num-posts=\"5\"></div></body></html>" 

     webView.loadHTMLString(facebookCommentsURL, baseURL: NSURL(string:"my base url")) 
    } 

    func userContentController(userContentController: WKUserContentController,didReceiveScriptMessage message: WKScriptMessage) 
    { 
     print(message.name) 
    } 
} 

yanlış yapıyorum: Burada

benim kodudur?

+0

yılında

Objective C

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { if (!navigationAction.targetFrame.isMainFrame) { [webView loadRequest:navigationAction.request]; } return nil; } 

yılında:

Sonra bu sorunu önlemek için bu temsilci yöntemini çağırın? –

+0

Hayır. Sadece eklenti - https://developers.facebook.com/docs/plugins/comments – Luda

+0

Oturum açmak için facebook sdk kullanıyor musunuz? –

cevap

3

Fount it!

UIWebView ile WKWebView değiştirdim ve her şey çalıştı!

giriş başarılı yenileme webView iken:

func webViewDidFinishLoad(webView: UIWebView) 
    { 
     let currentURL : NSString = (webView.request?.URL!.absoluteString)! 
     if (currentURL.containsString("/login_success")) 
     { 
      self.reloadFacebookComments() 
     } 

     self.updateFramesAccordingToWebViewcontentSize() 
    } 
0

JS'niz hatalı mı? kod üreteci benim için oluşturulan: Dan

<div id="fb-root"></div> 
 
<script> 
 
    (function(d, s, id) { 
 
    var js, fjs = d.getElementsByTagName(s)[0]; 
 
    if (d.getElementById(id)) return; 
 
    js = d.createElement(s); 
 
    js.id = id; 
 
    js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.5"; 
 
    fjs.parentNode.insertBefore(js, fjs); 
 
    }(document, 'script', 'facebook-jssdk')); 
 
</script>

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta name=\ "viewport\" content=\ "width=device-width, initial-scale=1.0\"> 
 
</head> 
 

 
<body> 
 
    <div id=\ "fb-root\"></div> 
 
    <script> 
 
    (function(d, s, id) { 
 
     var js, fjs = d.getElementsByTagName(s)[0]; 
 
     if (d.getElementById(id)) return; 
 
     js = d.createElement(s); 
 
     js.id = id; 
 
     js.src = \"http://connect.facebook.net/en_US/all.js#xfbml=1&appId=527957123932456&status=0\"; fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk')); 
 
    </script> 
 
    <div class=\ "fb-comments\" data-href=\url-to-my-item data-width=\ "470\" data-num-posts=\ "5\"></div> 
 
</body> 
 

 
</html>

ne olduğunu gibi görünüyor söyleyebilir?

düzenleme: oh, belki de değil. Dizenin diğer kısımları bağlam dışıdır.

+0

Yeni bir sorunuz varsa, lütfen [Soru Sor] (http://stackoverflow.com/questions/ask) düğmesini tıklayarak sorun. İçerik sağlamaya yardımcı olursa, bu soruya bir bağlantı ekleyin. – Nullify

+0

@ 200OK Kafanız karıştı mı?Bu soru için bir cevaptır. –

+0

evet, facebookCommentsURL'de var. Cevabını gerçekten anlamadım – Luda

0

webviews değiştirmek için gereken yoktur. Asıl sorun, facebook script'in bu sayfayı yeni sayfada açmaya çalışmasıdır (_blank), bu yüzden betik web sayfasında çalışmamaktadır. WKWebView numaralı telefondaki bu sorunu önlemek için, kontrol cihazınızda WKUIDelegate numarasına sahip olmanız gerekmektedir ve bu kodu viewDidLoad yönteminize eklemeniz gerekmektedir. Eğer Cocoapods kullanıyorsunuz Swift

optional func webView(_ webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, 
    forNavigationAction navigationAction: WKNavigationAction, 
     windowFeatures windowFeatures: WKWindowFeatures) -> WKWebView?{ 
    if navigationAction.targetFrame.isMainFrame == nil { 
     webView.loadRequest(navigationAction.request) 
    } 

    return nil 
} 
İlgili konular