Downloading files WKWebView ios - ios

I'm in the progress of migrating my app from UIWebView to WKWebView. All is going well and working as I tinker more with it. However, I notice now that I can't download forum attachments.
I'm using HCDownload, and thus far has always worked perfect for me, so I know it's not on that end. I believe its the request, but I cant figure it out.
I know the following:
UIWebView => WKWebView Equivalent
--------------------------------------------------------------
didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction
So with that known I am trying below:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest *request = navigationAction.request;
NSURL *fileURl = [request URL];
NSString *externalFileExtension = [fileURl pathExtension];
NSString *internalFileExtension = [[fileURl absoluteString] pathExtension];
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:dlvc];
dlvc.delegate = self;
vc.transitioningDelegate = self;
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
//External file extensions
if ([fileExtensions containsObject:[externalFileExtension lowercaseString]]) {
[dlvc downloadURL:fileURl userInfo:nil];
[self presentViewController:vc animated:YES completion:nil];
[vc release];
NSLog(#"externalURL is %#", fileURl);
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
//Internal file links
if ([fileExtensions containsObject:[internalFileExtension lowercaseString]]) {
[self presentViewController:vc animated:YES completion:nil];
[dlvc downloadURL:fileURl userInfo:nil];
[vc release];
NSLog(#"internalURL is %#", fileURl);
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
It will fire my download controller by finding the file extension, but instead it downloads the index.php, not the file.
Example url:
https://example.com/index.php?app=core&module=attach&section=attach&attach_id=1234=example.zip;
What am I doing wrong. It worked fine before in UIWebView and I know things are done differently. But how can it download something from say dropbox just fine, but an attached file on a forum gets goofed up.
Any help would be appreciated

Well heres what I ended up with, and all seems to be working fine.
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
decisionHandler(WKNavigationResponsePolicyAllow);
//NSLog(#"decidePolicyForNavigationResponse");
}
//Download manager
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest *request = navigationAction.request;
fileURL = request.URL;
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:dlvc];
vc.transitioningDelegate = self;
dlvc.delegate = self;
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
//Internal file links
NSString *internalFileExtension = fileURL.absoluteString.pathExtension;
if ([fileExtensions containsObject:[internalFileExtension lowercaseString]]) {
//Fire download
[dlvc downloadURL:fileURL userInfo:nil];
[self presentViewController:vc animated:YES completion:nil];
[vc release];
NSLog(#"internalURL is %#", fileURL);
if (decisionHandler) {
decisionHandler(WKNavigationActionPolicyCancel);
}
return;
}
//External file extensions
NSString *externalFileExtension = fileURL.pathExtension;
if ([fileExtensions containsObject:[externalFileExtension lowercaseString]]) {
//Fire download
[dlvc downloadURL:fileURL userInfo:nil];
[self presentViewController:vc animated:YES completion:nil];
[vc release];
NSLog(#"externalURL is %#", fileURL);
if (decisionHandler) {
decisionHandler(WKNavigationActionPolicyCancel);
}
return;
}
}
if (decisionHandler) {
decisionHandler(WKNavigationActionPolicyAllow);
}
}

You are probably downloading a not-authorized page because your WKWebView and HCDownload instances don't share cookie sessions like UIWebView could. That's a necessary trade-off for the WK2 process model's speed & security improvements.
I added downloading to my WKWebKit-based OSX/Swift mini-browser by implementing _WKDownloadDelegate. Its a totally undocumented private protocol as of El Cap and iOS9. It lets your navigation delegate call decisionHandler(_WKNavigationResponsePolicyBecomeDownload) whereby WebKit will download in the background after giving you the chance to pick/change a file name. I haven't the foggiest idea on how to implement a file picker in iOS yet, nor if using that protocol would be allowed by the App Store reviewers. But my app now does correctly handle downloading from session-authentication sites like your PHP forum.

Swift 4:
ChrisOSX's sollution also solved my issue. I'm trying to download a document from a link on a site after logging to that site. I've used WKZombie to scrape the link, and then I wanted to download the file using Alamofire. I added this
if let httpResponse = navigationResponse.response as? HTTPURLResponse, let url = httpResponse.url {
let allHeaders = httpResponse.allHeaderFields.reduce([String : String](), { (result, next) -> [String : String] in
guard let stringKey = next.key as? String, let stringValue = next.value as? String else { return result }
var buffer = result
buffer[stringKey] = stringValue
return buffer
})
let cookies = HTTPCookie.cookies(withResponseHeaderFields: allHeaders, for: url)
for cookie in cookies {
HTTPCookieStorage.shared.setCookie(cookie)
}
}
to:
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Void)
After this, the cookies are stored in the shared cookies, and I was able to download the file with Alamofire without and changes to the download methods..

Related

Azure AD B2C getting oauthConnection Error: Bad Request

While trying to integrate Azure AD B2C, I am stuck with an error "oauthConnection Error: Bad Request". Following their given sample app it all works fine. But after integrating the same copy paste code from the working sample app, and trying to log in with Facebook or Google Plus, it throws an error! I am pretty sure that every credential that I used in the sample app is the same for my app. Any idea about this will be highly appreciated. Here is my code, AppDelegate.m
#import "AppData.h"
#import "NXOAuth2.h"
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupOAuth2AccountStore];
// Override point for customization after application launch.
return YES;
}
- (void)setupOAuth2AccountStore {
AppData *data = [AppData getInstance]; // The singleton we use to get the settings
NSDictionary *customHeaders =
[NSDictionary dictionaryWithObject:#"application/x-www-form-urlencoded"
forKey:#"Content-Type"];
// Azure B2C needs
// kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters for
// sending policy to the server,
// therefore we use -setConfiguration:forAccountType:
NSDictionary *B2cConfigDict = #{
kNXOAuth2AccountStoreConfigurationClientID : data.clientID,
kNXOAuth2AccountStoreConfigurationSecret : data.clientSecret,
kNXOAuth2AccountStoreConfigurationScope :
[NSSet setWithObjects:#"openid", data.clientID, nil],
kNXOAuth2AccountStoreConfigurationAuthorizeURL :
[NSURL URLWithString:data.authURL],
kNXOAuth2AccountStoreConfigurationTokenURL :
[NSURL URLWithString:data.tokenURL],
kNXOAuth2AccountStoreConfigurationRedirectURL :
[NSURL URLWithString:data.bhh],
kNXOAuth2AccountStoreConfigurationCustomHeaderFields : customHeaders,
// kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters:customAuthenticationParameters
};
[[NXOAuth2AccountStore sharedStore] setConfiguration:B2cConfigDict
forAccountType:data.accountIdentifier];
}
LoginViewController.m
#import "AppData.h"
#import "LoginViewController.h"
#import "NXOAuth2.h"
#interface LoginViewController ()
#end
#implementation LoginViewController {
NSURL *myLoadedUrl;
bool isRequestBusy;
}
// Put variables here
- (void)viewDidLoad {
[super viewDidLoad];
// OAuth2 Code
self.loginView.delegate = self;
[self requestOAuth2Access];
[self setupOAuth2AccountStore];
NSURLCache *URLCache =
[[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
}
- (void)resolveUsingUIWebView:(NSURL *)URL {
// We get the auth token from a redirect so we need to handle that in the
// webview.
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:#selector(resolveUsingUIWebView:)
withObject:URL
waitUntilDone:YES];
return;
}
NSURLRequest *hostnameURLRequest =
[NSURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0f];
isRequestBusy = YES;
[self.loginView loadRequest:hostnameURLRequest];
NSLog(#"resolveUsingUIWebView ready (status: UNKNOWN, URL: %#)",
self.loginView.request.URL);
}
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
AppData *data = [AppData getInstance];
NSLog(#"webView:shouldStartLoadWithRequest: %# (%li)", request.URL,
(long)navigationType);
// The webview is where all the communication happens. Slightly complicated.
myLoadedUrl = [webView.request mainDocumentURL];
NSLog(#"***Loaded url: %#", myLoadedUrl);
// if the UIWebView is showing our authorization URL or consent URL, show the
// UIWebView control
if ([request.URL.absoluteString rangeOfString:data.authURL
options:NSCaseInsensitiveSearch]
.location != NSNotFound) {
self.loginView.hidden = NO;
} else if ([request.URL.absoluteString rangeOfString:data.loginURL
options:NSCaseInsensitiveSearch]
.location != NSNotFound) {
// otherwise hide the UIWebView, we've left the authorization flow
self.loginView.hidden = NO;
} else if ([request.URL.absoluteString rangeOfString:data.bhh
options:NSCaseInsensitiveSearch]
.location != NSNotFound) {
// otherwise hide the UIWebView, we've left the authorization flow
self.loginView.hidden = YES;
[[NXOAuth2AccountStore sharedStore] handleRedirectURL:request.URL];
} else {
self.loginView.hidden = NO;
}
return YES;
}
#pragma mark - UIWebViewDelegate methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// The webview is where all the communication happens. Slightly complicated.
}
- (void)handleOAuth2AccessResult:(NSURL *)accessResult {
// parse the response for success or failure
if (accessResult)
// if success, complete the OAuth2 flow by handling the redirect URL and
// obtaining a token
{
[[NXOAuth2AccountStore sharedStore] handleRedirectURL:accessResult];
} else {
// start over
[self requestOAuth2Access];
}
}
- (void)setupOAuth2AccountStore {
[[NSNotificationCenter defaultCenter]
addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
object:[NXOAuth2AccountStore sharedStore]
queue:nil
usingBlock:^(NSNotification *aNotification) {
if (aNotification.userInfo) {
// account added, we have access
// we can now request protected data
NSLog(#"Success!! We have an access token.");
} else {
// account removed, we lost access
}
}];
[[NSNotificationCenter defaultCenter]
addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification
object:[NXOAuth2AccountStore sharedStore]
queue:nil
usingBlock:^(NSNotification *aNotification) {
NSError *error = [aNotification.userInfo
objectForKey:NXOAuth2AccountStoreErrorKey];
// Always got stuck here while trying to login with any credentials
NSLog(#"Error!! %#", error.localizedDescription);
}];
}
- (void)requestOAuth2Access {
AppData *data = [AppData getInstance];
[[NXOAuth2AccountStore sharedStore]
requestAccessToAccountWithType:data.accountIdentifier
withPreparedAuthorizationURLHandler:^(NSURL *preparedURL) {
NSURLRequest *r = [NSURLRequest requestWithURL:preparedURL];
[self.loginView loadRequest:r];
}];
}
ViewController.m
#import "ViewController.h"
#import "AppData.h"
#import "LoginViewController.h"
#import "NXOAuth2.h"
// Login Action
- (IBAction)login:(id)sender {
LoginViewController *userSelectController =
[self.storyboard instantiateViewControllerWithIdentifier:#"login"];
[self.navigationController pushViewController:userSelectController
animated:YES];
}
In case if anybody stumbles in this, Here is the solution
Go to pod, NXOAuth2Client.m and replace the method
- (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)redirectURL; with the below code
- (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)redirectURL;
{
NSAssert1(!authConnection, #"authConnection already running with: %#", authConnection);
NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL];
[tokenRequest setHTTPMethod:self.tokenRequestHTTPMethod];
[authConnection cancel]; // just to be sure
self.authenticating = YES;
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"authorization_code", #"grant_type",
clientId, #"client_id",
// clientSecret, #"client_secret",
[redirectURL absoluteString], #"redirect_uri",
authGrant, #"code",
nil];
if (self.desiredScope) {
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:#" "] forKey:#"scope"];
}
if (self.customHeaderFields) {
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[tokenRequest addValue:obj forHTTPHeaderField:key];
}];
}
if (self.additionalAuthenticationParameters) {
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
}
authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
requestParameters:parameters
oauthClient:self
delegate:self];
authConnection.context = NXOAuth2ClientConnectionContextTokenRequest;
}
Commenting clientSecret solved the issue

