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.
Related
I am facing app extension close issues , please tell me if anyone know what wrong am doing.I am using action extension after preform some action inside extension i need to return response back.
Sample Code
// With Success Case
- (void) completeActionWithItems: (NSString *) response {
NSExtensionItem *extensionItem = [[NSExtensionItem alloc] init];
extensionItem.attachments = #[[[NSItemProvider alloc] response typeIdentifier: (NSString *)kUTTypePlainText]];
[self.extensionContext completeRequestReturningItems: #[extensionItem] completionHandler: nil];
}
// With Error Case
- (void) completeActionWithError: (NSError *) error {
[self.extensionContext cancelRequestWithError: error];
}
With Success Case working fine but some time is not closing,
With Error Case not working above code.
Please let me know what went wrong.Thanks
When you create an action extension, this is the default method which will close the Action Extension View Controller:
- (IBAction)done {
// Return any edited content to the host app.
// This template doesn't do anything, so we just echo the passed in items.
[self.extensionContext completeRequestReturningItems:self.extensionContext.inputItems completionHandler:nil];
}
Since this method is already provided, you should just try calling it from your success method.
// With Success Case
- (void) completeActionWithItems: (NSString *) response {
NSExtensionItem *extensionItem = [[NSExtensionItem alloc] init];
extensionItem.attachments = #[[[NSItemProvider alloc] response typeIdentifier: (NSString *)kUTTypePlainText]];
[self.extensionContext completeRequestReturningItems: #[extensionItem] completionHandler: nil];
// Call to "done" method
[self done];
}
I am implementing the CCAvenue gate way in my application.
In which CCAvenue gateway is loaded in web view from my web site.
Now after Transaction is completed with Success/Failur page,How can i came to know and load my apps success page....
Please help me Friends
The easiest way would be for your web page to send a request with a custom scheme for example myapp://transactionSuccess and your app to handle this in the webView delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"myapp"]) {
// handle the response
NSString *host = [URL host];
if ([host isEqualToString:#"transactionSuccess"]) {
// show your custom screen
return NO;
}
}
return YES;
}
Dan Spag is correct - URL Schemes make things a lot easier to manage and understand but sometimes these are just not possible. An alternative would be to ensure in the success / failure web page that in the dom (or HTML body response), the state of response is set and then capture that natively in iOS using stringByEvaluatingJavaScriptFromString like this...
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *responseState = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
// or possibly a javascript property in the webview's loaded page if preferred.
if([responseState isEqualToString:#"success"])
{
// do something with success
}
else if([responseState isEqualToString:#"fail"]) {
// do something with fail
}
[self.webView removeFromSuperView];
}
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];
when I try to share on twitter I do the following steps
if (!twitterEngine)
{
twitterEngine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
twitterEngine.consumerKey = kOAuthConsumerKey;
twitterEngine.consumerSecret = kOAuthConsumerSecret;
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: twitterEngine delegate: self];
if (controller)
[currentController presentModalViewController: controller animated: YES];
else
[self postTwitterStatus];
}
else {
[self postTwitterStatus];
}
after I got the twitter login view and after I put my pass and user and click add app
I get crash on asembly code, which I don't have an error message, but I saw something weiered also that in the delegates
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(#"Authenicated for %#", username);
[self postTwitterStatus];
}
I got that authintication succeeded but I get that the username == null
how can I solve this issue
Thanks
I did NOT get THIS error, but a similar one using Twitter+OAuth by Ben Gottlieb. If you are using this thing, it is required to use https and not http to connect to twitter.
go to: SA_OAuthTwitterEngine.m and change to:
- (SA_OAuthTwitterEngine *) initOAuthWithDelegate: (NSObject *) delegate {
if (self = (id) [super initWithDelegate: delegate]) {
//CHANGED FROM 'HTTP' TO 'HTTPS':
self.requestTokenURL = [NSURL URLWithString: #"https://twitter.com/oauth/request_token"];
self.accessTokenURL = [NSURL URLWithString: #"https://twitter.com/oauth/access_token"];
self.authorizeURL = [NSURL URLWithString: #"https://twitter.com/oauth/authorize"];
}
return self;
}
give it a try, i'm not sure it's the same as your problem..
Every thing looks fine to me. check with this example and see weather you are getting username in this.
I m passing an url as string from UIviewController to another uiviewcontroller..i was able to pass the url successfully but not able to load the string in the webview...in console i m getting a null value in webview could u guys help me out below is the code...
-(void)playAction1
{
webviewcontroller *newEnterNameController = [[webviewcontroller alloc] initWithItem:#"http://www.theappcodeblog.com/?p=222"];
[self.navigationController pushViewController:newEnterNameController animated:YES];
[newEnterNameController release];
}
- (id)initWithItem:(NSString *)url
{
if (self = [super initWithNibName:#"webviewcontroller" bundle:nil])
{
self.title=#"facebook";
self.url1 = [NSURL URLWithString:url];
//URL Requst Object
self.requestObj1 = [NSURLRequest requestWithURL:url1];
NSURLConnection *connection=[[[NSURLConnection alloc] initWithRequest:self.requestObj1 delegate:self]autorelease];
[self.webViewAnnouncements loadRequest:self.requestObj1];
NSLog(#"webView:%#",webViewAnnouncements);
[self.webViewAnnouncements setDelegate:self];
}
return self;
}
While what you're doing doesn't look wrong to me (in truth I'd need to see .h and .xib files) as well to be sure. I would consider the initWithItem to be an unusual pattern.
If I was you, I would init the view controller in the "normal" way using initWithNib and then create the URL as a property type and set it before you present the view controller to the screen.