I added MSTextView in my project. It is working fine in every manner except some of the links e.g. http://www.t-mobileadvantagedirect.com/L.aspx?d=Vb4UseqIl9QYojIAqfjNqw==. I am not having any idea of link regex. Please help me...I finished my whole app, only this issue is bothering me..
In line 190 of MSTextView.m, return the following instead:
return #"(\\bhttps?:\\/\\/[-A-Z0-9+&##\\/%?=~_|!:,.;]*[-A-Z0-9+&##\\/%=~_|])";
You also need to specify the NSRegularExpressionCaseInsensitive option when creating the NSRegularExpression object.
Also, be a good person, fork the repository, make the changes in your fork and submit a pull request to the original author.
Source of the regular expression: https://stackoverflow.com/a/8943487/350272
I've no idea with the MSTextView, but I can help you! I believe that you've all URLs like in question, and it's not showing up with the textview, right?
What you've to do is,
Show normal url inside your text with MSTextView, something like,
Check out my website, here's the link, http://www.t-mobileadvantagedirect.com
okay, now in, MSTextViewdelegate,
You've to check for the URL tapped,
- (void) handleURL:(NSURL*)url
{
if([url.absoluteString isEqualToString:#"http://www.t-mobileadvantagedirect.com"])
{
WebViewController *webview = [[WebViewController alloc] initWithURL:[NSURL urlWithString:#"http://www.t-mobileadvantagedirect.com/L.aspx?d=Vb4UseqIl9QYojIAqfjNqw=="]];
[self.navigationController pushViewController:webview animated:YES];
[webview release];
}
}
I know this isn't a solution if you've the same domain url for multiple paths!
Related
Posting after finding answer
After "rubber duck debugging" this answer a bunch, I finally came across the correct answer on a question that appears to me to be unrelated. I think this question (and its answer) are still relevant, so I'm posting the question and will post my own answer to hopefully help others like me.
I am creating a PDF in my iOS app that I would like to allow the user to export. For the purposes of this testing, I'm trying to save it to my personal Dropbox on a physical device.
I have turned on iTunes file sharing, and I can verify that the PDF file is being generated correctly, and when I copy it off of my device (iPad Pro Gen. 2 running iOS 11), I can open the PDF and it has the expected content and appearance.
I am able to get the document pop-up to display correctly, and I have options to share via:
Line 1: AirDrop
Line 2: Message, Mail, Add to Notes, (Facebook) Messenger, etc.
Line 3: Copy, Print, Save to Files, Save to Dropbox, etc.
No matter what I try to select (Save to Dropbox is the one I want to solve, but the issue seems universal), it fails. Of note, when I click Save to Dropbox, I do see the Dropbox panel display, but there is immediately a modal over top of the Save to Dropbox modal that says, "An unknown error occurred."
I have tried to look around and see how to get more information about this error, but I'm stumped. I'm not sure if it's correlated, but I get this message in the console:
[AXRun-PID] Client requesting unsuspension of PID:813 Name:<redacted>
Trying to google that error has proved unfruitful.
Here's the code where I generate the PDF and show the menu:
#pragma mark • Sharing Methods
- (void)showShareMenu {
NSArray *bookList = [BookManager bookList];
NSURL *pdfUrl = [PdfGenerator generatePdfFromBooks:bookList];
UIDocumentInteractionController *vc = [[UIDocumentInteractionController alloc] init];
vc.name = #"Booklet.pdf";
vc.URL = pdfUrl;
vc.UTI = #"com.adobe.pdf";
[vc presentOptionsMenuFromBarButtonItem:self.navigationItem.leftBarButtonItem animated:YES];
}
I've tried using UIDocumentInteractionController *vc = [UIDocumentInteractionController interactionControllerWithURL:pdfUrl]; instead of the one above, but the results are the same.
I tried making self the delegate of vc and then tried to implement the following methods:
- (void)documentInteractionController:(UIDocumentInteractionController *)controller
willBeginSendingToApplication:(nullable NSString *)application;
- (void)documentInteractionController:(UIDocumentInteractionController *)controller
didEndSendingToApplication:(nullable NSString *)application;
Neither of those methods ever fired.
Interestingly, though I think I've supplied the file name correctly based on what I've read, the name in the File textbook in the Save to Dropbox modal is a current timestamp (e.g., File Oct 28, 11 12 22 PM). The Dropbox modal stays up until I click "OK" on the "An unknown error occurred" modal, and then disappears immediately.
It seems like I'm somehow not providing the right information, but I'm not sure how. It seems like there ought to be a delegate method to indicate an error to me, but I don't see anything like that in the docs. (It is late, and I have been looking at this for hours, including reading several related tutorials, so I could have missed something obvious.)
I came across this answer as an example question while asking this current question.
It doesn't really ask the same question I have, nor did that user have the same error outputs I did. But, the linked answer did work for me, too.
The problem I had in the code above was that I was not keeping the UIDocumentInteractionController around after I created it. Adding a private property fixed this issue. So, the following code now works:
#pragma mark • Sharing Methods
- (void)showShareMenu {
NSArray *bookList = [BookManager bookList];
NSURL *pdfUrl = [PdfGenerator generatePdfFromBooks:bookList];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:pdfUrl];
self.docController.name = #"Booklet.pdf";
self.docController.UTI = #"com.adobe.pdf";
[self.docController presentOptionsMenuFromBarButtonItem:self.navigationItem.leftBarButtonItem animated:YES];
}
i'm trying to open an app in appstore from an UIAlertView When the otherButtonTitle. The problem is nothing is happening.
I've checked when the otherButton is pressed if this method is called and it is. The problem lies in the openURL i guess. the appId contain an name of an app without any whiteSpace like "youtube". How come the link is not opening? i've tried looking in other threads and this is the code that should open an app in appstore
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex: (NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
NSString *appId = [[otherArray objectAtIndex:currentRow] objectForKey:#"link"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: #"itms-apps://itunes.apple.com/app/%#?mt=8", appId]]];
}
}
You need to break it down into smaller steps and debug the steps.
First, is the didDismiss method being called.
Next, is the code inside the if statement being executed.
The easiest way to figure this out is to set a breakpoint on the code that assigns appID and then make sure your code breaks there.
After that line executes, make sure appId contains the string you expect it to.
Net, you should rewrite the last line into steps that first create the URL, THEN call openURL. Step through the code that creates the URL to make sure it is being created correctly.
You might want to copy the URL string and paste it into a browser to see if it works from there.
I'm trying to show a UIWebView with a GIF in it, but only once the GIF has loaded.
I load the GIF as follows:
self.GIFWebView = [[UIWebView alloc] init];
self.GIFWebView.delegate = self;
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
[self.GIFWebView loadHTMLString:html baseURL:nil];
Where post is just an object with some properties such as the URL for the GIF.
Then in webViewDidFinishLoad: I show the web view:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"%f", webView.scrollView.frame.size.width);
NSLog(#"%#", [webView.request.URL absoluteString]);
}
I get "0" and "about:blank" for the NSLogs each time, however.
Why does it not load the GIF properly?
I get "0" and "about:blank" for the NSLogs each time, however.
Not surprising. You're telling the web view to load HTML that you're providing in a string rather than giving it a request. The URL that you're logging is the request URL, and since there's no request, there's no request URL.
Why does it not load the GIF properly?
Possibly because you're misusing the URL object. Look at the code:
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
We can't tell what type post.url is, but it's probably NSURL*. You're probably passing a NSURL into the format string, and that may not produce the result you're looking for. Try passing in a string like [post.url absoluteString] instead of the actual NSURL object.
Also, you might want to log the value of html right after you create it so that you can check the full HTML that you're sending to the web view.
Update: Some additional things to check:
Are you running the code in question on the main thread?
Is the thread's run loop getting time?
Have you tried setting a non-nil base URL?
If the web view's delegate has a -webView:shouldStartLoadWithRequest: method, does it return YES?
What happens if you use a constant string that includes the HTML you want and the hard-coded URL instead of constructing the string with +stringWithFormat:?
Is the test device connected to the network? (Sometimes it's the simplest thing that gets you.)
Does it work correctly if you use a different image? I notice that the URL you're using is for an animated .gif file, try a non-animated .gif or a .jpg image instead.
Update 2: The problem lies in your creation of the web view. Look at the very first line in the code that you showed:
self.GIFWebView = [[UIWebView alloc] init];
That looks okay for a typical object, but -init is not the designated initializer for a view. You should use -initWithFrame: instead. The image loads fine in your sample project when I change the code in your project to use the right initializer:
UIWebView *GIFWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
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.
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.