I am new to xcode development so hang in there with my lack of knowledge. (BTW i've searched for this already with no luck).
I have a webview app I am working on, I do not want the users using the onsite menu because it's not optimal for mobile yet. So I want to offer a menu on the app that loads up various in-site url's. I have created a menu and added the back and refresh buttons so I have gotten that far but cannot figure out how to get other buttons i've created (example "categories") to load example.com/categories and hope someone can help me as this will save me much frustration. The app is a single webview so i'm not sure if that plays a role in this. Thank you in advance.
Let say that your buttons trig the same function and you have set their tag, you could do :
-(IBAction)loadButton:(id)sender
{
NSString *urlString;
switch(((UIButton*)sender).tag)
{
case 1:
urlString=#"http://www.exemple.com/categories";
break;
case 2:
//...
}
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
Related
I am new to iOS app development and I just tried using UIWebView to display a mobile website in my app and was not quite successful.
What I did is just some minimal Xcode project configuration and coding that I found during some Googling efforts:
Create a new iOS application project in Xcode using the Single View
Application template.
Drag a Web View from the Object Library to the View Controller
scene of Main.storyboard.
While holding down the Control key, drag the Web View from the View
Controller scene to the ViewController.h editor, resulting a
source code line like this:
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
Add the following code in ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:#"http://www.amazon.com"];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
This is all what I did (and also everything those tutorials told me). After I built and ran the app (on simulator and on real iphone device), the site did load in the app's view (and did load the mobile version instead of the PC version), but it displayed the site as if the view were much larger than the actual iphone screen. Here is a screeshot (Amazon in this case but basically the same for other sites):
What should I do to make the iOS UIWebView display a mobile website correctly?
Just tried Dipen Chudasama's suggestion of disabling the "User Size Classes" option, for the first time of all those suggestions, the result did change a little bit (from left-align to sort of center-align), but still not what I am looking for. Here is the screenshot:
EDIT:
Thanks for all the suggestions given by you enthusiastic people. Loading a site like amazon.com in a UIWebView should be a rather easy task as I understand, nevertheless, I didn't succeed with any of the suggestions.
It would be great if anyone could share with me (via Github or alike) just an sample xcode project (starting from scratch with the latest version of xcode tool chain) with nothing but a UIWebView that could load amazon.com correctly. That way I can do a line by line diff and may be able to find what I did wrong in my own project.
The UIWebView has a property called "scalesPageToFit":
self.myWebView.scalesPageToFit = YES;
self.myWebView.contentMode = UIViewContentModeScaleAspectFit;
should do the trick.
You can scale the page to fit the screen width with this UIWebview property webView.scalesPageToFit = YES;
Otherwise you can run a js command in the UIWebView to do the scale:
NSString *js = [NSString stringWithFormat:#"document.body.style.zoom = 0.8;"];
[webView stringByEvaluatingJavaScriptFromString:js];
I got it..you just set iPhone size view in storyboard instead of any size, means disable Use Size Classes Check box and see, this will work..:)
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.amazon.com"];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
Thats it, Try this one only.
OR
you have to set appropriate constraint for this.
I had the same issue as you #goodbyeera, and then realized I hadn't set the AutoLayout constraints on the UIWebView. Therefore, when the WebView loaded it was too big for the iphone screen. By adding the constraints to fit the UIWebView to the screen fixed it all. Hope this might help you.
Im trying to make an app for planning out where you want to put "shelves" on a wall (sounds strange i know ;) )
i have got it so the image is picked from the UIImagePickerController then loaded in a new ViewController i also have a button at the bottom of the ViewController that i want to open a list of images to select from that once selected the user can move around then stretch the required size if they don't like, delete and once they are happy i want my second button to save the image and put it into an email ready for sending.
I have looked at a few ways but none of them seem to be "adequate" would be so grateful anyone could suggest any ways or even help me with some code, I'm very new to this and this is my first app.
Im not too sure on the code you'd need to see as none of what is written yet is relevant to this part of the app
This is a 2 part so the first is, what way do i go about it?, the second is what code?
Thanks
-(UIImage *)GetStreachableImage
{
UIImage *takephotoButtonImagenormal = [UIImage imageNamed:CHOOSE_TAKE_NORMAL_IMAGE];
UIImage *strechableButtonImageNormal1 = [takephotoButtonImagenormal stretchableImageWithLeftCapWidth:takephotoButtonImagenormal.size.width/2 topCapHeight:takephotoButtonImagenormal.size.height/2];
takephotoButtonImagenormal=nil;
return strechableButtonImageNormal1;
}
try this
this looks very simple, but I just can't figure out what's going wrong. So what I want to do is I have a button on the 1st controller, and when the user clicks that button, I push another controller into the navigation stack and show a website in the webview.
code to push the webview controller:
SellerViewController *sellerController = [[SellerViewController alloc] initWithNibName:#"SellerViewController" bundle:nil];
[self.navigationController pushViewController:sellerController animated:YES];
code to load website
- (void)viewDidLoad
{
[super viewDidLoad];
[self.sellerWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.clickUrl]]];
}
I have a UIWebView in IB and I connect it to the outlet sellerWebView. I also checked the clickUrl, it is correct.The problem is when I click the button, the new controller shows up, but no content is loaded in the webview. Any suggestions? Thanks!
As others suggested, set a delegate and check for failure.
If your request does load and your issue is with the UIWebView's content, you can debug it, see this answer.
tl;dr:
If you're using iOS >= 6 and you have mountain lion (10.8) or Safari >= 6, you can just:
Open the application in the simulator (or your device in XCode >= 4.5.x).
Open Safari (go to Preferences -> Advanced and make sure "Show Develop Menu in Menubar" is on.
From the Menu-bar (of Safari) select Develop -> iPhone Simulator -> [your webview page].
That's it !
Edit:
Glad you found the issue. You could use Safari to catch errors having to do with invalid or wrong URLs.
One you have attached the Web Inspector to your web view, click "Inspect" on the top bar, then click "Timelines" and "Network Requests", and try to load your request.
Since you left out the http:// before the path, it would likely get prefixed with file:// instead -- you'd be able to see that it is requesting the wrong resource without guesswork.
So in a UIWebView, whenever you press anything, a grey selection box appears. Like this:
I know you can remove it if you have access to the source code of the website, but obviously in this example I'm using YouTube, which I cannot access. Can I remove the grey highlight within Xcode?
Many thanks!
Why not use JavaScript-injection?
[webView stringByEvaluatingJavaScriptFromString:
#"document.body.style.setProperty(\"-webkit-tap-highlight-color\", \"rgba(0,0,0,0)\");"
];
i'm working on an iOS app which has a tableview containing facebook friends' list, I want to manage this list for scrolling performance.
i don't want to load the images each time user open that friend list view, and for that images can be saved locally but at the same time it would not be a good practice as users can change their profile pictures after we already saved older ones to file system.
So i just wondering what is the best way to manage this?
Also how can i get those images in best possible optimized size?
thanks in advance
You might be able to use setImageWithURL:placeholderImage: in the UIImageView class in AFNetworking to load a placeholder (or existing image) and then request the latest image which will get updated when fetched.
The docs are here:
http://afnetworking.org/Documentation/Categories/UIImageView+AFNetworking.html
The project is here:
https://github.com/AFNetworking/AFNetworking/
UIImageView should take care of resizing the images.
Well if you want to set placeholder image it is possible. The FBProfilePictureView is a UIView but if take a look into the code you'll see that they add UIImageView to it. That's why you can use:
for (UIView *view in cell.faceImageView.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
[(UIImageView*)view setImage:[UIImage imageNamed:#"placeholder"]];
break;
}
}
Ofc, that's a workaround. However until they provide better thing you don't have to mess here with AFNetworking(which is a great lib btw) and finding facebook image url ;).