Skip to content

close() method for closing the browser programmatically #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RCTBrowser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#import "RCTBridgeModule.h"
#import "RCTConvert.h"

#import "TOWebViewController.h"

@interface RCTBrowser : NSObject <RCTBridgeModule>

@property (nonatomic) TOWebViewController *browser;

@end
15 changes: 15 additions & 0 deletions RCTBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ @implementation RCTBrowser

RCT_EXPORT_METHOD(presentUrl:(NSString *)url withOptions:(NSDictionary *)options) {

if ([[options objectForKey:@"deleteAllCookies"] isEqual: @(YES)]) {
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}

TOWebViewController *webVC = [[TOWebViewController alloc] initWithURLString:url];

[options enumerateKeysAndObjectsUsingBlock:^(NSString* key, id obj, BOOL *stop) {
Expand Down Expand Up @@ -65,6 +73,13 @@ @implementation RCTBrowser
dispatch_async(dispatch_get_main_queue(), ^{
[rootVC presentViewController:nav animated:YES completion: nil];
});

self.browser = webVC;
}

RCT_EXPORT_METHOD(close) {
UIViewController *rootVC = [[UIApplication sharedApplication] keyWindow].rootViewController;
[rootVC dismissViewControllerAnimated:YES completion:nil];
}

@end
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# react-native-browser
A full-featured web browser module for React Native apps, based on the awesome [TOWebViewController](https://github.com/TimOliver/TOWebViewController)

***Added a close() method to close the browser programatically***

![TOWebViewController](screenshot.jpg)

### Installation
Expand Down
3 changes: 3 additions & 0 deletions index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var RCTBrowserExport = {
open: function(url, options={}) {
Browser.presentUrl(url, options);
},
close: function (){
Browser.close();
}
};

module.exports = RCTBrowserExport;