Call alert view from website - ios

I'm looking for a possibility to invoke an alert view from a website.
I'm pretty sure that this works somehow because if go through configuring your Apple ID and stuff like that in the App Store you are navigating through webviews and not a native environment (prior to iOS 7!).
Apple uses alert view and action sheets and date picker there so there has to be a way to do so.
I wasn't able to find anything useful on the web nor in the docs.
Cheers
Constantin

To achieve this you can use redirects, set javascript onClick function to some DOM element.
f.e
javascript function callNativeAlert(message) {
window.location = "showAlert://"+message;
}
On UIWebView delegate's you can catch such redirect then show your UIAlertView and ignore loading this page by returning NO
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[request.URL scheme] isEqualToString:#"showAlert"]) {
//TODO: Show your alert here, or execute any native method
NSLog(#"The message is %#", [request.URL host])
...
// Always return NO not to allow `UIWebView` process such links
return NO;
}
....
}
Note: this code has been written from memory
Note2: of course it works if you can modify both javascript and native application

You can use simple JS function alert('message').

Related

Detecting UIImagePicker opened or not from a web view

In my project I am having a web view. The page loaded in the web view contains an file (Image) uploader. On clicking the choose image button on the file uploader, then the phone's image picker opens. I am not authorised to make changes in the web page, because it is client organisations web page. Is there any way to detect the opening of UIImagePicker in the app.
<input id=“fileUpload" name=“fileUpload" type="file" data-bind="value: UploadedFile">
This is the html used for the file picker. On clicking the file picker the UIImage Picker View pops up. I want to detect it.
In the web page, for ‘Choose Image’ Button write a Java Script function . iOS supports custom URL schemes provided with syntax below
scheme://host/path?query
Example Java script function in your .html :
<p class="btn-upload" onclick="onUploadButtonClick()" >
<span> Upload File </span>
</p>
<script>
function onUploadButtonClick() {
window.location = "ios://button-upload”;
}
</script>
In your class .m file, inside Webview delegate method webView:shouldStartLoadWithRequest:request:navigationType compare the host. if it matches invoke your objective c function to open UIImagePicker in the app.
#pragma mark - UIWebView Delegates
- (void) webViewDidFinishLoad:(UIWebView *)webView {
//Do after web view load.
}
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType;
{
// get the action from the path
NSString *actionType = request.URL.host;
if ([actionType isEqualToString:#“button-upload”]) {
// do something in response to your javascript action
// if you used an action parameters dict, deserialize and inspect it here
[self openImagePicker]; //Cal your method to open UIImagePicker
}
// make sure to return NO so that your webview doesn't try to load your made-up URL
return NO;
}
Bonus : Make sure you have added App Transport Security Key in your info.plist.

moving data from webview into ios app

I am not sure of how I would achieve the following functionality and was looking for suggestions. I have an Objective-C iOS app that loads a WebView. The user has a way to download data within the webview running a JS application. When the user triggers this download, I would like to use the downloaded data and execute a method call in the native code.
Looks like you have to use some kind of Hybrid calls. Once Javascript downloads the file, you should invoke a native app call (with a special schema say iosbridgecall:// from Javascript) and check this schema in shouldStartLoadWithRequest,
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] scheme] isEqualToString:#"iosbridgecall"]) {
//here invoke your Native Method, ex:
processDownloadedData();
return NO;
} else {
return YES;
}
}

Detect when url containing local anchor tag is clicked on UIWebView?

