Embedding YouTube Video in UIWebView Using GData Library - ios

I'm building an app that will access a specific user's YouTube uploads. For this, I'm using Google's GData Objective-C client library. I use it to get access to the user's uploads feed and return an NSArray of GDataEntryYouTubeVideos. Using GDataEntryYouTubeVideo's htmlLink method, I load the url into a UIWebView with some special HTML code. However, the UIWebView just shows a red page.
The url that htmlLink returns is an https. When I manually replace https with http, the video loads in the UIWebView as expected. The following code does not load the YouTube video:
- (void)viewDidLoad
{
[super viewDidLoad];
GDataServiceGoogleYouTube *youtubeService = [[GDataServiceGoogleYouTube alloc] init];
NSURL *uploadUrl = [GDataServiceGoogleYouTube youTubeURLForUserID:#"higaara" userFeedID:kGDataYouTubeUserFeedIDUploads];
void(^handler)(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) = ^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error)
{
NSLog(#"handler");
// Get the link from the youtube entry
GDataEntryYouTubeVideo *youTubeVideo = [feed firstEntry];
NSString *htmlLinkString = [[youTubeVideo HTMLLink] href];
// Create an html string to load into the web view
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name =
\"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width =
212\"/></head><body style=\"background:#F00;margin-top:0px;margin-
left:0px\"><div><object width=\"212\" height=\"172\"><param name=\"movie\"
value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed
src=\"%#\"type=\"application/x-shockwave-flash\" wmode=\"transparent\"
width=\"212\" height=\"172\"></embed></object></div></body></html>",
htmlLinkString, htmlLinkString];
NSURL *tempurl = [NSURL URLWithString:#"http://onnati.net/apptrap"];
[webView loadHTMLString:htmlString tempurl];
};
[youtubeService fetchFeedWithURL:uploadUrl completionHandler:handler];
}
The html code in htmlString is from this blog post on YouTube's API blog.
If I replace the htmlLinkString creation line with this:
NSString *htmlLinkString = [[[youTubeVideo HTMLLink] href] stringByReplacingOccurrencesOfString:#"https://" withString:#"http://"];
then the UIWebView properly loads the video.
I'm not sure what I'm missing here. Do I need to do something more in order to load an https url into a UIWebView? Or is there something I'm missing in the GData library that returns a standard non-secure URL?

To hopefully close out this old question:
Some older versions of mobile Safari automatically translated YouTube Flash embeds into clickable thumbnails that would use an alternative playback mechanism, since iOS obviously doesn't support Flash. The logic to turn those embeds into clickable links probably has a hardcoded pattern that checks for http:// and not https:// URLs.
The recommended way to embed YouTube videos within an iOS UIWebView is using the iframe embed. You can obtain the video id from the Data API and then use that to construct the iframe URL.

Related

Ios Intercept UIWebview request via NSURLCache and replace the response

I'm developing an Epub Reader for Ios, notice that i don't use any epub library and i parse the epub myself.
I needed a method for loading resource from epub into the UIWebView, for example images or CSS file ... (resource that requested in the html files).
for this purpose i decided to use NSURLCache class and override it's method (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
so i can intercept the request and find the right data from epub then create a NSCachedURLResponse and return it.
so far so good. BUT the the problem is that the data (i.e images) wont show in the webview. look at below code:
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
/*
the request.URL is something like this applewebdata://UUID/OEBPS/Images/cover.jpg ,
so the getResourcePathFromRequest give me the path OEBPS/Images/cover.jpg,
so i can find the right data in epub
*/
NSString *resourcePath = [self getResourcePathFromRequest:request.URL];
/*
ResourceResponse is a class contaning data and mimeType,
i tested this and the data and mimeType are good so this is not the problem
*/
ResourceResponse *response = [[self getBook] fetch:resourcePath];
NSURLResponse *urlResponse = [[NSURLResponse alloc] initWithURL:[request URL] MIMEType:response.mMimeType expectedContentLength:[response.mData length] textEncodingName:#"UTF-8"];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlResponse data:response.mData];
return cachedResponse;
}
at this point everything looks normal, the method getting called, the right data being found and the response object return, but it wont load the image into the webview.
so what is the problem?
tnx in advance.
NOTE: i'm new in ios programming world (coming from android:)
I loaded the data with base url (http://localhost) and everything went fine.

In iOS how we come back to initial view after google+ sharing?

I am implementing google plus sharing successfully by using the following lines of code. But after sharing it doesn't come back to initial screen. Is there any delegate call after completing the procedure of sharing
- (void)googelSharing{
//Set bool for Handler
[self setUserDefaultForSharing:NO];
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.clientID = kClientId;
signIn.scopes = #[#"https://www.googleapis.com/auth/plus.login"];
id<GPPShareBuilder> shareBuilder = [[GPPShare sharedInstance] shareDialog];
// This line will fill out the title, description, and thumbnail from
// the URL that you are sharing and includes a link to that URL.
[shareBuilder setURLToShare:[NSURL URLWithString:#"https://www.example.com/restaurant/sf/1234567/"]];
[shareBuilder open];
}
For your information Apple will reject the app if you use
a web page in mobile Safari for creating an account or logging in, then returns the user to the app.
See this discussion.
So Google release new Google Plus SDK for sign in
Google Sign In SDK 2.0 is documented on the new dev site :
https://developers.google.com/identity/sign-in/ios/
Hope this helps
It is not possible because when u request to open the specific web page iOS automatically transfer this request to Safari . And Safari will open that web page.
One thing you can do is used UIWebView in your app and open the web page inside UIWebView and u can easily get back to pervious viewController OR web page by adding simple UIButton in currentViewController class.
Here is code
UIWebView *webView = [[UIWebView alloc]init];
NSString *urlString = #"https://www.example.com/restaurant/sf/1234567/";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[self.view addSubview:webView];
If u are used UIWebView than refer this answer it will help you Back Button on UIWebView

iOS embed UIWebView change with new youtube API

I'm starting to notice a change in the way that youtube videos are being loaded into UIWebViews and I wanted to know if this is behavior we should be expecting in the future and/or if we can replicate the previous functionality.
Comparison screenshot :
Old on the right, new on the left. The added youtube button allows users to leave the youtube video and go into the youtube web interface. I would like to be able to prevent the user from leaving the video being played.
I am currently using a category on UIWebView like this :
- (void)loadYouTubeEmbed:(NSString *)videoId
{
NSString* searchQuery = [NSString stringWithFormat:#"http://www.youtube.com/embed/%#?showinfo=0&loop=1&modestbranding=1&controls=0",videoId];
searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:searchQuery]];
[self loadRequest:request];
}
I've noticed that my query will respect either modestbranding=1 or showinfo=0 but not both at the same time. Will this change as the youtube redesign rolls out?
When the Youtube video is loaded, and webView:shouldStartLoadWithRequest:navigationType: is hit, you should be able to filter out that link so it won't proceed.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] isEqualToString:#"<URL String Youtube spits out when video selected>"]) {
NSLog(#"Blocking YouTube...");
return NO;
} else {
NSLog(#"Link is fine, continue...");
return YES;
}
}

Facebook feeds on iOS app?

How can I display face book feeds related to a group on an iOS App?
Is there any JSON/JSONP call to fetch the feeds??
Any help is Appreciated.
The simplest - is just to show a web page on web view. Facebook manages itself to fit the width of your webView control.
// Load facebook data
NSString *fbUrlAddress = #"http://www.facebook.com/FLORtiles"; // here you put your own group
//Create a URL object.
NSURL *fbUrl = [NSURL URLWithString:fbUrlAddress];
NSString *webHTMLfb = [NSString stringWithContentsOfURL:fbUrl encoding:NSUTF8StringEncoding error:NULL];
//NSLog(#"webHTML of facebook: %#", webHTMLfb);
//URL Requst Object
NSURLRequest *fbRequestObj = [NSURLRequest requestWithURL:fbUrl];
//Load the request in the UIWebView.
[fbWebView loadRequest:fbRequestObj];
You might need to get familiar with Facebook's Graph Api
Go through this -> https://developers.facebook.com/docs/mobile/ios/build/

UIWebView reuse

I want to use UIWebview in different view controllers,but i don't want these view controller to reload the web page, in another way, I want a UIWebview to load the page once,and used it anywhere.
SO I want to create a web view and load the webpage in a singleton way.
My question is : is this possible or appropriate?
Thanks a lot.
What's type of your html to present ? Is it static HTML content or dynamic URL source?
For static HTML content
I suggest you to create prefer HTML file and load them directly.
NSString *htmlContent = #"Your Static HTML Code";
NSData *htmlData = [htmlContent dataUsingEncoding:NSUTF8StringEncoding];
if (htmlData) {
NSString *_path = [[NSBundle mainBundle] bundlePath];
NSURL *_baseURL = [NSURL fileURLWithPath:_path];
[self.BottomWebView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:_baseURL];
}
For dynamic HTML content base on your URL source
When you have loaded the data in the first request , you can store them with NSData that can be the properties in your singleton class. Of course, you can call them any where, any ViewController to get your data and load them in your UIWebview with once connection.
hope it helps you !

Resources