The code below saves the Word.docx file in the application document directory at startup. Then it reads this file into the UIWebView while viewing the DidLoad. Finally, he expects the UIWebView to load the document before extracting text from the UIWebView. Remember to execute the UIWebViewDelegate protocol in your View controller header file. And, of course, a Word document should be included in your project. Be sure to add the document to Phase Build> Copy Package Resources.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [docsDirectory stringByAppendingPathComponent:@"Text.docx"]; NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Text.docx"]]; [data writeToFile:path atomically:YES]; } - (void)viewDidLoad { [super viewDidLoad]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *wordFilePath = [documentsDirectory stringByAppendingPathComponent:@"Text.docx"]; UIWebView *theWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; NSURL *wordFileUrl = [NSURL fileURLWithPath:wordFilePath]; NSURLRequest *request = [NSURLRequest requestWithURL:wordFileUrl]; [theWebView loadRequest:request]; theWebView.delegate = self; [self.view addSubview:theWebView]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *text = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerText"]; }
source share