I have a wkwebview on hybrid App.
When loaded first page, I'd like to open another wkwebview page.
So, I made blow code, but, it doesn't work.
How to open new one?
== PermissionViewController.h ==
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
#interface PermissionViewController : UIViewController < WKUIDelegate, WKNavigationDelegate >
#property(nonatomic) WKWebView *webView2;
+ (id) sharedManager;
- (void) openWKWebView;
#end
== PermissionViewController.m ==
#import "PermissionViewController.h"
#implementation PermissionViewController
+(id) sharedManager{
static PermissionViewController *permissionView = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{permissionView = [[self alloc] init];});
return permissionView;
}
...
...
...
-(void)openWKWebView{
NSLog(#"PermissionViewController callWKWebView start");
WKWebView *mainview = [[WKWebView alloc] initWithFrame:self.view.frame];
[mainview setUIDelegate:self];
[mainview setNavigationDelegate:self];
[self.view addSubview:mainview];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"mydomain/login/agree.jsp"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[mainview loadRequest:request];
NSLog(#"PermissionViewController callWKWebView end");
}
== console.log ==
16:24:58 MobileE onAppForeground start
16:24:58 MobileE call PermissionView start
16:24:58 MobileE PermissionViewController callWKWebView start
16:24:58 MobileE PermissionViewController loadView start
16:24:58 MobileE PermissionViewController viewDidLoad start
16:24:58 MobileE PermissionViewController loadView start
16:24:58 MobileE PermissionViewController viewDidLoad start
16:24:58 MobileE PermissionViewController callWKWebView end
16:24:58 MobileE onAppForeground end
Related
In iOS 10, AVPlayerLooper was added to loop videos in an AVQueuePlayer. I've viewed the official Apple documentation, but it is not clear at all - on top of that, the sample project provided is only in swift. I want to know how to run AVPlayerLooper in objective-c. Here is the Apple Documentation for reference: https://developer.apple.com/reference/avfoundation/avplayerlooper?language=objc
My end goal is to use this as part of an SKVideoNode subclass. Here is the code for the subclass using the AVPlayerLooper:
#import "SDLoopingVideoNode.h"
#import <AVFoundation/AVFoundation.h>
#interface SDLoopingVideoNode()
#property AVQueuePlayer *avQueuePlayer;
#property AVPlayerLooper *playerLooper;
#end
#implementation SDLoopingVideoNode
-(instancetype)initWithPathToResource:(NSString *)path withFiletype: (NSString *)filetype
{
if(self == [super init])
{
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:path ofType:filetype];
NSURL *videoURL = [NSURL fileURLWithPath:resourcePath];
AVAsset *videoAsset = [AVAsset assetWithURL:videoURL];
AVPlayerItem * videoItem = [AVPlayerItem playerItemWithAsset:videoAsset];
self.avQueuePlayer = [[AVQueuePlayer alloc] initWithItems:#[videoItem]];
self.playerLooper =[AVPlayerLooper playerLooperWithPlayer:self.avQueuePlayer templateItem:videoItem];
self = (SDLoopingVideoNode*)[[SKVideoNode alloc] initWithAVPlayer: self.avQueuePlayer];
return self;
}
return nil;
}
#end
And then to initialize the object, I use this code in the "didMoveToView" method:
SDLoopingVideoNode *videoNode = [[SDLoopingVideoNode alloc]initWithPathToResource:#"147406" withFiletype:#"mp4"];
[videoNode setSize:CGSizeMake(self.size.width, self.size.height)];
[videoNode setAnchorPoint:CGPointMake(0.5, 0.5)];
[videoNode setPosition:CGPointMake(0, 0)];
[self addChild:videoNode];
[videoNode play];
Trying to stop the webview from bouncing, when user drags the app/screen down there is a blank space appears on top. This is the code which is not working:
#import "Webview.h"
#interface Webview ()
#end
#implementation Webview
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlAddress= [[NSBundle mainBundle] pathForResource:#"demos/index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath: urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.scrollView.delegate = self;
webView.scrollView.bounces = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//The webview is is scrolling
int yPosition = [[webView stringByEvaluatingJavaScriptFromString: #"scrollY"] intValue];
if([[webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"] intValue] - yPosition == webView.frame.size.height)
{
//The user scrolled to the bottom of the webview
webView.scrollView.bounces = YES;
}else if([[webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"] intValue] - yPosition > webView.frame.size.height + 100){
webView.scrollView.bounces = NO;
}
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
NSLog(#"Finish");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
Having message: Assigning to 'id _Nullable' from omcompatible type 'Webview *const __strong'
If that error is on the line:
_webView.scrollView.delegate = self;
...then the error is that your webView instance is not the correct type.
Check that your webView instance is a subclass of UIWebView.
You should be adopting the UIScrollViewDelegate protocol, by including a protocol declaration for the interface: #interface ViewController : UIViewController <UIScrollViewDelegate>
I've been through all the posts in StackOverflow but I can't get a full example of how to get a video from the bundle and reproduce it in a controller.
I've tried many things but my screen appeared blank or black.
In the last amend, my video remains like this:
no error is thrown on the log. This is my code:
.h (please ignore the name of the controller, it will be finally a camera)
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#interface NativeCameraViewController : UIViewController
#end
.m
#import "NativeCameraViewController.h"
#interface NativeCameraViewController ()
#property (nonatomic, retain) AVPlayerViewController *avPlayerViewcontroller;
#end
#implementation NativeCameraViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = self.view;
NSString *stringPath = [[NSBundle mainBundle]pathForResource:#"video" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:stringPath];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:fileURL];
self.avPlayerViewcontroller = playerViewController;
[self resizePlayerToViewSize];
[view addSubview:playerViewController.view];
view.autoresizesSubviews = TRUE;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) resizePlayerToViewSize
{
CGRect frame = self.view.frame;
NSLog(#"frame size %d, %d", (int)frame.size.width, (int)frame.size.height);
self.avPlayerViewcontroller.view.frame = frame;
}
#end
and that's what I've got so far.
My video is correctly on the Bundle, called video.mov:
I've tried both linking and copying. Same result.
What am I doing wrong??
Thank you very much in advance.
I'd like to avoid using MPMoviePlayerController since it's deprecated in iOS9
It was actually my fault...
I was trying to reproduce an alias.
When you have 20.000 videos and 30.000 resources this things happen.
Instead of using AVPlayer, use MPMoviePlayerController class:
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];
I want to reload a WebView whenever a certain menu item is clicked. Here's my code that's not working. (Assume normal declarations in .h files.)
// AppDelegate.m
#import "AppDelegate.h"
// #import "ViewController.h" (in AppDelegate.h)
#interface AppDelegate ()
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
// Menu Item triggers this:
- (IBAction)reloadWebView:(id)sender {
NSLog(#"AppDelegate reloadWebView");
ViewController * vc = [[ViewController alloc] init];
[vc reloadWebView:sender]; // tried this,
[vc.webview reload:sender]; // and this,
[[vc.webview mainFrame] reload]; // and this,
[[vc.webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // and this.
}
#end
// ViewController.m
#import "ViewController.h"
// #property (assign) IBOutlet WebView *webview; (in ViewController.h)
#implementation ViewController
#synthesize webview;
- (void)viewDidLoad {
[super viewDidLoad];
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // this works
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
// Called in AppDelegate:
- (void)reloadWebView:(id)sender {
NSLog(#"ViewController reloadWebView");
// The problem here seems to be: webview == nil
[webview reload:sender]; // also tried this,
[[webview mainFrame] reload]; // and this,
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/"]]]; // and this.
}
#end
This is all my code. I've just started working on this project.
I've tried using a button in the ViewController. That works, but I need this to be a menu item.
Thanks in advance!
instead of creating a new view controller:
ViewController * vc = [[ViewController alloc] init];
try:
ViewController * vc = (ViewController *)[[[NSApplication sharedApplication] mainWindow] contentViewController];
to call the current view controller
I've started a tiny project to get my head around NSViewControllers.
I have an AppController that handles a NSOpenPanel. Once I get a URL to a movie file, I pass it to a NSViewController subclass (NNMovieViewController). This is how I do it:
-(void)openMovieWithURL:(NSURL *)url {
NSError *error;
movie = [[QTMovie alloc] initWithURL:url error:&error];
[startButton setEnabled:YES];
[movieView setMovie:movie];
NSLog(#"button: %#", [startButton isEnabled]?#"YES":#"NO");
// logs "NO"
NSLog(#"movie: %#", movie);
// logs the correct movie object
NSLog(#"movieView: %#", [movieView movie]);
// logs "(null)"
}
The header file looks like this:
#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>
#interface NNMovieViewController : NSViewController {
QTMovie *movie;
BOOL playing;
IBOutlet QTMovieView *movieView;
IBOutlet NSButton *startButton;
}
-(IBAction)clickStart:(id)sender;
-(void)openMovieWithURL:(NSURL*)url;
#end
What am I missing? I re-did the whole thing in a project without a NSViewController and it just worked...
UPDATE
After I received the comments from Kreiri and Parag Bafna I tinkered a little bit more and found out that at the time I call [movieViewController openMovieWithURL:url]; inside my AppController the Outlets are not hooked up yet.
This is my AppController implementation:
#import "AppController.h"
#implementation AppController
#synthesize movieViewController;
- (void)awakeFromNib {
movieViewController = [[NNMovieViewController alloc] initWithNibName:#"NNMovieViewController" bundle:nil];
NSView *viewControllerView = [movieViewController view];
[view addSubview:viewControllerView];
}
- (IBAction)clickOpen:(id)sender {
NSOpenPanel *dialog = [NSOpenPanel openPanel];
[dialog setCanChooseFiles:TRUE];
[dialog setCanChooseDirectories:FALSE];
[dialog setAllowsMultipleSelection:FALSE];
[dialog setAllowedFileTypes:[QTMovie movieFileTypes:0]];
if ([dialog runModal] == NSOKButton) {
NSURL *movieFileURL = [[dialog URLs] objectAtIndex:0];
[self openMovie:movieFileURL];
}
}
- (void)openMovie:(NSURL *)url {
NSLog(#"startButton: %#", [movieViewController movieView]);
// logs "null"
NSLog(#"startButton: %#", [movieViewController startButton]);
// logs "null"
NSLog(#"---------------------------------");
[movieViewController openMovieWithURL:url];
}
#end
Yes, silly me. In Interface Builder I hooked up my controls with the wrong object. I should have used File's Owner but instead I dragged in an NSObject and set its class to NNMovieViewController and connected the widgets to it.