Hi friends,
I am getting data from server when user post a link on his timeline am displaying like this but my requirement is when i click Title then it will open browser. How can i do this?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.stackoverflow.com"]];
Will open a URL by the Application defined for the scheme. http has Safari as default (I guess)
To open it in the App itself create a ViewController + xib,
add a UIWebView to your View
& Buttons to get back to the app.
Then you can create an instance of your new ViewController and present it with
[self presentViewController:WebVC animated:YES completion:^(void){
[[WebVC MyWebView] loadRequest: [NSMutableURLRequest requestWithURL:[NSURL URLWithString: #"http://stackoverflow.com"]];
}];
e.g.
To make it even easier you could add a function to your ViewController like this one
-(void) LoadUrlFromString: (NSString *)url{
[self.MyWebView loadRequest: [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]]];
}
and then just do as before but call
[WebVC LoadUrlFromString:#"http://stackoverflow.com"];
on completition
You can use TTTAttributedlabel
TTTAttributedlabel *title_lbl = [TTTAttributedlabel alloc]init] //Make it an instance of TTTAttributed label what you are using for displaying the Title.
NSRange range = [title_lbl.text rangeOfString:"YOUR_TILE_HERE (Latest bollywod and hollywood.....")"];
[title_lbl addLinkToURL:[NSURL URLWithString:#"YOUR_URL_HERE (www.bollywood.com)"] withRange:range];
title_lbl.delegate = self;
And for onlcick method
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
NSlog("%#",label.text);
[UIApplication sharedApplication] openURL:url];
}
Related
I want to create custom back button on each page in my iOS web-view (objective-C). Can anyone please suggest me how to implement.
Thanks in Advance
I am new to iOS Web-view that is why I posted directly what I want this is my code :
#import "ViewController.h"
#interface ViewController () <UIWebViewDelegate>
#end
#implementation ViewController{
__weak IBOutlet UIImageView *logoImage;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.delegate = self;
NSString *urlString = #"https://www.anything.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
self.webView.hidden = YES;
if ([_webView canGoBack]) {
[_webView goBack];
}}
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.webView.hidden = NO;
logoImage.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"%#", error);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am passing a url of my website and I want to show a back button on every page of my application so that user can go to previous page. I have one more issue that whenever I minimise my application and opens it back from recent apps it get restarted while I want it to be resumed from the same screen
For the UIWebView, check the method - (void)goBack; out.
For the WKWebView, check the method - (WKNavigation *)goBack; and - (WKNavigation *)goBack:(id)sender; out.
UPDATE
// init the back button
[btnBack addTarget:self action:#selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- (void)backButtonTapped:(id)sender
{
[self.webview goBack];
}
Assume you have set up your webview and back button (in the navigation bar or somewhere else) in the full code.
The browser could go back when the current (tab) page has a history list and the current page url is not the first and last one.
Make sure you really want to goBack in viewDidLoad, it needs some time to finishing loading the target page you specified (https://www.anything.com) just like you do it in a browser.
The method loadRequest of webview is asynchronous, it pushes the url request to webview and returns it immediately.
Bind the webview's goBack method to your custom back button like the above code snippets.
Try to click a sample link in the initial page (https://www.anything.com), once the new page finishes loading, it could be able to goBack with satisfying [self.webview canGoBack].
Make a breakpoint in the implementation of method backButtonTapped, if needed.
Drop the logoImage or other unrelated code issues in your next question, that makes your question more clear and helpful.
iOS 8.1, XCode 6.1, Storyboards and 2 UIViewControllers that look like this:
The view on the left is the main view, the view on the right is a UIWebView that will show help information written in HTML. My problem is that when I tap on the blue circle with an 'i' in it, it is supposed to go to the 2nd view controller (which it does) and display the html for that language (which it doesn't do)... all I get is a black screen! Here is my code to display the html file contents:
-(void) viewWillAppear:(BOOL)animated {
NSURL *indexURL;
NSString *sysLangCode = [[NSLocale preferredLanguages] objectAtIndex:0];
// do we support this language?
if([sysLangCode isEqualToString:#"en"] || [sysLangCode isEqualToString:#"de"] || [sysLangCode isEqualToString:#"it"] ||
[sysLangCode isEqualToString:#"es"] || [sysLangCode isEqualToString:#"fr"] || [sysLangCode isEqualToString:#"ja"] ||
[sysLangCode isEqualToString:#"zh-Hant"] ) {
indexURL = [[NSBundle mainBundle] URLForResource: [NSString stringWithFormat:#"instRST-%#", sysLangCode]
withExtension:#"html"]; // contatenate the language code to the filename
}
else
indexURL = [[NSBundle mainBundle] URLForResource: #"instRST-en" withExtension:#"html"]; // make 'en' the default
NSLog(#"\n\nmainBundle: %#",[NSBundle mainBundle]);
NSURLRequest *request = [NSURLRequest requestWithURL: indexURL];
NSLog(#"\n\nhtmlURL: %#",indexURL);
[self.webView loadRequest:request];
}
- (void)viewDidLoad {
[super viewDidLoad];
[webView setDelegate:self];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Failed to load with error :%#",[error debugDescription]);
}
UPDATE This is the connection for the webView:
I have tried different scenarios for hours now, and have given up doing this on my own. Can someone please tell me what I'm doing wrong?
Since you are using a storyboard you must use a Segue to present your ViewController. Delete your button action implementation and Control+Drag from your button onto your Help view controller. Link it to the Show segue.
i also face the same issue, below webview property helps me to avoid black screen problem
[self.web setOpaque:NO];
What I have done so far:I use the code below to change User-Agent in UIWebView in the project.
NSDictionary *userAgentReplacement = [[NSDictionary alloc] initWithObjectsAndKeys:userAgent, #"UserAgent",nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userAgentReplacement];
Problem:I have this code working, however when I call the function again, to change the UserAgent to something else, UIWebView's do not update. In order to successfully update the UserAgent I need to remove/release the UIWebView and alloc/show it again, but it causes the UIWebView to lose all history. Is there any way to update the UserAgent without having to create a new UIWebView in order to have it take effect?
As you're using NSUserDefaults I assume that you're setting them in
application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
The alternative that I've previously used is
NSMutableURLRequest*request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:yourURL]];
[request setValue:userAgent forHTTPHeaderField:#"User-Agent"];
[webView loadRequest:request];
You can also use the UIWebView Delegate to capture the last loaded state of the webview using a NSString variable
-(void)webViewDidFinishLoad:(UIWebView *)webView{
currentConfig=[webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML];
}
And then reload the webView using
[webView loadHTMLString:currentConfig baseURL:yourURL];
In my window controller, I have this:
- (void)windowDidLoad {
[super windowDidLoad];
NSLog(#"webView = %#", webView);
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://www.apple.com/"]]];
}
My understanding is that should make the webView in my window now download and render Apple's home page. But all I see in a white panel. What am I missing?
(I've checked that webView is not null; NSLog shows something like
"webView = <WebView: 0x10be7c3b0>")
I've also tried the more modern approach, but still had no success:
[webView setMainFrameURL:#"http://www.apple.com/"];
As this project is sandboxed, the solution in this case was to add the relevant entitlement to the project.
I wonderd if there is any way to make the user dismiss the WebView window if he doesn't want it anymore on the screen...?
I looked at this post but i didn't understand it well.
How to cancel a UIWebView?
can anyone give me an example please?
This is the code i have:
CGSize webScreen1;
webScreen1 = [[UIScreen mainScreen] applicationFrame].size;
CGRect webFrame1 = CGRectMake((webScreen1.width/11.0) ,(webScreen1.height/19.0) ,webScreen1.width/1.2,webScreen1.height/1.25);
defaultWebView.frame = webFrame1;
self.defaultWebView = [[UIWebView alloc] initWithFrame:webFrame1];
self.defaultWebView.backgroundColor = [UIColor whiteColor];
self.defaultWebView.scalesPageToFit = YES;
self.defaultWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
defaultWebView.inputView.hidden = YES;
[self.view addSubview: self.defaultWebView];
[self.defaultWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
[NSString stringWithFormat:(NSString *)#"%#", #"http://www.google.com"]]]];
Thanks!
There is no such thing as cancel a webView, what you need to do is remove the UIWebView from the parent view. If your UIWebView is a subview of self.view then you can provide a button named Close which behaves like this -
- (IBAction)closeWebView:(id)sender
{
[self.webView removeFromSuperView];
self.webView = nil;
return;
}
This should remove the webview from your view.
...way to make the user dismiss the WebView window...
[self.defaultWebView removeFromSuperview];
look I will explain what happenes in the post you attached above and you will understand the technique,
the UIWebview is a component which views an web page, it may be html or other types,
there is one way to hide your webview is to add an action in the html of the webview and ovveride the request in the code of your app,
when you click on link or ahref or any action on UIwebview there is a delegate method which automatically runs before continuing with the request
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
you can add a button or link on the html page and ovveride the request on this method
for example
Thing to click
and in the delegate method
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] isEqualToString:#"http://hideWebView/"]) {
[self.webview setHidden:YES];
return NO;
}
return YES;
}
here you ovveride the request http://hideWebView/ and in the delegate you searched for this request and then hiddes the web view or anything else you want to do