How to implement multiple tabs like in safari? - ios

I am currently creating a google-cast app for iPhone. Its all working like I wanted, I can cast a video on GoogleCast. My only problem is that if the website user visiting has popup ads, the webview automatically redirects to that ad site (sometimes multiple redirects at once).
Since UIWebView doesnt have adBlock function, I thought maybe I should implement multiple tabs feature, so that the ad can be opened in a new tab and the user can close the ad tab manually.
Thanks in advance.
Has any of you have experience with this?

first of you need to use WKWebView instead of UIWebView, now to your point about the tabs, there isnt actually an already made component that will handle this for you but what you can do is intercept the requests from the webview's delegate (WKNavigationDelegate for WKWebView and UIWebViewDelegate for UIWebView) and whenever you feel that a request needs to be open in a separate webview then you block that request in the current webview and create a new view with a new webview nested in it and a close button and whatever else you feel a 'tab' needs to have and execute that request (the one you blocked in the original webview) in there

In decidePolicyForNavigationAction delegate method you can declare a new WKWebView class and add it as a subview to previous WKWebView class based on navigationType or request. For example see below code snippet how am doing and which works fine to me.
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) { // OR if (![navigationAction.request.URL.Path isEqualToString:#"Previous URL Path"]) {
// Add cancel button at top of new tab
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(5, 5, 50, 20);
[btn setTitle:#"Close" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:#selector(close:) forControlEvents:UIControlEventTouchUpInside];
CGRect nFrm = oldWebVw.frame; // Set frame as per your requirement.
nFrm.origin.y = 100;
nFrm.size.height -=120;
newWebVw = [[WKWebView alloc] initWithFrame:nFrm];
newWebVw.backgroundColor = [UIColor blueColor];
[newWebVw addSubview:btn];
[oldWebVw addSubview:newWebVw];
[newWebVw loadRequest:navigationAction.request];
decisionHandler(WKNavigationActionPolicyCancel); // You must cancel the policy else the new request loades on previous WKWebView class
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}

here's an opensource ios browser that supported multiple tabs
Radium Browser

Related

FoxitIOSRDK: not able to click on Hyperlink

I am using FoxitRDK framework for opening the pdf document in my App. In the demo everything works fine apart from one thing: I am not able to click on hyperlink. I have go through the SDK document and the classes inside framework but can not able to fine the solution.
The document link is below:
http://www.foxitsdk.com/docs/mobile-pdf-sdk/developer_guide_ios.pdf
and here is my code
NSString* pdfPath = [[NSBundle mainBundle] pathForResource:#"getting_started_ios1" ofType:#"pdf"];
// Initialize a PDFDoc object with the path to the PDF file
FSPDFDoc* pdfdoc = [FSPDFDoc createFromFilePath:pdfPath];
// Initialize a FSPDFViewCtrl object with the size of the entire screen
pdfViewCtrl = [[FSPDFViewCtrl alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-60)];
[pdfViewCtrl registerDocEventListener:self];
[pdfViewCtrl registerPageEventListener:self];
[pdfViewCtrl registerGestureEventListener:self];
// Set the document to display
[pdfViewCtrl setDoc:pdfdoc];
// Add the pdfViewCtrl to the root view
[self.view addSubview:pdfViewCtrl];
extensionsManager= [[UIExtensionsManager alloc]initWithPDFViewControl:pdfViewCtrl];
pdfViewCtrl.extensionsManager = extensionsManager;
[extensionsManager registerAnnotEventListener:self];
[extensionsManager registerAnnotHandler:self];
//Search button
searchButton = [[UIButton alloc] initWithFrame:CGRectMake(280, 80, 80, 40)];
[searchButton setBackgroundColor:[UIColor grayColor]];
[searchButton setTitle: #"Search" forState: UIControlStateNormal];
[searchButton addTarget:self action:#selector(showSearchBar)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:searchButton];
How can I fix this?
You need to added UI extensions component to your app.
you can refer to the step in section"2.4.5 Add support for Text Search, Bookmark, and Annotation" of the developer guide.
After added the UI extensions to you project, and Initialize it, the link will be work.
related code are here:
"#import "../uiextensions/UIExtensionsManager.h"
UIExtensionsManager* extensionsManager;
...
extensionsManager = [[UIExtensionsManager alloc] initWithPDFViewControl:pdfViewCtrl];
pdfViewCtrl.extensionsManager = extensionsManager;"
If you want to Goto special page when a document open, you need to do it in the onDocOpened event.
(void)onDocOpened:(FSPDFDoc*)document error:(int)error
{
[_pdfViewCtrl gotoPage:2 animated:false];
}
Thanks Foxit support team after updating the version to 2.0 it works perfect.
I have got the solution.
Good luck Team.

iOS FBSDKLikeControl / FBSDKLikeButton cached like result?

I found out that the FBSDKLikeControl cached the like result.
When I click the Like control to like a page, I can verify from browser that the page is liked. The FBSDKLikeControl codes are as follow:
if(likeButton) {
[likeButton removeFromSuperview];
likeButton = nil;
}
likeButton = [[FBSDKLikeControl alloc] initWithFrame:CGRectMake(150, 150, 200, 50)];
likeButton.likeControlStyle = FBSDKLikeControlStyleStandard;
likeButton.likeControlHorizontalAlignment = FBSDKLikeControlHorizontalAlignmentCenter;
likeButton.objectID = #"https://www.facebook.com/FacebookDevelopers"; // FBID: 19292868552
likeButton.center = self.view.center;
[self.view addSubview:likeButton];
Then, I unlike the Page in browser and reload the view controller to make sure the like button is recreated, the Like Control still claims I liked the page.
How can I avoid the cache? The same situation happens in FBSDKLikeButton. Using FBSDKShareKit 4.8.0 via Cocoapods

UIButton retains the glows after it has been touched

Hi i am making customUIButtons in my app by using the following piece of code.
+ (NSArray *) createButtonItemNormalImage:(UIImage *)normalImage
highlightImage:(UIImage *)highlightImage
disabledImage:(UIImage *)disabledImage
touchUpSelector:(SEL)selector
target:(id)target
{
// HighlightImage is not used. Highlight is shown using iOS glow
UIButton *uiButton = [UIButton buttonWithType:UIButtonTypeCustom];
uiButton.bounds = CGRectMake(0,
0,
normalImage.size.width,
normalImage.size.height);
[uiButton setImage:normalImage
forState:UIControlStateNormal];
if (disabledImage)
{
[uiButton setImage:disabledImage
forState:UIControlStateDisabled];
}
[uiButton addTarget:target
action:selector
forControlEvents:UIControlEventTouchUpInside];
uiButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:uiButton];
return [NSArray arrayWithObjects:buttonItem, uiButton, nil];
}
I have made a cancel button using the above function. The cancel button takes the user from one screen to another screen. The problem is when i come back to the first screen the cancel button is still glowing. I have seen this problem before also but a call to [self.view setNeedsLayout] used to solve it.
Why does it happen and what would be a correct way of solving it?
Thanks!
To solve this problem in not so standard way I now set the highlighted state to no for all the buttons when I enter the first screen. I use myButton.highlighted = NO;. However the documentation says following for highlighted property.
Specify YES if the control is highlighted; otherwise NO. By default, a control is not highlighted. UIControl automatically sets and clears this state automatically when a touch enters and exits during tracking and when there is a touch up.
Its not happening so in my case.I would love to know the reason behind it and standard ways of solving it

Come back to app from an other app

Hello,
When we are phoning with us iphone and you left the call view to come back at springboard, we can come back to the call view with the status bar. (This picture can better explain : http://what-when-how.com/wp-content/uploads/2011/08/tmpB178_thumb.jpg)
Or, in the Facebook app when you go to the messenger Facebook app (not same app) we can touch status bar to comme back Facebook App too.
I would like to know if it's possible to make it in my app ? And if it's possible, how I will proceed ? (Edit: I want co come back in my app from another app such Youtube.)
Thanks
Once you become familiar with how to open another app within your current app form following link:
http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
You can simply create a view that has tap gesture and use it as a button
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
UIView *tapToReturnButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
tapToReturnButton.backgroundColor = [UIColor blueColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapToReturnButtonClicked)];
[tap setNumberOfTouchesRequired:1];
[tapToReturnButton addGestureRecognizer:tap];
UILabel *tapToReturnLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 20)];
tapToReturnLabel.text = #"Tap to return";
tapToReturnLabel.textColor = [UIColor whiteColor];
tapToReturnLabel.textAlignment = NSTextAlignmentCenter;
tapToReturnLabel.font = [UIFont fontWithName:#"ArialMT"
size:14];
[tapToReturnButton addSubview:tapToReturnLabel];
[self.view addSubview:tapToReturnButton];
}
- (void)tapToReturnButtonClicked
{
NSLog(#"Now you add your code that opens another app(URL) here");
}
Edited:
After I posted the code above I kind of realized that there will be no tap gesture on the status bar even though other bottom part (20 pixel) of tapToReturnButton has a click gesture. After I did some research, I think following link has the better solution on click gesture. I will probably use tapToReturnButton as placeholder to let users know where to touch though and remove UITapGestureRecognizer *tap.
How to detect touches in status bar
Again, I think there is multiple way to achieve your need but those links above will give you good starting point.
URL schemes will allow you to launch an app from an another app.
Check the following links for more info:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW18
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW50
here are some system supported URL schemes
https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899

UIButton doesn't respond to touches

In my app, I need to parse some data from the network, and add some customized buttons.
Later, when user click on it, I would provide more details.
An image view is the background for the app
The position of these buttons(xPos, yPos) are parsed from the server(dynamic data)
no prints when I click on these buttons that I add programmatically
The code I have for adding it is like this
...
[businessButton setImage:businessImage forState:UIControlStateNormal];
[businessButton setFrame:CGRectMake([xPos floatValue], [yPos floatValue], [businessImage size].width/2, [businessImage size].width/2)];
[businessButton addTarget:self.imageView action:#selector(serviceProviderSelected:) forControlEvents:UIControlEventTouchUpInside];
...
- (void)serviceProviderSelected:(id)sender
{
NSLog(#"sp tapped\n");
}
I created another dummy app to do (what I think is the same thing), and the button works out fine...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *customizedButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:#"business-icon.png"];
[customizedButton setImage:image forState:UIControlStateNormal];
[customizedButton setFrame:CGRectMake(100, 100, 20, 20)];
[customizedButton addTarget:self action:#selector(customButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customizedButton];
}
- (IBAction)buttonPressed:(id)sender {
NSLog(#"yes, pressed\n");
}
I've been suspecting that the buttons I create in code are already released, and printed out the array that I use for storing these Buttons, they are valid addresses though...
Thanks!
William
I just found out why.
Instead of [self.imageView addSubview:businessButton];, [self.view addSubview:businessButton]; works now.
I think I didn't really understand how the view relationship was after adding an imageView in storyboard.
UIImageView has user interactions disabled by default. you have to set this property to YES in order to get an UIButton or anything else working as it's subview
You need to do:
[businessButton addTarget:self action:#selector(serviceProviderSelected:) forControlEvents:UIControlEventTouchUpInside];
The target needs to be the class that defines the selector.

Resources