App is crashing while loading request in UIWebVIew - ObjectiveC - ios

I am loading an URL to my webView but just after starting loadRequest my App is crashing.
My WebView is inside a custom UIView Class with XIB and I am adding my view like this:
WebViewContainer *webViewContainer = [[[NSBundle mainBundle] loadNibNamed:#"WebViewContainer" owner:nil options:nil] firstObject];
[webViewContainer startLoadingURL:kPersonalWebsiteURL];
webViewContainer.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
[self.view addSubview:webViewContainer];
My startLoadingURL method:
- (void)startLoadingURL:(NSString *)url
{
NSURL *urlToLoad = [NSURL URLWithString:url];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlToLoad];
[self.webView loadRequest:requestObj];
}
Here is the Interface Builder Configuration:
Here is the Error Message i am only getting this error message not getting any logs.

Try to retype nil to self as the owner parameter. So you may have to replace:
WebViewContainer *webViewContainer = [[[NSBundle mainBundle] loadNibNamed:#"WebViewContainer" owner:nil options:nil]
with
WebViewContainer *webViewContainer = [[[NSBundle mainBundle] loadNibNamed:#"WebViewContainer" owner:self options:nil]

Related

Add a gif video file in UIWebview

I'm adding a GIF image in UIWbeview in Objective-C. The problem is that the image size doesn't fit in the screen and the contents above the gif video is not showing. How can I adjust the image size and show the buttons above it.
My code is:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"back" ofType:#"gif"];
NSData *gif = [NSData dataWithContentsOfFile:filePath];
UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
[webViewBG loadData:gif MIMEType:#"gif" textEncodingName:NULL baseURL:NULL];
webViewBG.userInteractionEnabled = NO;
webViewBG.scalesPageToFit=YES;
[self.view addSubview:webViewBG];
// Do any additional setup after loading the view.
}
You can move your code to viewDidAppear
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"back" ofType:#"gif"];
NSData *gif = [NSData dataWithContentsOfFile:filePath];
UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
[webViewBG loadData:gif MIMEType:#"image/gif" textEncodingName:#"UTF-8" baseURL:NULL];
webViewBG.userInteractionEnabled = NO;
webViewBG.scalesPageToFit=YES;
[self.view addSubview:webViewBG];
}

How to force an app to change language in iOS/Objective-C?

I'm having a problem with make app change language immediately like this app.
I've found a lot of similar question like this and this.
But, unfortunately, the related answers to these question don't work for me. I need my app change language immediately when I tap on an cell.
Any ideas? Thank in advance.
My solution
First you need to create the String File
Go to Project Right Click.
New File->iOS
Under iOS you can see the Resource
Click Resource and you can see the String File among the group of files.
Click that give name whatever you want.
Now you can create the string file below.
"Date"="Date";
"Time"="Time";
"Done"="Done";
"Back"="Back";
In appDelegate.h
-(NSString*) languageSelectedStringForKey:(NSString*) key
{
NSString *path;
AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
if(app.currentLanguage==ENGLISH)
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
else if(app.currentLanguage==TAMIL)
path = [[NSBundle mainBundle] pathForResource:#"ta-IN" ofType:#"lproj"];
else if(app.currentLanguage==SPANISH)
path = [[NSBundle mainBundle] pathForResource:#"es" ofType:#"lproj"];
else if(app.currentLanguage==FRENCH)
path = [[NSBundle mainBundle] pathForResource:#"fr" ofType:#"lproj"];
else if(app.currentLanguage==JAPANESE)
path = [[NSBundle mainBundle] pathForResource:#"ja" ofType:#"lproj"];
else if(app.currentLanguage==GERMAN)
path = [[NSBundle mainBundle] pathForResource:#"de" ofType:#"lproj"];
else if(app.currentLanguage==KOREAN)
path = [[NSBundle mainBundle] pathForResource:#"ko" ofType:#"lproj"];
else if(app.currentLanguage==RUSSIAN)
path = [[NSBundle mainBundle] pathForResource:#"ru" ofType:#"lproj"];
else if(app.currentLanguage==HINDI)
path = [[NSBundle mainBundle] pathForResource:#"hi" ofType:#"lproj"];
else if(app.currentLanguage==CHINESE)
path = [[NSBundle mainBundle] pathForResource:#"zh-Hans" ofType:#"lproj"];
else if(app.currentLanguage==ITALIAN)
path = [[NSBundle mainBundle] pathForResource:#"it" ofType:#"lproj"];
else if(app.currentLanguage==PORTUGUESE)
path = [[NSBundle mainBundle] pathForResource:#"pt" ofType:#"lproj"];
else if(app.currentLanguage==THAI)
path = [[NSBundle mainBundle] pathForResource:#"th" ofType:#"lproj"];
else if(app.currentLanguage==MALAY)
path = [[NSBundle mainBundle] pathForResource:#"ms" ofType:#"lproj"];
else if(app.currentLanguage==INDONESIAN)
path = [[NSBundle mainBundle] pathForResource:#"id" ofType:#"lproj"];
else if(app.currentLanguage==CHINESE1)
path = [[NSBundle mainBundle] pathForResource:#"zh-Hant" ofType:#"lproj"];
else
{
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
}
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[languageBundle localizedStringForKey:key value:#"" table:#"LocalizeSTRING"];
return str;
}
Call this above method in didFinishLaunchingWithOptions of appDelegate.m
NSString *str=[[[NSUserDefaults standardUserDefaults]objectForKey:#"AppleLanguages"] objectAtIndex:0];
if([str isEqualToString:[NSString stringWithFormat: #"en"]])
{
currentLanguage=ENGLISH;
selectedrow=ENGLISH;
}
else if([str isEqualToString:[NSString stringWithFormat: #"ta-IN"]])
{
currentLanguage=TAMIL;
selectedrow=TAMIL;
}
else if([str isEqualToString:[NSString stringWithFormat: #"es"]])
{
currentLanguage=SPANISH;
selectedrow=SPANISH;
}
else if([str isEqualToString:[NSString stringWithFormat: #"fr"]])
{
currentLanguage=FRENCH;
selectedrow=FRENCH;
}
else if([str isEqualToString:[NSString stringWithFormat: #"ja"]])
{
currentLanguage=JAPANESE;
selectedrow=JAPANESE;
}
else if([str isEqualToString:[NSString stringWithFormat: #"de"]])
{
currentLanguage=GERMAN;
selectedrow=GERMAN;
}
else if([str isEqualToString:[NSString stringWithFormat: #"ko"]])
{
currentLanguage=KOREAN;
selectedrow=KOREAN;
}
else if([str isEqualToString:[NSString stringWithFormat: #"ru"]])
{
currentLanguage=RUSSIAN;
selectedrow=RUSSIAN;
}
else if([str isEqualToString:[NSString stringWithFormat: #"hi"]])
{
currentLanguage=HINDI;
selectedrow=HINDI;
}
else if([str isEqualToString:[NSString stringWithFormat: #"zh-Hans"]])
{
currentLanguage=CHINESE;
selectedrow=CHINESE;
}
else if([str isEqualToString:[NSString stringWithFormat: #"it"]])
{
currentLanguage=ITALIAN;
selectedrow=ITALIAN;
}
else if([str isEqualToString:[NSString stringWithFormat: #"pt"]])
{
currentLanguage=PORTUGUESE;
selectedrow=PORTUGUESE;
}
else if([str isEqualToString:[NSString stringWithFormat: #"th"]])
{
currentLanguage=THAI;
selectedrow=THAI;
}
else if([str isEqualToString:[NSString stringWithFormat: #"ms"]])
{
currentLanguage=MALAY;
selectedrow=MALAY;
}
else if([str isEqualToString:[NSString stringWithFormat: #"id"]])
{
currentLanguage=INDONESIAN;
selectedrow=INDONESIAN;
}
else if([str isEqualToString:[NSString stringWithFormat: #"zh-Hant"]])
{
currentLanguage=CHINESE1;
selectedrow=CHINESE1;
}
[self languageSelectedStringForKey:str];
Then in your Required View Controller
-(NSString*) languageSelectedStringForKey:(NSString*) key
{
app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
if(app.currentLanguage==ENGLISH)
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
else if(app.currentLanguage==TAMIL)
path = [[NSBundle mainBundle] pathForResource:#"ta-IN" ofType:#"lproj"];
else if(app.currentLanguage==SPANISH)
path = [[NSBundle mainBundle] pathForResource:#"es" ofType:#"lproj"];
else if(app.currentLanguage==FRENCH)
path = [[NSBundle mainBundle] pathForResource:#"fr" ofType:#"lproj"];
else if(app.currentLanguage==JAPANESE)
path = [[NSBundle mainBundle] pathForResource:#"ja" ofType:#"lproj"];
else if(app.currentLanguage==GERMAN)
path = [[NSBundle mainBundle] pathForResource:#"de" ofType:#"lproj"];
else if(app.currentLanguage==KOREAN)
path = [[NSBundle mainBundle] pathForResource:#"ko" ofType:#"lproj"];
else if(app.currentLanguage==RUSSIAN)
path = [[NSBundle mainBundle] pathForResource:#"ru" ofType:#"lproj"];
else if(app.currentLanguage==HINDI)
path = [[NSBundle mainBundle] pathForResource:#"hi" ofType:#"lproj"];
else if(app.currentLanguage==CHINESE)
path = [[NSBundle mainBundle] pathForResource:#"zh-Hans" ofType:#"lproj"];
else if(app.currentLanguage==ITALIAN)
path = [[NSBundle mainBundle] pathForResource:#"it" ofType:#"lproj"];
else if(app.currentLanguage==PORTUGUESE)
path = [[NSBundle mainBundle] pathForResource:#"pt" ofType:#"lproj"];
else if(app.currentLanguage==THAI)
path = [[NSBundle mainBundle] pathForResource:#"th" ofType:#"lproj"];
else if(app.currentLanguage==MALAY)
path = [[NSBundle mainBundle] pathForResource:#"ms" ofType:#"lproj"];
else if(app.currentLanguage==INDONESIAN)
path = [[NSBundle mainBundle] pathForResource:#"id" ofType:#"lproj"];
else if(app.currentLanguage==CHINESE1)
path = [[NSBundle mainBundle] pathForResource:#"zh-Hant" ofType:#"lproj"];
else
{
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
}
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[languageBundle localizedStringForKey:key value:#"" table:#"LocalizeSTRING"];
return str;
}
Call this above method in your viewDidLoad and viewWillAppear method or Call that method where you want to call
- (void)viewWillAppear:(BOOL)animated
{
//If you want to set language in label,TextFiled
localizeBackLabel.text=[self languageSelectedStringForKey:#"Back"];
localizeDoneLabel.text=[self languageSelectedStringForKey:#"Done"];
localizeTimeLabel.text=[self languageSelectedStringForKey:#"Time"];
localizeDateLabel.text=[self languageSelectedStringForKey:#"Date"];
//If you want to set language in button
[yourButton setTitle:[self languageSelectedStringForKey:#"Back"]; forState:UIControlStateNormal];
[yourButton setTitle:[self languageSelectedStringForKey:#"Done"]; forState:UIControlStateNormal];
//If you want to set the Language in cell
cell.labelHomeList.text=[self languageSelectedStringForKey:#"Date"];
cell.labelHomeList.text=[self languageSelectedStringForKey:#"Time"];
}
Finally if you want to change the language to other language.For example in (Settings) table you have two languages.If you want to change it to other languages please follow the below coding
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
app.selectedrow=indexPath.row;
if (indexPath.row==0)
{
app.currentLanguage=ENGLISH;
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", nil]forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%d", [[NSUserDefaults standardUserDefaults] synchronize]);
}
else
{
app.currentLanguage=CHINESE1;
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"zh-Hant", nil]forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
it works perfectly.
use reloadData.
[YourUITableView reloadData];
and don't forget to save your language in a NSUserDefaults,
[[NSUserDefaults standardUserDefaults] setValue:lang forKey:LANG];
[[NSUserDefaults standardUserDefaults] synchronize];
so you can load your views and storyboards accordingly because when your tableView is reloaded, you can add your check for language to display the correct view in the cellForRowAtIndexPath method.
Edit.....
If you want to use NSNotification, register your observer in your viewDidLoad, as this
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateLanguage:)
name:#"updateLanguageObserver"
object:nil];
and in your didSelectRowAtIndexPath add the listener
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateLanguageObserver" object:self userInfo:nil];
which will call your method updateLanguage where you can handle your localization and tableView reloading.
and don't forget in the viewController that adds the listener to remove observer as such
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
You should look at this once : HERE
I think that this will solve your problem.
I had the same problem 1 month ago.
You can follow this tutorial.
Example:
In your .strings file you put translation: "hello" = "HOLA MUNDO";
#import "LocalizationSystem.h"
LocalizationSetLanguage(#"Spanish");
CCLabel* label = [CCLabel labelWithString:AMLocalizedString(#"hello",#"Hello World") fontName:#"Marker Felt" fontSize:32];
EDIT: This is additional information as user asks in comment:If you want to translate whole view where your language option is changed, I propose you to make method that will be called everytime you change language:
-(void)translateOnChoose {
self.label.text = AMLocalizedString(#"hello",#"Hello World");
}

iPad PDF Rendering too small. Not to screen size

I'm trying to fully render a PDF in an iPad webview but the document displays too small and not the full size as to what it should be. The PDF renders perfectly on the iPhone just not the iPad webview. Please let me know if you need more information.
Main View
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSString *url = [chapterFiles objectAtIndex:indexPath.row];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource: [chapterFiles objectAtIndex:indexPath.row] ofType:#"pdf"]];
NSLog(#"Selected File: %#",url);
//transfer selected
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
chapterDetailViewController *detailView = (chapterDetailViewController *)[sb instantiateViewControllerWithIdentifier:#"chapterDetails"];
detailView.url = url;
[self.navigationController pushViewController:detailView animated:YES];
}
Detail View:
//NSString *path = [[NSBundle mainBundle] pathForResource:url ofType:#"pdf"];
//NSURL *targetURL = [NSURL fileURLWithPath:url];
NSURLRequest *siteRequest = [NSURLRequest requestWithURL:url];
[chapterWebView loadRequest:siteRequest];
chapterWebView.delegate = self;
//[chapterWebView setScalesPageToFit:YES];
chapterWebView.scalesPageToFit = YES;
// chapterWebView.contentMode = UIViewContentModeScaleToFill;
// chapterWebView.multipleTouchEnabled = YES;
// chapterWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// [self.view addSubview:chapterWebView];
}
You are instantiating a view controller on a storyboard whose filename has "iPhone" in it. You're probably instantiating a view controller from the wrong storyboard. Make sure you're initializing the right storyboard for the right device.

Load Different HTML in iOS UIWebView for iPhone and iPad

I can get both iPhone and iPad to load the same HTML file, however I would like to load a more optimized page based on which device the program is loading on.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_webView loadHTMLString:html baseURL:baseURL];
// disable scrolling
_webView.scrollView.scrollEnabled = NO;
_webView.scrollView.bounces = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have tried to create a new outlet, but I seem to be running into overlapping code.
How can I load different HTML files for iPad and iPhone with a universal build?
Just use one web view. All you need is two different HTML files.
Then you code can be something like:
NSURL *url;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
url = [[NSBundle mainBundle] URLForResource:#"index-iphone" withExtension:#"html"];
} else {
url = [[NSBundle mainBundle] URLForResource:#"index-ipad" withExtension:#"html"];
}
This assumes you have both an index-iphone.html and a index-ipad.html file in your app bundle.

Locate a html file in UIWebView

I have a NotificationVC containing table view having 10 cells index 0 to 9. on click of each cell NotificationWebVC should open up that has a UIWebView that loads diff. html depending upon which cell is selected in NotificationVC. Right now my code works only for index=0 , but not for other indexes , seems bit weird.
NotificationWebVC
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:_resourceName ofType:#"html" inDirectory:nil] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
Please suggest. _resourceName is a property having the name of html file.
try this code
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:_resourceName ofType:#"html" inDirectory:nil] ;
[_webView loadHTMLString:htmlFile baseURL:nil];
_webView.delegate=(id)self;
First you need to make an array containing your 10 urls. Then when a user tap on each cell, use the didSelectRowAtIndexPath delegate method.
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:[resources objectAtIndex:indexPath.row] ofType:#"html" inDirectory:nil] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
Add line of code to NotificationWebVC.h
#property (nonatomic,retain)NSString *resopnseString;
then in .m class synthise it
#synthesize resopnseString;
after this go to didSelectMethod of tablevew datasouce in NotificationVC.m
there you add the following code
NotificationWebVC *notif=[[NotificationWebVC alloc]init];
notif.resopnseString=[resources objectAtIndex:indexPath.row];
//here it may present view or push view or what ever it be
After doing all this now you shift to NotificationWebVC.m
add the following code to in viewDidLoad method
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:resopnseString ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
after doing all this pl z be sure to add all 10 HTML file in your project
and try to check that you are getting correct html file on select in table from
be sure you are passing correct name of file in responsString
[resources objectAtIndex:indexPath.row];
or better put
NSLog(#"%#",[resources objectAtIndex:indexPath.row]);
to it check it.
i think this code will help you
// docfileName should be your file name call this method in tableview delegate didSelectrow
-(void)loadLocaldataonWebview :(NSString *)docfileName
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:docfileName ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[documentsWebView loadHTMLString:htmlString baseURL:nil];
}
then in viewDidload add the following code
-(void)viewDidLoad{
documentsWebView=[[UIWebView alloc]init];
documentsWebView.delegate=self;
documentsWebView.frame=CGRectMake(0, yAxis, kWidth, 616);
documentsWebView.backgroundColor=[UIColor clearColor];
[self.view addSubview:documentsWebView];
}

Resources