open webpage within OF app on iPhone

Hi

Instead of sending users to a page in Safari from a link in my app, i’d like to load and show that page within my app (in a rectable region), along with other graphics from my app.

A little searching around I see UIWebView as a possible answer, but have not used this within OF. Has anyone done this? Also any way to detect if the page has loaded or not? Any other pointers?

Thanks

I’ve used UIWebViews on top of an app with an openFrameworks core, basically by extending the iPhoneGuiExample view. You can easily add a webview to that example, and then talk to it.

I added a transparent background webview with code, but you can also use the interface builder.

in your .h

  
  
UIWebView * myWebView;  
  

in your .cpp

  
  
  
-(void)viewDidLoad {  
	myApp = (testApp*)ofGetAppPtr();  
  
	myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,323)]; //x,y,w,h    
	myWebView.tag = 0; // for reference purposes  
	myWebView.backgroundColor = [UIColor clearColor];  
	myWebView.opaque = false;  
	myWebView.delegate = self; // this refers to the GuiExample view. Because of this you can put functions like webViewDidFinishLoad to the GuiExample and the webView will know where to look for it.  
	myWebView.scalesPageToFit = YES;  
  
	//also handy: I didn't want the directional lock, this is the trick to do that  
	for (id subview in myWebView.subviews)  
	{		  
	    if ([[subview class] isSubclassOfClass: [UIScrollView class]]){  
	        ((UIScrollView *)subview).bounces = YES;  
	        ((UIScrollView *)subview).directionalLockEnabled = NO;  
	    }  
	}  
	  
	[self.view addSubview:myWebView]; // this adds the webview to the GuiExample  
  
}  
  

Then for instance if you want to show a complete HTML string with local images you need to make sure the baseURL refers to the location of the App on your device:

  
  
-(void)loadHtmlStuff: (NSString *)myHtmlString{  
	  
	NSString *path = [[NSBundle mainBundle] bundlePath];  
	NSURL *baseURL = [NSURL fileURLWithPath:path];  
	  
	[myWebView loadHTMLString:myHtmlString baseURL:baseURL];  
  
}  
  

UIWebViews, like any other view, comes with a lot of great built-in functionality, in this case for instance the -webViewDidFinishLoad function. Add this to GuiExample:

  
  
-(void)webViewDidFinishLoad:(UIWebView *)webView{  
	  
	switch ([webView tag]){  
			  
		case 0: //the reference tag we gave myWebView  
			  
	        [self hideMyView:false]; //some function in GuiExample that only shows the webview when completely loaded  
                myApp->myWebViewLoadCompleted(); // call oF function to handle the rest  
                break;   
        }  
}  
  

You can also call a javascript function that you’ve put with HTML inside UIWebView to set / check for things. It’s really handy! :slight_smile:

  
  
NSString *myJSFunction = [NSString stringWithFormat:@"javascriptFunction(%d,%d);", firstInt, secondInt];  
[myWebView stringByEvaluatingJavaScriptFromString: myJSFunction];  
  

Hi eelke, Chris OShea, and everybody!

I would really like to stream a video from a webpage within my app. Is this even possible?

I have tried this add-on with iOS 5.1 + oF 0070, 0071, 0072, and 0073 with no success: https://github.com/daanvanhasselt/ofxiPhoneWebViewController

There seems to be a problem with the core oF libraries and the add-on. Any suggestions?

This leaves me with the instructions given by eelke in this thread. I have tried to follow his instructions and added it to the iPhoneGuiExample in oF 0073. It doesn’t work when I add this code to the testApp.h and testApp.mm file. Perhaps because I don’t quite understand the walkthrough. Is there any “complete” code example out there I can use?

Any suggestions are welcome! :slight_smile:

Kind regards,
smornish