xcode与html网站的互动

时间:2015-05-13 07:07:26

标签: html xcode macos cocoa

我想写一个小程序,它将一个字符串发送到一个网站,然后得到答案字符串。

关于以下网站:http://www.pandorabots.com/pandora/talk?botid=ec8b3be33e34bf73

  1. 程序将字符串发送到网站上的文本字段" Frage:..."
  2. 程序员在"兄弟B之后取回字符串!:"
  3. 它是OS X的可可应用程序。

    -(BOOL)getStringFromWebView:(NSString*)frage
    {
    
    //----------------Put the question (frage) into the text field on the web page
    
        if (frage.length != 0) {
            //create js strings
            NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", frage];
            //autofill the form
            [self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
        }
    
        //-------------Press 'return' to send the message 
        //PROBLEM: Webview is not active. It do not work...
    
        CGEventRef push = CGEventCreateKeyboardEvent(NULL, 0x24, true);
        CGEventRef release = CGEventCreateKeyboardEvent(NULL, 0x24, false);
        CGEventPost(kCGHIDEventTap, push);
        CGEventPost(kCGHIDEventTap, release);
    
        //I tried this to focus the web view:
        //[self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"input\").setFocus();"];
    
    
        //----------get response from web page (work!):
    
        WebFrame *frame = [self.webView mainFrame];
        WebDataSource *source = [frame dataSource];
        NSData *data = [source data];
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
       // NSLog(@"%@",str);
        NSArray *woerter1 = [str componentsSeparatedByString: @"<I>Brother B!</I>: "];
        NSArray *woerter2 = [woerter1[1] componentsSeparatedByString: @"<form method=\"POST\">"];
    
        self.out3.stringValue = woerter2[0];
    
     return true;
    }
    

    其实我真的不知道怎么做......请帮助我!

1 个答案:

答案 0 :(得分:0)

您可以在WebView / IUWebView对象中加载您的网页,并通过JavaScript桥库与其进行交互。您可以使用此库link向本机应用程序发送消息给JavaScript。

接下来,您可以在html-client-side使用JavaScript来管理此消息并执行表单。

干杯