简体   繁体   English

如何从Web视图向objc发送大量文本?

[英]How can I send a large amount of text from a webview to objc?

I'm trying to build an "iBooks-like" reader for iPhone. 我正在尝试为iPhone构建“类似于iBooks”的阅读器。 I'm receiving a huge amount of text (html) from my webservice, columnizing it (inside a webview, via javascript) and then trying to send an array of "columns" back to objective c from javascript (from the webview) in order to create the viewControllers I need to use UIPageViewController (one per column, or basically one per page). 我从Web服务接收到大量文本(html),将其列化(通过JavaScript在webview内部),然后尝试按顺序从javascript(从webview)将“列”数组发送回目标c要创建viewControllers,我需要使用UIPageViewController(每列一个,或者基本上每页一个)。

I'm using shouldStartLoadWithRequest to intercept every location change in the webview, taking the url as method to call and the first string after a "/" as parameter for that method in this way: 我正在使用shouldStartLoadWithRequest来拦截web视图中的每个位置更改,以这种方式将url作为要调用的方法,并将“ /”后的第一个字符串作为该方法的参数:

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

    //NSLog(@"REQUEST: %@", request);
    NSURL *URL = [request URL]; //Get the URL

    if ( [[URL scheme] isEqualToString:@"objc"] ) {

        NSMutableString *host = [[URL host] mutableCopy];

        [host appendString:@":"];

        SEL method =  NSSelectorFromString( host );

        NSArray *chunks = [URL pathComponents];

        NSString *stringToPass = [[chunks objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        NSLog(stringToPass);

        if ([self respondsToSelector:method])
        {
            [self performSelector:method withObject:stringToPass afterDelay:0.1f];
        }

        return NO;
    }

    return YES;
}

My main problem right now is that, probably, the text is too long for the url to actually contain it (in fact if I NSLog it, it gets cut). 我现在的主要问题是,可能文本太长而无法实际包含url(实际上,如果我使用NSLog记录,它将被剪切掉)。 Is there a better method to achieve the same thing? 有没有更好的方法来实现同一目标?

Should I create a local server or open a socket to send data from JS to objc or there is an easier way at the moment? 我应该创建本地服务器还是打开套接字以将数据从JS发送到objc,或者现在有一种更简单的方法?

You could ask to the webview the code you need by calling a JS function like that: 您可以通过调用JS函数,向网络视图询问所需的代码:

NSString* htmlData = [_webView stringByEvaluatingJavaScriptFromString:@"jsFunction()"]; NSString * htmlData = [_webView stringByEvaluatingJavaScriptFromString:@“ jsFunction()”];

It's a bit of work, but you could define a chunking protocol to use for sending the data back in the location changes. 这需要一些工作,但是您可以定义一个分块协议,以用于在位置更改中将数据发送回去。 This way each "column" could be broken up into a discrete and digestible number of pieces, and assembled on the iOS side. 这样,每个“列”都可以分解为离散且可消化的片段,并在iOS端进行组装。

For example: 例如:

http://bridge.myapp.com/?segnum=2&segtotal=5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM