I am getting empty string when using StringByEvaluatingJavascriptFromString that is reading a javascript function. I am using a StringByEvalutaingJavascriptFromString in ShouldAutoRotateToInterfaceOrientation because i want to control supported orientations on specific pages.
It seems that i need to use it webViewDidFinishLoad.
But how to control supported orientations on specific pages using StringByEvalutaingJavascriptFromString.
Or how to use it in ShouldAutoRotateToInterfaceOrientation.
I followed this tutorial: link
and he is using it in ShouldAutoRotateToInterfaceOrientation and not in webViewDidFinishLoad
this is my code in MainViewController.m:
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
NSString *isFirstPage = [self.webView stringByEvaluatingJavaScriptFromString: #"isFirstPage();"];
NSString *truth = #"true";
if ([isFirstPage isEqualToString:truth]) {
return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
else {
return interfaceOrientation==UIInterfaceOrientationPortrait;
}
}
and isFirstPage is return a empty string #"".
function isFirstPage(){
return true;
}
Related
I've implemented a view controller which is displaying a WKWebView. The web view displays certain pages containing links with a tags, e. g.:
<a id="dien10000513721" href="/ge/tr/issue.html" class="dien">Issue</a>
I would like to add a preview to these links by activating 3d-Touch in the web view, but the delegate method webView:shouldPreviewElement: is never called, when I run my app on a iPhone 7 with iOS 11 and doing a force touch on that link.
When I've created my web view and set the necessary properties with
theWebView.navigationDelegate = self;
theWebView.UIDelegate = self;
theWebView.allowsLinkPreview = YES; // Setting this to NO doesn't help
self.webView = theWebView;
The three delegate methods for previewing are implemented as dummies:
- (BOOL)webView:(WKWebView *)inWebView shouldPreviewElement:(WKPreviewElementInfo *)inElementInfo {
NSLog(#"url = %#", inElementInfo.linkURL);
return NO;
}
- (UIViewController *)webView:(WKWebView *)inWebView previewingViewControllerForElement:(WKPreviewElementInfo *)inElementInfo defaultActions:(NSArray<id<WKPreviewActionItem>> *)inPreviewActions {
return nil;
}
- (void)webView:(WKWebView *)webView commitPreviewingViewController:(UIViewController *)previewingViewController {
NSLog(#"");
}
Do I still have to perform any important configuration steps? What else could prevent the execution of 3D touch events?
I am loading a url in UIWebView in iOS. the url contains a button. I want to call a objective C method, when i click on this button. Please help.
This is source code
NSString *strUrl = #"<html><body><button type=\"button\" id=\"success_id\" style=\"width:200px;margin:0px auto;text-align:center; background:#1B2F77;color:#fff;padding:10px;font-size:10px;border:0px;\" name=\"continue\" onclick=\"ok.performClick();\">Continue</button></body></html>";
[_tempWebView loadHTMLString:strUrl baseURL:nil];
I want to load this string to UIWebview and When I click on button , then I want to open my own viewcontroller.
You need to do three things:
Override shouldStartLoadWithRequest delegate method of UIWeBView.
shouldStartLoadWithRequest method returns a bool value so return false, then it will not load the link.
Inside this method write whatever functionality you want to perform.
If it is your page, communicate with app via JavaScript otherwise you can override shouldStartLoadWithRequest method and check if this specific request is called
some code:
- (BOOL)isItThatButtonEvent:(NSURLRequest *)req {
if([req.URL.absoluteString isEqualString:#"that.specific.url.or.some.other.way.to.check.that"]) {
return YES;
}
return NO;
}
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if([self isItThatButtonEvent:request]) {
[self methodThatOpensYourViewController];
return NO;
}
return YES;
}
I am new to Objective c Native coding in IOS. I am having two buttons. One to lock the app to portrait and another lock the landscape orientation. What should I do to achieve this. Locking in the sense it will fix to the locked side and will not turn.
Yes i have used a method to implement the same using ios plugin which i created in worklight(For hybrid apps).
The function for setting the flag is
#import "HelloworlPlugin.h"
#implementation HelloworlPlugin
int count=0;
-(void)sayhello:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)option
{
[arguments pop];
NSString *responseString=[NSString stringWithFormat:#"%#",[arguments objectAtIndex:0]];
if([responseString isEqualToString:#"1"])
{
count=1;
}
else
{
count=0;
}
NSLog(#"Zero %#",responseString);
}
-(int)count1
{
NSLog(#"count= %d",count);
if(count<1)
{
Cv=0;
}
else
{
Cv=1;
}
NSLog(#"%d",Cv);
return Cv;
}
#end
Where in say hello function i get the arguments from the hybrid app and based on that i set the flag variable Cv to 1 or 0.
Below is the default method which i have changed little bit to do my implementation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
HelloworlPlugin *object=[[HelloworlPlugin alloc] init];
int fValue=[object count1];
NSLog(#"%d",fValue);
if(fValue==1)
{
return (interfaceOrientation ==UIInterfaceOrientationPortrait);
}
else
{
return true;
}
}
shouldAutorotateToInterfaceOrientation
is a thing that is default function based on which the orientation is set returning true set all the orientation else by giving the approriate orientation we can set the answer.So here based on the cv value i stored in fValue i have locked the orientation.
For example this way:
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
I am using ZBarSDK for QR Code scanning feature. I want to use this only in PORTRAIT mode only. As per the documentation I set it up with below code line:
_reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);
As expected it works well with iOS 5 but with the same code this view changes orientation for iOS 6 & 7. Is supportedOrientationsMask only works with < iOS 6? Is there any other way to force this ZBar reader camera view to work only in Portrait mode? Thanks in advance
Here more details with Code:
if(_reader) // first check `_reader` is created or not?
{
[_reader.readerView stop]; // then stop continue scanning stream of "self.ZBarReaderVC"
for(UIView *subViews in _reader.view.subviews) // remove all subviews
[subViews removeFromSuperview];
[_reader.view removeFromSuperview];
_reader.view = nil;
}
_reader = [ZBarReaderViewController new];
_reader.readerDelegate = self;
_reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);
ZBarImageScanner *scanner = _reader.scanner;
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
[_reader.view setFrame:CGRectMake(0, _topbar.frame.size.height, self.view.bounds.size.width, self.view.bounds.size.height-_topbar.frame.size.height)];
_reader.cameraOverlayView = [self CommomOverlay];
_reader.showsZBarControls=NO;
// present and release the controller
[self presentModalViewController: _reader
animated: NO];
Let me know in case more details required.
Finally found the solution.
The problem was like this:
ZbarViewController *reader was presented from my current view controller and it's portrait support property was not working somehow.
_reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);
What i did to resolve this issue is I created TBZbarViewController the new class which was inheriting the ZbarViewController class and placed the below method.
-(BOOL)shouldAutorotate{
return NO;
}
Then I used the TBZbarViewController *reader to present from My controller which solved the issue and it's working in Portrait mode only as needed.
Thanks.
I did like this, and is working for all iOS versions :
Step 1 : Set your Device Orientation
Step 2 : Add this code into you implementation (.m) file.
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_6_0
- (BOOL) shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationMaskPortrait;
}
#endif
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
I have setup a class with the following:
MESSidePanelViewControllerSubClass
Header file
#property BOOL setLandscapeOK;
Imp file
- (NSInteger)supportedInterfaceOrientations {
// Restriction for the welcome page to only allow potrait orientation
if (setLandscapeOK == YES) {
NSLog(#"Reveal-setting all");
return UIInterfaceOrientationMaskAll;
}
NSLog(#"Reveal-setting portrait");
return UIInterfaceOrientationMaskPortrait;
}
I now want to update the value from another file (view controller).
LoginViewController
Other view controller imp file
- (NSInteger)supportedInterfaceOrientations {
// Restriction for the welcome page to only allow potrait orientation
NSLog(#"Login-supportInterfaceOrientations");
MESSidePanelViewControllerSubClass* setLandscapeOK = YES;
return UIInterfaceOrientationMaskAll;
}
I get an error:
Implicit conversion of 'BOOL' (aka 'signed char') to 'MESSidePanelViewControllerSubClass *' is disallowed with ARC
How should I be updated the BOOL value in another file?
This line is wrong:
MESSidePanelViewControllerSubClass* setLandscapeOK = YES;
It should be something like this: (Also mESSidePanelViewControllerSubClass is probably an iVar and instantiated somewhere outside your supportedInterfaceOrientations method.)
MESSidePanelViewControllerSubClass *mESSidePanelViewControllerSubClass = [MESSidePanelViewControllerSubClass alloc] init];
then:
mESSidePanelViewControllerSubClass.setLandscapeOK = YES;
Edited to add link to delegate.
Shouldn't -supportedInterfaceOrientations in MESSidePanelViewControllerSubClass always return UIInterfaceOrientationMaskPortrait? If an instance of MESSidePanelViewControllerSubClass is pushed as a child view controller, won't that force your UI orientation to be portrait? Did you try that?