SFSafariViewController does not reopen a webview inline URL link

I have a Webview with inline URL links which are opened with SFSafariViewController, as shown below:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([SFSafariViewController class] != nil) {
NSString *inR = [[inRequest URL] absoluteString];
NSURL *inReq = [NSURL URLWithString:inR];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
} else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}
#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
// Load finished
}
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
NSLog(#"DONE PRESSED!!!");
}
When I press the DONE button correctly returns to my Webview. The problem is that if I will press again on the same inline link, it does not open with SFSafariViewController but in Webview which is not what i desire. I tried to force Webview reload in safariViewControllerDidFinish but without success.
Could you please help? Thanks!
The code corrected as below (following proposal of beyowulf) and now is working OK:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([SFSafariViewController class] != nil) {
NSString *inR = [[inRequest URL] absoluteString];
NSURL *inReq = [NSURL URLWithString:inR];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
return NO;
} else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
} else {
return YES;
}
}
#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
// Load finished
}
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
NSLog(#"DONE PRESSED!!!");
}

How to play youtube/vimeo video within the application in iPhone

I am building an application where I want to play video from url like Youtube, Vimeo, Direct url. I am making custom player using AVPlayer to play a video from a direct url of video clip(like www.abc/play.mp4). But later I faced a huge issue to play youtube & vimeo video. And after searching a lot I found these link where it says without using UIWebview I can't play a Youtube link:
Play YouTube videos with MPMoviePlayerController instead of UIWebView
Playing video from youtube link without UIWebView
So I just used this code:
NSString *youTubeVideoHTML = #"<html><head><style>body{margin:0;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:\'100%%\', height:'200px', videoId:\'%#\', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, videoId];
self.embedded_player_view.mediaPlaybackRequiresUserAction = NO;
[self.embedded_player_view loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
Now when I click on youtube/Vimeo video link from tableview its playing the video with the default player i.e. quicktime player. Its not running the video within the UIWebview frame itself.
But I want to show the video in the 1st half of the screen i.e my UIWebview frame. Is that possible?
In my app I can see this:
when clicking on red play button I can see the video in full screen in quicktime player like this:
But I want to show the video within the same webView frame not via quick time player. It should play like this:
Same does in MusicTube and PlayTube.
Or is there any other way to achieve the same? Any help will be appreciated. Thanks in advance.
I've used this class just for that.
The video you see inside the UIViewController is playable in its current size.
That's the only code I've used:
UIView *videoContainerView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 200)];
[self.view addSubview:videoContainerView];
XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:#"_OBlgSz8sSM"];
[videoPlayerViewController presentInView:videoContainerView];
[videoPlayerViewController.moviePlayer play];
For Vimeo player you can check this link https://github.com/lilfaf/YTVimeoExtractor it plays the video in real player and if you want to run the video in uiwebview,here is the code for that https://stackoverflow.com/a/15918011/1865424.
Pass you URL of YouTube Video in “urlStr”.
//In .h file
UIWebView *videoView;
// In .m file
videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 385)];
[self embedYouTube :urlStr frame:CGRectMake(0, 0, 320, 385)];
[self.view addSubview:videoView];
// methos to embed URL in HTML & load it to UIWebView
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame
{
NSString* embedHTML = #”\
<html><head>\
<style type=\”text/css\”>\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\”margin:0\”>\
<embed id=\”yt\” src=\”%#\” type=\”application/x-shockwave-flash\” \
width=\”%0.0f\” height=\”%0.0f\”></embed>\
</body></html>”;
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
Courtsey :- http://nanostuffs.com/Blog/?p=641
Hope This Will Help You Out.
If this doesn't help you out please check out these links:-
http://blog.softwareispoetry.com/2010/03/how-to-play-youtube-videos-in-your.html
https://gist.github.com/darkredz/5334409
http://maniacdev.com/2012/02/open-source-library-for-easily-playing-a-youtube-video-in-an-mpmovieplayer
If you wanna play youtube, here's a link to youtube player project on github, It's really helpful. and yes it is possible to play it in your uiwebview, just give the webview the url and tell it to load, it shouldn't open in the default player, as far as i believe at least.
I don't know about the Vimeo but for youtube videos you can use HCYouTubeParser to fetch mp4 urls for the youtube videos which can be later played on AVPlayer or MpMoviePlayerVC as per requirements.
I used youtube and vimeo in my Project, I share my codings for you it will very hopeful for you
in my view controller.m
//Button action
- (IBAction)importAudioClip:(id)sender
{
flag = 0;
customActionSheet = [[UIActionSheet alloc]initWithTitle:#"Select Audio/Video from:" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"YouTube",#"Vimeo", nil];
[customActionSheet showInView:self.view];
}
#pragma ActionSheet Delegate Methods
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"CLiekc button is %i",buttonIndex);
if([actionSheet.title isEqualToString:#"Select Audio/Video from:"])
{
if (buttonIndex == 0)
{
videoStatus=0;
webView.hidden = NO;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.youtube.com"]]];
NSLog(#"Taking from Youtube");
}
else if (buttonIndex == 1)
{
videoStatus=1;
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:#"Vimeo" message:#"Please enter Vimeo Username" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alertView.tag=123;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==123)
{
if (buttonIndex==1)
{
templateText=[[UITextField alloc]init];
templateText = [alertView textFieldAtIndex:0];
templateText.autocapitalizationType=UITextAutocapitalizationTypeWords;
templateText.delegate=self;
if ([templateText.text length]!=0)
{
NSString *str=[templateText.text capitalizedString];
NSLog(#"Str== %#",str);
[self getVimeoDetails:str];
}
}
}
}
-(void)getVimeoDetails:(NSString*)userName
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://vimeo.com/api/v2/%#/videos.json",userName]]];
[request setHTTPMethod:#"GET"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(#"The value is==%#",resSrt);
vimeoDetailsArray =(NSArray*) [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];
NSLog(#"jsonObject== %i",[vimeoDetailsArray count]);
NSString *theReply = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding: NSASCIIStringEncoding];
NSLog(#"the reply == %#",theReply);
if(response)
{
if (vimeoDetailsArray==NULL)
{
NSLog(#"its Null");
NSLog(#"null response== %#",response);
// UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert!!!" message:[NSString stringWithFormat:#"%#",theReply] delegate:self
// cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
// [alert show];
// [self performSelector:#selector(showAlertMessage:theReply withTitle:#"Alert!!!") withObject:nil afterDelay:5];
vimeoVideoTable.hidden=YES;
[self showAlertMessage:[NSString stringWithFormat:#"%#",theReply] withTitle:#"Alert!!!"];
}
else
{
[self createTableView];
vimeoVideoTable.hidden=NO;
[vimeoVideoTable reloadData];
NSLog(#"got response== %#",response);
}
}
else
{
NSLog(#"faield to connect");
}
if ([responseData length] == 0 && err == nil)
{
NSLog(#"Nothing was downloaded.");
}
else if (err != nil){
if ([[err description] rangeOfString:#"The Internet connection appears to be offline"].location != NSNotFound)
{
NSLog(#"string does not contain ");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert!!!" message:#"Please Check your Internet Connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
NSLog(#"Error = %#", err);
}
}
-(void)createTableView
{
if (vimeoVideoTable !=nil)
{
[vimeoVideoTable removeFromSuperview];
vimeoVideoTable=nil;
}
vimeoVideoTable=[[UITableView alloc]initWithFrame:CGRectMake(10, 20, 300, self.view.bounds.size.height-100)];
[vimeoVideoTable setDelegate:self];
[vimeoVideoTable setHidden:YES];
[vimeoVideoTable setDataSource:self];
[self.view addSubview:vimeoVideoTable];
//[vimeoVideoTable reloadData];
}
I definitely suggest youtube_ios_player_helper, "sponsored by" Google.
import youtube_ios_player_helper
class YTViewController: YTPlayerViewDelegate {
#IBOutlet weak var player: YTPlayerView!
// MARK: - Public properties -
var videoID = "k_okcNVZqqI"
// MARK: - View life cycle -
override func viewDidLoad() {
super.viewDidLoad()
self.player.delegate = self
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.load()
self.player.fadeOut()
}
/**
Play video with the relative youtube identifier.
*/
func load() {
Utils.showHUD(self.view)
let options = ["playsinline" : 1]
self.player.loadWithVideoId(self.videoID, playerVars: options)
}
/**
Stop video playing.
*/
func stop() {
}
// MARK: - YOUTUBE video player delegate -
func playerViewDidBecomeReady(playerView: YTPlayerView) {
self.player.playVideo()
}
func playerView(playerView: YTPlayerView, didChangeToState state: YTPlayerState) {
switch state {
case .Playing:
self.player.fadeIn(duration: 0.5)
Utils.hideHUD(self.view)
print("Started playback")
break;
case .Paused:
print("Paused playback")
break;
default:
break;
}
}
}
Use this
NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ;
[html appendString:#"<html><head>"];
[html appendString:#"<style type=\"text/css\">"];
[html appendString:#"body {"];
[html appendString:#"background-color: transparent;"];
[html appendString:#"color: white;"];
[html appendString:#"}"];
[html appendString:#"</style>"];
[html appendString:#"</head><body style=\"margin:0\">"];
[html appendString:#"<iframe src=\"//player.vimeo.com/video/84403700?autoplay=1&loop=1\" width=\"1024\" height=\"768\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>"];
[html appendString:#"</body></html>"];
[viewWeb loadHTMLString:html baseURL:urlMovie];
I used the following method to solve the same problem successfully.
Use youtube-ios-player-helper and add the code:
[_playerView loadWithVideoId:#"DmTzboEqfNk" playerVars:#{
#"playsinline" : #1
}];
You can find more vars in Youtube > IFrame API.
I have used WKWebView in storyboard with constraints and added code :
func playVideo() {
if yourLink.lowercased().contains("youtu.be"){
getVideo(videoCode: yourVideoCode)
if let range = yourLink.range(of: "be/"){
let videoId = yourLink[range.upperBound...].trimmingCharacters(in: .whitespaces)
getVideo(videoCode: videoId)
}
} else if yourLink.lowercased().contains("youtube.com"){
if let range = yourLink.range(of: "?v="){
let videoId = yourLink[range.upperBound...].trimmingCharacters(in: .whitespaces)
getVideo(videoCode: videoId)
}
} else if yourLink.lowercased().contains("vimeo.com") {
let url: NSURL = NSURL(string: yourLink)
webKitView.contentMode = UIViewContentMode.scaleAspectFit
webKitView.load(URLRequest(url: url as URL))
}
}
func getVideo(videoCode: String) {
guard
let url = URL(string: "https://www.youtube.com/embed/\(videoCode)")
else { return }
webKitView.load(URLRequest(url: url))
}
To get videoId from youtube/Vimeo links, please refer to :
Youtube Video Id from URL - Swift3
Regex to get vimeo video id in swift 2
Hope will help! :)

Release memory/cookie/cache from UIWebView once closed

I have a UIViewController which contains a UIWebView.
i login to a website for say facebook using the UIWebview in the UIViewController then i click on done, which dismiss the view controller, which i suppose will release the view controller. but when i open the UIWebview again (without exiting the app and also even after terminating the app) the webpage is still log into Facebook after the web view loads. How should i release the web view so that every time i click on done and return back to the web view, the web view will always be like "brand new" not log-on to any website that i had previously log-on to.
- (void)viewDidLoad{
NSLog(#"webView viewDidLoad called");
appDelegate = (LoginDBAppDelegate *)[[UIApplication sharedApplication] delegate];
loginObj = [appDelegate.loginArray objectAtIndex:currentSelectedRow];
myWebView.delegate = self;
[super viewDidLoad];
[self showWebView];
}
-(void)showWebView{
NSLog(#"webView showWebView called");
NSURL *url = [NSURL URLWithString:loginObj.loginURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}
-(IBAction)done_Clicked:(id)sender{
NSLog(#"webView Done_Clicked");
//dismiss the controller
[self.navigationController dismissModalViewControllerAnimated:YES];
}
-(void)dealloc{
[myWebView release];
myWebView.delegate = nil;
[activity release];
[urlTextField release];
[super dealloc];
}
I have tried this but it didn't work:
-(IBAction)done_Clicked:(id)sender{
NSLog(#"webView Done_Clicked");
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[myWebView stringByEvaluatingJavaScriptFromString:#"var body=document.getElementsByTagName('body')[0];body.style.backgroundColor=(body.style.backgroundColor=='')?'white':'';"];
[myWebView stringByEvaluatingJavaScriptFromString:#"document.open();document.close()"];
//dismiss the controller
[self.navigationController dismissModalViewControllerAnimated:YES];
}
I have also tried this without any success:
- (void) viewDidDisappear:(BOOL)animated {
NSLog(#"webView viewDidDisappear called");
[super viewDidDisappear:animated];
[self.myWebView removeFromSuperview];
self.myWebView.delegate = nil;
self.myWebView = nil;
[myWebView release];
}
Got the answer from someone on the forum, so credit to him...
Place this code in viewDidLoad
//Set Cache
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
//Clear All Cookies
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
//if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}

Open links in Safari instead of UIWebVIew?

I have an app with a UIWebView inside a UIViewController. I load HTML from a web service as a string like this:
self.webView loadHTMLString:_string baseURL:nil
Is it possible for the HTML links in this string to be opened in the browser and not in the UIWebView in my app? How can I do this?
I have tried this in the UIViewController that "hosts" the UIWebVIew:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
It doesn't seem to be working....
Any ideas?
Have you set the delegate of the UIWebView to your UIViewController? There's nothing obviously wrong with your code as far as I can see, so it's likely to be something small like that.
Add this in class..
#interface yourViewController : UIViewController
<UIWebViewDelegate>
Add this in View did load
- (void)viewDidLoad
{
[description loadHTMLString:string baseURL:nil];
description.delegate = self;
}
Add this in your .m file
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Note:
UIWebView *description;
#synthesize description;
Then It will work perfectly the way you deserve..!! :)
Set the delegate of the UIWebView to your UIViewController after that use this UIWebview method and check the condition, for example now webview current url is google.com, if suppose you clicked on gmail the url contains the string with gmail. Use the below method it opened a safari browser and automatically loaded that url.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
NSURL *url = [inRequest URL];
if ([[url absoluteString] rangeOfString:#"gmail"].location == NSNotFound) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}
If you already setup properly the UIWebViewDelegate, simply doing
self.webView loadHTMLString:_string baseURL:nil
self.webView.delegate = self;
should work
Add this line (
self.webview.delegate = self;
)
For Example in viewController.m
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlString baseURL:nil];
self.webview.delegate = self;
Apple introduced a Safari View Controller on iOS 9. Safari View Controller brings all of the features user expect from Safari over to your app without ever leaving it.
First we need to import Safari Services
#import <SafariServices/SafariServices.h>
For Objective C:-
NSString *yourUrl = #"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];
For Swift:-
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)
Yes, in your hyperlink tag add
target="blank"
And one question mark will be fine, thank you

Resources