Skip to content

Commit

Permalink
fix(ios): add user agent and method support
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifanyuan authored and hippy-actions[bot] committed Oct 31, 2023
1 parent ca3807a commit 6c7e5fa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
NS_ASSUME_NONNULL_BEGIN

@interface NativeRenderSimpleWebView : WKWebView <WKUIDelegate, WKNavigationDelegate>
@property (nonatomic, strong) NSString *userAgent;
@property (nonatomic, strong) NSString *url;
@property (nonatomic, strong) NSDictionary *source;
@property (nonatomic, copy) HippyDirectEventBlock onLoadStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,35 @@ - (void)setSource:(NSDictionary *)source {
_source = source;
if (source && [source[@"uri"] isKindOfClass:[NSString class]]) {
NSString *urlString = source[@"uri"];
[self loadUrl:urlString];
NSString *method = source[@"method"];

// Wait for other properties to be updated
dispatch_async(dispatch_get_main_queue(), ^{
[self loadUrl:urlString withMethod:method];
});
}
}

- (void)loadUrl:(NSString *)urlString {
- (void)loadUrl:(NSString *)urlString withMethod:(NSString*)method {
_url = urlString;
NSURL *url = HippyURLWithString(urlString, NULL);
if (!url) {
return;
}
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
method = [method uppercaseString];
if([method isEqualToString:@"GET"]){
request.HTTPMethod = @"GET";
}else if ([method isEqualToString:@"POST"]){
request.HTTPMethod = @"POST";
}else{
// System default is 'GET' no need to be specified explicitly
}
NSString* ua = self.userAgent;
if(ua){
self.customUserAgent = ua;
}
[self loadRequest:request];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ @implementation NativeRenderSimpleWebViewManager

HIPPY_EXPORT_MODULE(WebView)

HIPPY_EXPORT_VIEW_PROPERTY(userAgent, NSString)
HIPPY_EXPORT_VIEW_PROPERTY(source, NSDictionary)
HIPPY_EXPORT_VIEW_PROPERTY(onLoadStart, HippyDirectEventBlock)
HIPPY_EXPORT_VIEW_PROPERTY(onLoadEnd, HippyDirectEventBlock)
Expand Down

0 comments on commit 6c7e5fa

Please sign in to comment.