I want to detect when a url with local anchor tag is clicked on html page which is current been rendered on UIWebView. I have done lots of searches around and implemented various workarounds but no success. The ultimate suggestion from all around always comes to this delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
The biggest problem what I concluded was that this method simply doesn't get called for local anchor tags if already in a rendered HTML page. What i mean is if fresh new page URL (anchor tag) included is clicked that method gets fired. Here is an example. If i my UIWebView has already rendered www.xyz.com, now if i press some url which is like www.xyz.com#amazing (link present on that same page) then delegate method doesn't get fired.
I was developing a simple in-app browser. I had to show URL of every page been rendered/visible at address box of my in-app browser. Only obstacle now is i cannot show every anchor tags/javascript links that just brings new content but same URL domain.
There were solutions regarding usage of this
`[webView stringByEvaluatingJavaScriptFromString:#"window.location.hash"]`
Problem is that until and unless if I can't get delegate method called/fired than there is no benefit of this code.
Is there any workaround or shall I just give up on this? I hope I am clear with explaining the problem.
Note: Best TestCase Scenario is mobile version pinterest.com on UIWebView
Thanks.
I don't exactly follow what you're trying to do, perhaps you could clarify a bit more.
In the meantime, here's how to get your UIWebView to handle every link click in the web page it has loaded. It's looking for a URL Scheme that begins with "bloodwing". If it finds it, it will stop the request. Otherwise, it will work as expected.
So http://www.yahoo.com will work, and bloodwing://www.yahoo.com will fail.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
if ([url.scheme isEqualToString: #"bloodwing"]) {
[webView stringByEvaluatingJavaScriptFromString: #"doSomething();"];
return NO;
} else {
return YES; // Return YES to make sure regular navigation works as expected.
}
}
Hopefully this helps!

Bool web view function not working as planned

Basically, what I want is a function that executes this function [self.navigationController popViewControllerAnimated:YES]; when a certain button is pressed and if you are on a Google page. But the function happens even if I am not on a Google page. Is there something I might be missing?
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if(wasClicked)
{
if([[request.URL absoluteString] rangeOfString:#"google"].location!=NSNotFound)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
return YES;
}
You should check within this method if button is clicked with UIWebViewNavigationTypeFormSubmitted or link is clicked UIWebViewNavigationTypeLinkClicked,
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
}
you should at least read the documentation of the UIWebViewDelegate before posting the question for every single step. I am writing this because in the previous question you asked about how it works, you got answer in other post and you copied the answer and pasted as a question here instead of trying it yourself.
If you're on a Google page, there's a strong likelihood that most links will initiate a load request for a URI that contains "google" somewhere in the string. I'm not sure of this, but I think even Google search results first load a Google URL for tracking before redirecting to the actual page clicked.
Try adding NSLog(#"URL: %#", [request.URL absoluteString]); to your function and see why that statement is evaluating to YES.

Make an affiliate link to the iTunes store without redirects?

Apple has explained in "Launching the App Store from an iPhone application" how one can make an affiliate link to the app store and handle the redirect in background so it doesn't annoy the user. But it would be even better not to have the redirect at all. I seem to remember seeing a way to do that, but now I can't find it anywhere.
Is it possible to make an affiliate link from an iOS app to the app store without any redirect at all?
EDIT: To clarify, I am talking about a Linkshare affiliate link.
EDIT 2: I'm getting closer. I have this link, which I grabbed straight off of linkshare's "text links" page. When using k1th's trick below, works without any redirects on the iPad, but still has one redirect on an iPod touch [and presumably iPhone]. I speculate that the redirect may be to switch from top iPad apps to top iPhone apps, but I don't know that for sure.
http://click.linksynergy.com/fs-bin/click?id=sf2bW7QX/qU&offerid=146261.10005745&type=3&subid=0
Yes, you may have slashes in the params (that's because it's a slash after the question mark starting the parameter part of the URL.
Regarding skipping Mobile Safari to process the affiliate links:
You can either set up a hidden UIWebView to handle the redirect or do all that in the URL loading system yourself.
This is with a hidden WebView:
NSURLRequest *r = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://click.linksynergy.com/fs-bin/click?id=sf2bW7QX/qU&offerid=146261.431296703&type=2&subid=0"]];
testWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
testWebView.hidden = YES;
testWebView.delegate = self;
[testWebView loadRequest:r];
the delegate:
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.scheme isEqualToString:#"itms"] &&
[[UIApplication sharedApplication] canOpenURL:request.URL]) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES; // go on redirecting
}
testWebView needs to be an instance var and the view controller itself needs to be a <UIWebViewDelegate>. You also need to set the webview delegate to nil somewhere (e.g. in -dealloc)
I take it the reason you don't want redirects is to
avoid the Safari browser from popping up
avoid redirection within the App Store app itself
I would prefer k1th's solution, but failing that (I suppose it could fail #2 above), I assume the problem is that the first itms link is not the "final" one. One solution would be to simply hard-code the URL (or provide it by some other means):
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL myAppUrl = ....
if ([request.URL.scheme isEqualToString:#"itms"] &&
[[UIApplication sharedApplication] canOpenURL:myAppURL]) {
[[UIApplication sharedApplication] openURL:myAppURL];
return NO;
}
return YES; // go on redirecting
}
A cleaner solution would be to read the app ID off the itms link in the request.URL and format a new URL using the pattern that will take you directly to your app.
There is a much cleaner solution directly from Apple here: https://developer.apple.com/library/ios/#qa/qa1629/_index.html
And for brevity, here is the code from that page:
// Process a LinkShare/TradeDoubler/DGM URL to something iPhone can handle
- (void)openReferralURL:(NSURL *)referralURL {
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:referralURL] delegate:self startImmediately:YES];
[conn release];
}
// Save the most recent URL in case multiple redirects occur
// "iTunesURL" is an NSURL property in your class declaration
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
self.iTunesURL = [response URL];
return request;
}
// No more redirects; use the last URL saved
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[UIApplication sharedApplication] openURL:self.iTunesURL];
}
I found this document and I think it's giving me the answer. I always have a hard time deciphering these things, but I think it says that what I do is start with a basic link:
http://itunes.apple.com
then append a partnerId of 30, plus my linkshare affiliate token, which I think is
sf2bW7QX/qU
to end up with the following:
http://itunes.apple.com?partnerId=30&id=sf2bW7QX/qU
I got what I think is my id by following the instructions in the Apple doc, which basically say to grab the id parameter from a random linkshare link. The link I used for the purpose was this:
<IMG border=0 src="http://a464.phobos.apple.com/us/r30/Music/b9/7f/91/mzi.kbjyfypr.170x170-75.jpg" ><IMG border=0 width=1 height=1 src="http://ad.linksynergy.com/fs-bin/show?id=sf2bW7QX/qU&bids=146261.431296703&type=2&subid=0" >
I'm still quite unsure about the whole thing. Can my linkshare affiliate token really have a slash in it?
This answers your question:
http://www.apple.com/itunes/affiliates/resources/documentation/linking-to-the-itunes-music-store.html#apps
BTW, I find this whole affiliate program too complicated. I looked into it and I don't think it's worth the 5% commission.

Resources