I am working with Sencha Touch and PhoneGap. The code is for iOS and it's waiting for url with suffix #phonegap-external ..
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ( ([url fragment] != NULL) && ([[url fragment] rangeOfString:#"phonegap=external"].location != NSNotFound))
{
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
}
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
So because I haven't written any line of code in Obj-C, I need your help. Can someone edit code, so that it would open url without suffix.
EDIT:
If user opens url in app, it would open it inside webview but on occasion I would prefer that url is opened in Safari. So this code checks if url has suffix like this - http://google.com#phonegap-external and than it opens it in Safari. Only thing what bugs me, is url is not changed into http://google.com and it opens given url http://google.com/#phonegap-external. Could someone please fix this.
If you're sure that the part of the URL that indicates whether it's to be opened inline or externally (i. e. the #phonegap-external string) is always the last one in the URL, then you can try removing this suffix by writing something like as follows:
NSString *orig = [url absoluteString];
size_t frLen = [#"phonegap-external" length];
NSString *stripped = [orig substringToIndex:orig.length - frLen];
NSURL *newURL = [NSURL URLWithString:stripped];
[[UIApplication sharedApplication] openURL:newURL];
Related
I wrote a simple Mac OS application. The view only contains a hidden WebView and a NSTextView. At a certain time 100 URLs will be parsed, one after another. A URL is loaded in the WebView, some DOM tree parsing happens, rinse and repeat.
Everything works fine except there are some major differences in loading times when the computer is in idle (no user activity). When i am using the computer a URL takes about 2 seconds to load. When i am not using the computer for some time, a URL takes about 1 minute to load.
When i come back to the computer after some time and see in the TextView that the last URLs took about a minute, the next ones immediately will be loaded in seconds. So it has to be something with the user activity on the system. Right? Anybody knows why?
I am on OS X 10.9.4.
Here is the code how i load the URLs:
#pragma mark -
- (void)processNextUrl
{
// No url, stop
if (_processQueue.count == 0)
return;
// Process url
NSString *urlString = [_processQueue objectAtIndex:0];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[_webView mainFrame] loadRequest:request];
}
#pragma mark - WebFrameLoadDelegate
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
// Page ready for parsing?
if ([[sender mainFrame] isEqual:frame] == NO)
return;
// Append date, time and url to TextView
NSString *url = [[[[frame dataSource] request] URL] absoluteString];
[self logText:url];
// Parser does some DOM parsing and outputs to TextView
Parser *parser = [[Parser alloc] initWithWebFrame:frame delegate:self];
[parser parse];
}
#pragma mark - ParserDelegate Methods
- (void)parser:(Parser *)parser didFinishParsingUrl:(NSString *)url
{
[_processQueue removeObjectAtIndex:0];
[self processNextUrl];
}
I'd bet AppNap is kicking in and starving your process of resources.
You may want to either disable AppNap in the Finder's Get Info window of your app - a temporary test to see if I'm right of course - or just use NSProcessInfo's beginActivityWithOptions:reason: to tell the system that the activity was user initiated and the app should not be throttled.
So I'm in the progress of developing an app but I'm kinda stuck at the moment. I have 2 textfields right now that the user will have to enter:
Ordernr:xxxxxxxx
Referencenr:xxxxxxx
these 2 values have to be added to this link:
http://www.artis-web.de/cgi-bin/WebObjects/Artis.woa/wa/detailedTrack?referencenr=xxxxxxxx&ordernr=xxxxxxxxxx
now I want safari to open this link. The ideal situation would be the user entering the 2 textfields values and then pressing a button called "open in safari"
What would be the best way to implement this?
Just add the following code to your button action:
NSString *urlString = [NSString stringWithFormat:#"http://www.artis-web.de/cgi-bin/WebObjects/Artis.woa/wa/detailedTrack?referencenr=%#&ordernr=%#", referenceNrTextField.text, orderNrTextField.text];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlString]];
To create the URL you can use...
NSURL *url = [NSURL urlWithString:[NSString stringWithFormat:#"http://www.artis-web.de/cgi-bin/WebObjects/Artis.woa/wa/detailedTrack?referencenr=%#&ordernr=%#", referenceTextField.text, ordernoTextField.text]];
To open in safari...
[[UIApplication sharedApplication] openURL:url];
Check whether the both fields have data and then hit the url with the given data.This condition would be sufficient.
if (Ordernr.text.length!=0 && Referencenr.text.length!=0)
{
NSLog(#"Hit the Url with the entered data");
}
else
{
NSLog (#"Show Error Alert Message");
}
I have an animal list and special button. When I press the button, I would like to go to Wikipedia and read about this animal more. So I wrote this code:
-(IBAction)goWiki:(id)sender
{
NSString *wikiUrl = "http://ru.wikipedia.org/wiki/";
NSString *url = [NSString stringWithFormat:#"%#%#",wikiUrl,animalTitle];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
NSLog(#"%#",url);
}
NSLog shows that url was written correctly, however, nothing happened. I am 99,9% sure its because of animalTitle. My native language is russian and animalTitle is also an animal name in russian.
So if link is like http://ru.wikipedia.org/wiki/Frog its fine and it works but if its like
http://ru.wikipedia.org/wiki/Лягушка nothing happens.
Any ideas, how can I move to a russian article?
Thanks!
use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding as follows -
-(IBAction)goWiki:(id)sender
{
NSString *wikiUrl = #"http://ru.wikipedia.org/wiki/";
NSString *url = [NSString stringWithFormat:#"%#%#",wikiUrl,animalTitle];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
NSLog(#"%#",url);
}
Try passing the string animalTitle through CFURLCreateStringByAddingPercentEscapes first.
I am using the below code to load a url into safari (iphone):
NSString *gotoURL = [self getPostID:indexPath.row];
//NSLog(gotoURL );
NSURL *url = [ [ NSURL alloc ] initWithString:gotoURL];
[[UIApplication sharedApplication] openURL:url];
the code works if I replace gotoURL with : #"http://www.example.com" but it wont if I replace it.
The gotoURL gets a url from getPostID (which also returns a NSString) and I did an nslog as you can see and the url format seems fine.
Any idea why its not working?
From what you've added, it is likely that you're missing the protocol in the url. The string passed must be http://domain.tld/4Mi.
I have a programatically crated UIWebView, and it is used to browse a iPhone-style site stored on my server. In this website, there are a few links to files users can download into my application. Right now, I'm trying to detect this with:
- (BOOL) webView:(UIWebView *) webView shouldStartLoadWithRequest:(NSURLRequest *) request navigationType:(UIWebViewNavigationType) navigationType
{
url = [request URL];
NSString *mimeType = [request valueForHTTPHeaderField:#"Content-Type"];
NSLog(#"Content-type: %#", mimeType);
if(mimeType == #"application/zip" || mimeType == #"application/x-zip" || mimeType == #"application/octet-stream")
{
NSLog(#"Downloading file!");
[NSThread detachNewThreadSelector:#selector(download:) toTarget:self withObject:#"/tmp/file.ipa"];
return NO;
}
return YES;
}
However, when this method is called, the content-type header is almost always (null), so I never am able to download a file.
How would you do this correctly?
You're trying to detect a Content-Type from an NSURLRequest which has not yet been made. You won't know the Content-Type until after the request is made using NSURLConnection. In this case, I'd probably just look at the file extension of the URL path.
----------Swift 4+-------
Example for audio/mp3 detect -
Step 1: Use delegate
class ViewController : WKUIDelegate,WKNavigationDelegate {
Step 2: Setting WebKit
func setWebView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
let myURL = URL(string: "https://www.bossmobi.guru/files/download/type/320/id/197255")//your audio url
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
Step 3: Get audio MIME type from webkit delegate.
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Void) {
print( #function + "url is \(String(describing: webView.url))" + "Mimetype" + "\(navigationResponse.response.mimeType ?? "NotAvailable")")
if let _ = navigationResponse.response.mimeType?.range(of: "audio/mpeg") {
print("MP3 is audio url \(String(describing: webView.url))")
webView.stopLoading()
}
decisionHandler(.allow)
}
---------ObjC----------
WKWebView setup
NSString *urlString = #"https://www.bossmobi.guru/files/download/type/320/id/197255";
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *_demoWKWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
_demoWKWebView.navigationDelegate = self;
_demoWKWebView.UIDelegate = self;
NSURL *nsurl=[NSURL URLWithString:urlString];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[_demoWKWebView loadRequest:nsrequest];
[self.view addSubview:_demoWKWebView];
WKWebView delegate
-(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
//NSLog(#"decidePolicyForNavigation---Response %#",webView.URL);
if ([navigationResponse.response.MIMEType isEqualToString:#"audio/mpeg"]) {
NSLog(#"MP3 audio url is %#",webView.URL);
}
decisionHandler(WKNavigationResponsePolicyAllow);
}
So here's the problem: UIWebView doesn't download anything it can't display, and it doesn't know how to display a ZIP file. It will always fail before the Content-Type is filled in.
So, what to do? I don't know if your server-side app runs on more than the iPhone, but you could register a custom URL scheme with links like myapplication://example.com/stuff/yourhexurlgoeshere. You can create a custom URL handler for the myapplication scheme. A couple of seconds of Googling produced this site, which explains how to do it.
This has an additional benefit because if you, say, emailed such a link to another user, they could tap on it in Mail and have it open in your application.