objective c - NSString returns empty with WebView Innerhtml -
i'm not hero objective-c please bare me i'm learning grips. although have great knowledge javascript, mac osx development seems hard.
i'm creating app relies on html5 , goal 'simply' save contents of specific div running javascript function. i've been searching everywhere , cannot seem find example me further or getting started. i'm hoping guys me point towards right direction.
ben taylor on twitter gave me starting code.
nsstring *html = [yourwebview stringbyevaluatingjavascriptfromstring:@"document.getelementbyid('nssidebar').innerhtml"]; nslog(@"contents:%@",html);
the log returns empty. may because webview has not been loaded in order have string find 'some-element' div. current code be:
- (void)applicationdidfinishlaunching:()mywebview { nsstring* filepath = [[nsbundle mainbundle] pathforresource:@"index" oftype:@"html" indirectory:@""]; nsurl* fileurl = [nsurl fileurlwithpath:filepath]; nsurlrequest* request = [nsurlrequest requestwithurl:fileurl]; [[webview mainframe] loadrequest:request]; nsstring *html = [webview stringbyevaluatingjavascriptfromstring:@"document.getelementbyid('nssidebar'.innerhtml"]; nslog(@"contents:%@",html); }
i hope me point right direction. if there's duplicate post - apologises , i'll remove topic.
two mistakes:
- you have syntax error: closing parenthesis missing.
- you need call
.tostring()
on returned object.
like this:
[webview stringbyevaluatingjavascriptfromstring:@"document.getelementbyid('nssidebar').innerhtml.tostring()"];
Comments
Post a Comment